Skip to content
Merged
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
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