Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Keep Assign expr on RemoveUnusedVariableAssignRector for impure function #282

Merged
merged 4 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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