Skip to content

Commit

Permalink
[DeadCode] Keep Assign expr on RemoveUnusedVariableAssignRector for i…
Browse files Browse the repository at this point in the history
…mpure function (#282)
  • Loading branch information
samsonasik authored Jun 25, 2021
1 parent 4f619aa commit 6162f70
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

final class OnObGetClean
{
public function run()
{
echo 'run';
}

public function execute()
{
ob_start();

$this->run();

$result = ob_get_clean();

echo 'done';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

final class OnObGetClean
{
public function run()
{
echo 'run';
}

public function execute()
{
ob_start();

$this->run();

ob_get_clean();

echo 'done';
}
}

?>
17 changes: 15 additions & 2 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rector\Core\PhpParser\Comparing\ConditionSearcher;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\NodeAnalyzer\UsedVariableNameAnalyzer;
use Rector\DeadCode\SideEffect\PureFunctionDetector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -35,7 +36,8 @@ public function __construct(
private ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
private CompactFuncCallAnalyzer $compactFuncCallAnalyzer,
private ConditionSearcher $conditionSearcher,
private UsedVariableNameAnalyzer $usedVariableNameAnalyzer
private UsedVariableNameAnalyzer $usedVariableNameAnalyzer,
private PureFunctionDetector $pureFunctionDetector
) {
}

Expand Down Expand Up @@ -102,7 +104,9 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->expr instanceof MethodCall || $node->expr instanceof StaticCall) {
if ($node->expr instanceof MethodCall || $node->expr instanceof StaticCall || $this->isImpureFunction(
$node->expr
)) {
// keep the expr, can have side effect
return $node->expr;
}
Expand All @@ -111,6 +115,15 @@ public function refactor(Node $node): ?Node
return $node;
}

private function isImpureFunction(Expr $expr): bool
{
if (! $expr instanceof FuncCall) {
return false;
}

return ! $this->pureFunctionDetector->detect($expr);
}

private function shouldSkip(Assign $assign): bool
{
$classMethod = $assign->getAttribute(AttributeKey::METHOD_NODE);
Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/SideEffect/PureFunctionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class PureFunctionDetector
'header', 'header_remove', 'http_response_code', 'setcookie',

// output buffer
'ob_start', 'ob_end_clean', 'readfile', 'printf', 'var_dump', 'phpinfo',
'ob_start', 'ob_end_clean', 'ob_get_clean', 'readfile', 'printf', 'var_dump', 'phpinfo',
'ob_implicit_flush', 'vprintf',

// mcrypt
Expand Down

0 comments on commit 6162f70

Please sign in to comment.