Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Skip static variable on SimplifyUselessVariableRector (#…
…630)
- Loading branch information
1 parent
fa8e3bd
commit c3e0ff3
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...Quality/Rector/Return_/SimplifyUselessVariableRector/Fixture/skip_static_variable.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Return_\SimplifyUselessVariableRector\Fixture; | ||
|
||
class SkipStaticVariable | ||
{ | ||
public function run() | ||
{ | ||
static $content; | ||
|
||
$content .= 'test'; | ||
|
||
return $content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt\Static_; | ||
use Rector\Core\PhpParser\Comparing\NodeComparator; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
|
||
final class VariableAnalyzer | ||
{ | ||
public function __construct( | ||
private BetterNodeFinder $betterNodeFinder, | ||
private NodeComparator $nodeComparator | ||
) { | ||
} | ||
|
||
public function isStatic(Variable $variable): bool | ||
{ | ||
return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (Node $n) use ( | ||
$variable | ||
): bool { | ||
if (! $n instanceof Static_) { | ||
return false; | ||
} | ||
|
||
foreach ($n->vars as $staticVar) { | ||
if ($this->nodeComparator->areNodesEqual($staticVar->var, $variable)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}); | ||
} | ||
} |