diff --git a/rules-tests/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector/Fixture/on_return.php.inc b/rules-tests/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector/Fixture/on_return.php.inc new file mode 100644 index 00000000..13eaea5a --- /dev/null +++ b/rules-tests/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector/Fixture/on_return.php.inc @@ -0,0 +1,29 @@ + +----- +setAccessible(true); + return $reflection; + } +} + +?> diff --git a/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php b/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php index ef3180c4..323fc1da 100644 --- a/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php +++ b/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php @@ -9,9 +9,13 @@ use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; +use PhpParser\Node\Stmt\Return_; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; +use Rector\Naming\Naming\VariableNaming; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -23,6 +27,11 @@ */ final class DowngradeSetAccessibleReflectionPropertyRector extends AbstractRector { + public function __construct( + private readonly VariableNaming $variableNaming + ) { + } + public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -80,32 +89,54 @@ public function refactor(Node $node): ?Node $hasChanged = false; foreach ($node->stmts as $key => $stmt) { - if (! $stmt instanceof Expression) { - continue; - } - - if (! $stmt->expr instanceof Assign) { + if (! $stmt instanceof Expression && ! $stmt instanceof Return_) { continue; } - $assign = $stmt->expr; - if (! $assign->expr instanceof New_) { - continue; + if ($stmt instanceof Expression) { + if (! $stmt->expr instanceof Assign) { + continue; + } + + $assign = $stmt->expr; + if (! $assign->expr instanceof New_) { + continue; + } + + $new = $assign->expr; + $variable = $assign->var; + } else { + if (! $stmt->expr instanceof New_) { + continue; + } + + $new = $stmt->expr; + $scope = ScopeFetcher::fetch($stmt); + $variable = new Variable($this->variableNaming->createCountedValueName('reflection', $scope)); } - $new = $assign->expr; if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) { continue; } - // next stmts should be setAccessible() call - $nextStmt = $node->stmts[$key + 1] ?? null; - if ($this->isSetAccessibleMethodCall($nextStmt)) { - continue; + if ($stmt instanceof Expression) { + // next stmts should be setAccessible() call + $nextStmt = $node->stmts[$key + 1] ?? null; + if ($this->isSetAccessibleMethodCall($nextStmt)) { + continue; + } + + array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($variable)]); + } else { + $previousStmts = [ + new Expression(new Assign($variable, $new)), + $this->createSetAccessibleExpression($variable), + ]; + + $stmt->expr = $variable; + array_splice($node->stmts, $key - 2, 0, $previousStmts); } - array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($assign->var)]); - $hasChanged = true; }