Skip to content

Commit 2681751

Browse files
authored
[DeadCode] Handle multi vars on RemoveNonExistingVarAnnotationRector (#7780)
* [DeadCode] Handle multi vars on RemoveNonExistingVarAnnotationRector * [DeadCode] Handle multi vars on RemoveNonExistingVarAnnotationRector * ensure update docblock only on has changed inside loop
1 parent ee36fdd commit 2681751

2 files changed

Lines changed: 56 additions & 24 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* @var \App\View\AppView $this
5+
* @var array<\App\Model\SomeEntity> $sickDays
6+
*/
7+
foreach ($sickDays as $vacation) {
8+
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
/**
16+
* @var array<\App\Model\SomeEntity> $sickDays
17+
*/
18+
foreach ($sickDays as $vacation) {
19+
20+
}
21+
22+
?>

rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function refactor(Node $node): ?Node
114114
$extractValues = [];
115115

116116
foreach ($node->stmts as $key => $stmt) {
117+
$hasChangedStmt = false;
117118
if ($stmt instanceof Expression && $stmt->expr instanceof FuncCall && $this->isName(
118119
$stmt->expr,
119120
'extract'
@@ -146,38 +147,47 @@ public function refactor(Node $node): ?Node
146147

147148
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($stmt);
148149

149-
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
150-
if (! $varTagValueNode instanceof VarTagValueNode) {
151-
continue;
152-
}
150+
$varTagValueNodes = $phpDocInfo->getPhpDocNode()
151+
->getVarTagValues();
153152

154-
if ($this->isObjectShapePseudoType($varTagValueNode)) {
155-
continue;
156-
}
153+
foreach ($varTagValueNodes as $varTagValueNode) {
154+
if ($this->isObjectShapePseudoType($varTagValueNode)) {
155+
continue;
156+
}
157157

158-
$variableName = ltrim($varTagValueNode->variableName, '$');
158+
$variableName = ltrim($varTagValueNode->variableName, '$');
159159

160-
if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) {
161-
continue;
162-
}
160+
if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) {
161+
continue;
162+
}
163163

164-
if ($this->hasVariableName($stmt, $variableName)) {
165-
continue;
166-
}
164+
if ($this->hasVariableName($stmt, $variableName)) {
165+
continue;
166+
}
167167

168-
$comments = $node->getComments();
169-
if (isset($comments[1])) {
170-
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
171-
continue;
172-
}
168+
$comments = $node->getComments();
169+
if (isset($comments[1])) {
170+
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
171+
continue;
172+
}
173173

174-
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $variableName)) {
175-
continue;
174+
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $variableName)) {
175+
continue;
176+
}
177+
178+
if ($variableName === '') {
179+
$phpDocInfo->removeByType(VarTagValueNode::class);
180+
} else {
181+
$phpDocInfo->removeByType(VarTagValueNode::class, $variableName);
182+
}
183+
184+
$hasChangedStmt = true;
176185
}
177186

178-
$phpDocInfo->removeByType(VarTagValueNode::class);
179-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
180-
$hasChanged = true;
187+
if ($hasChangedStmt) {
188+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
189+
$hasChanged = true;
190+
}
181191
}
182192

183193
if ($hasChanged) {

0 commit comments

Comments
 (0)