From ed7f7f97adaed8a3e960424842f572ae3c89e1d8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 23 Jul 2025 16:03:09 +0700 Subject: [PATCH 1/3] [DowngradePhp81] Handle on return on DowngradeSetAccessibleReflectionPropertyRector --- .../Fixture/on_return.php.inc | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 rules-tests/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector/Fixture/on_return.php.inc 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; + } +} + +?> From 62c599d653dccacbd7dd15bb9f255d8d982fbd47 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 23 Jul 2025 16:15:22 +0700 Subject: [PATCH 2/3] implemented --- ...eSetAccessibleReflectionPropertyRector.php | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php b/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php index ef3180c4..e7e85ad8 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) { + if (! $stmt instanceof Expression && ! $stmt instanceof Return_) { continue; } - if (! $stmt->expr instanceof Assign) { - continue; - } + if ($stmt instanceof Expression) { + if (! $stmt->expr instanceof Assign) { + continue; + } - $assign = $stmt->expr; - if (! $assign->expr instanceof New_) { - continue; + $assign = $stmt->expr; + if (! $assign->expr instanceof New_) { + continue; + } + + $new = $assign->expr; + } else { + if (! $stmt->expr instanceof New_) { + continue; + } + + $new = $stmt->expr; } - $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($assign->var)]); + } else { + $scope = ScopeFetcher::fetch($stmt); + $variable = new Variable($this->variableNaming->createCountedValueName('reflection', $scope)); + + $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; } From f2077448f5db291579e156a33ac19ab7c05a7388 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 23 Jul 2025 16:17:20 +0700 Subject: [PATCH 3/3] implemented --- .../DowngradeSetAccessibleReflectionPropertyRector.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php b/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php index e7e85ad8..323fc1da 100644 --- a/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php +++ b/rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php @@ -104,12 +104,15 @@ public function refactor(Node $node): ?Node } $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)); } if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) { @@ -123,11 +126,8 @@ public function refactor(Node $node): ?Node continue; } - array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($assign->var)]); + array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($variable)]); } else { - $scope = ScopeFetcher::fetch($stmt); - $variable = new Variable($this->variableNaming->createCountedValueName('reflection', $scope)); - $previousStmts = [ new Expression(new Assign($variable, $new)), $this->createSetAccessibleExpression($variable),