Skip to content

Commit

Permalink
Make SimplifyUselessVariableRector run without scope (#4791)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Aug 14, 2023
1 parent 2fe68ad commit 685fe90
Showing 1 changed file with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\MixedType;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeAnalyzer\CallAnalyzer;
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector\SimplifyUselessVariableRectorTest
*/
final class SimplifyUselessVariableRector extends AbstractScopeAwareRector
final class SimplifyUselessVariableRector extends AbstractRector
{
public function __construct(
private readonly AssignAndBinaryMap $assignAndBinaryMap,
Expand Down Expand Up @@ -67,7 +65,7 @@ public function getNodeTypes(): array
/**
* @param StmtsAwareInterface $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
$stmts = $node->stmts;
if ($stmts === null) {
Expand All @@ -85,7 +83,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

$previousStmt = $stmts[$key - 1];
if ($this->shouldSkipStmt($stmt, $previousStmt, $scope)) {
if ($this->shouldSkipStmt($stmt, $previousStmt)) {
return null;
}

Expand Down Expand Up @@ -127,17 +125,12 @@ private function processSimplifyUselessVariable(
return $stmtsAware;
}

private function shouldSkipStmt(Return_ $return, Stmt $previousStmt, Scope $scope): bool
private function shouldSkipStmt(Return_ $return, Stmt $previousStmt): bool
{
if (! $return->expr instanceof Variable) {
return true;
}

$functionReflection = $scope->getFunction();
if ($functionReflection instanceof FunctionReflection && $functionReflection->returnsByReference()->yes()) {
return true;
}

if ($return->getAttribute(AttributeKey::IS_BYREF_RETURN) === true) {
return true;
}
Expand Down

0 comments on commit 685fe90

Please sign in to comment.