diff --git a/rules-tests/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector/Fixture/multi_vars.php.inc b/rules-tests/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector/Fixture/multi_vars.php.inc new file mode 100644 index 00000000000..34d655a3b12 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector/Fixture/multi_vars.php.inc @@ -0,0 +1,22 @@ + $sickDays + */ +foreach ($sickDays as $vacation) { + +} + +?> +----- + $sickDays + */ +foreach ($sickDays as $vacation) { + +} + +?> diff --git a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php index 55193a04a9b..ab5d807e861 100644 --- a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php +++ b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php @@ -114,6 +114,7 @@ public function refactor(Node $node): ?Node $extractValues = []; foreach ($node->stmts as $key => $stmt) { + $hasChangedStmt = false; if ($stmt instanceof Expression && $stmt->expr instanceof FuncCall && $this->isName( $stmt->expr, 'extract' @@ -146,38 +147,47 @@ public function refactor(Node $node): ?Node $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($stmt); - $varTagValueNode = $phpDocInfo->getVarTagValueNode(); - if (! $varTagValueNode instanceof VarTagValueNode) { - continue; - } + $varTagValueNodes = $phpDocInfo->getPhpDocNode() + ->getVarTagValues(); - if ($this->isObjectShapePseudoType($varTagValueNode)) { - continue; - } + foreach ($varTagValueNodes as $varTagValueNode) { + if ($this->isObjectShapePseudoType($varTagValueNode)) { + continue; + } - $variableName = ltrim($varTagValueNode->variableName, '$'); + $variableName = ltrim($varTagValueNode->variableName, '$'); - if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) { - continue; - } + if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) { + continue; + } - if ($this->hasVariableName($stmt, $variableName)) { - continue; - } + if ($this->hasVariableName($stmt, $variableName)) { + continue; + } - $comments = $node->getComments(); - if (isset($comments[1])) { - // skip edge case with double comment, as impossible to resolve by PHPStan doc parser - continue; - } + $comments = $node->getComments(); + if (isset($comments[1])) { + // skip edge case with double comment, as impossible to resolve by PHPStan doc parser + continue; + } - if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $variableName)) { - continue; + if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $variableName)) { + continue; + } + + if ($variableName === '') { + $phpDocInfo->removeByType(VarTagValueNode::class); + } else { + $phpDocInfo->removeByType(VarTagValueNode::class, $variableName); + } + + $hasChangedStmt = true; } - $phpDocInfo->removeByType(VarTagValueNode::class); - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt); - $hasChanged = true; + if ($hasChangedStmt) { + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt); + $hasChanged = true; + } } if ($hasChanged) {