Skip to content

Commit cc7b45e

Browse files
samsonasikmpesari
andauthored
[DeadCode] Skip vars exists in static vars on RemoveNonExistingVarAnnotationRector (#7463)
* Test RemoveNonExistingVarAnnotationRector with static variable in method * Closes #7456 Fixes rectorphp/rector#9428 * Closes #7456 Fixes rectorphp/rector#9428 --------- Co-authored-by: Mikko Pesari <[email protected]>
1 parent d830051 commit cc7b45e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;
4+
5+
final class SkipStaticVariableInMethod
6+
{
7+
public function run(int $param): int
8+
{
9+
/**
10+
* Rector should not remove this @var annotation.
11+
*
12+
* @var array<int,int>
13+
*/
14+
static $cached_result = [];
15+
16+
if (!isset($cached_result[$param])) {
17+
$cached_result[$param] = doExpensiveCalculation($param);
18+
}
19+
20+
return $cached_result[$param];
21+
}
22+
}

rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ public function refactor(Node $node): ?Node
128128
continue;
129129
}
130130

131+
if ($stmt instanceof Static_) {
132+
foreach ($stmt->vars as $staticVar) {
133+
$staticVarName = $this->getName($staticVar->var);
134+
135+
if ($staticVarName === null) {
136+
continue;
137+
}
138+
139+
$extractValues[] = $staticVarName;
140+
}
141+
}
142+
131143
if ($this->shouldSkip($node, $key, $stmt, $extractValues)) {
132144
continue;
133145
}

0 commit comments

Comments
 (0)