Skip to content

Commit

Permalink
[DeadCode] Skip global and static variable on RemoveJustVariableAssig…
Browse files Browse the repository at this point in the history
…nRector (#2641)

* [DeadCode] Skip global var on RemoveJustVariableAssignRector

* update

* skip static as well

* skip static as well

* skip static as well

* s

* Fixed 🎉

* eol
  • Loading branch information
samsonasik committed Jul 8, 2022
1 parent b67bf64 commit bf92b9c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector\Fixture;

$result = 1;

function run()
{
global $result;
$result = 100;

$temporaryValue = $result;
}
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector\Fixture;

class SkipStaticVar
{
private $property;

function init($value)
{
static $result = null;

if (! isset($result)) {
$result = $value;
$this->property = $result;
}
}
}
Expand Up @@ -159,6 +159,10 @@ private function areTwoVariablesCrossAssign(Assign $currentAssign, Assign $nextA
return false;
}

if ($this->variableAnalyzer->isStaticOrGlobal($currentAssign->var)) {
return false;
}

return ! $this->exprUsedInNextNodeAnalyzer->isUsed($nextAssign->expr);
}

Expand Down

0 comments on commit bf92b9c

Please sign in to comment.