|
9 | 9 | use PhpParser\Node\Expr\Assign; |
10 | 10 | use PhpParser\Node\Expr\MethodCall; |
11 | 11 | use PhpParser\Node\Expr\New_; |
| 12 | +use PhpParser\Node\Expr\Variable; |
12 | 13 | use PhpParser\Node\Stmt; |
13 | 14 | use PhpParser\Node\Stmt\Expression; |
| 15 | +use PhpParser\Node\Stmt\Return_; |
14 | 16 | use Rector\Contract\PhpParser\Node\StmtsAwareInterface; |
| 17 | +use Rector\Naming\Naming\VariableNaming; |
| 18 | +use Rector\PHPStan\ScopeFetcher; |
15 | 19 | use Rector\Rector\AbstractRector; |
16 | 20 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
17 | 21 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
|
23 | 27 | */ |
24 | 28 | final class DowngradeSetAccessibleReflectionPropertyRector extends AbstractRector |
25 | 29 | { |
| 30 | + public function __construct( |
| 31 | + private readonly VariableNaming $variableNaming |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
26 | 35 | public function getRuleDefinition(): RuleDefinition |
27 | 36 | { |
28 | 37 | return new RuleDefinition( |
@@ -80,32 +89,54 @@ public function refactor(Node $node): ?Node |
80 | 89 | $hasChanged = false; |
81 | 90 |
|
82 | 91 | foreach ($node->stmts as $key => $stmt) { |
83 | | - if (! $stmt instanceof Expression) { |
| 92 | + if (! $stmt instanceof Expression && ! $stmt instanceof Return_) { |
84 | 93 | continue; |
85 | 94 | } |
86 | 95 |
|
87 | | - if (! $stmt->expr instanceof Assign) { |
88 | | - continue; |
89 | | - } |
| 96 | + if ($stmt instanceof Expression) { |
| 97 | + if (! $stmt->expr instanceof Assign) { |
| 98 | + continue; |
| 99 | + } |
90 | 100 |
|
91 | | - $assign = $stmt->expr; |
92 | | - if (! $assign->expr instanceof New_) { |
93 | | - continue; |
| 101 | + $assign = $stmt->expr; |
| 102 | + if (! $assign->expr instanceof New_) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + $new = $assign->expr; |
| 107 | + } else { |
| 108 | + if (! $stmt->expr instanceof New_) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + $new = $stmt->expr; |
94 | 113 | } |
95 | 114 |
|
96 | | - $new = $assign->expr; |
97 | 115 | if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) { |
98 | 116 | continue; |
99 | 117 | } |
100 | 118 |
|
101 | | - // next stmts should be setAccessible() call |
102 | | - $nextStmt = $node->stmts[$key + 1] ?? null; |
103 | | - if ($this->isSetAccessibleMethodCall($nextStmt)) { |
104 | | - continue; |
| 119 | + if ($stmt instanceof Expression) { |
| 120 | + // next stmts should be setAccessible() call |
| 121 | + $nextStmt = $node->stmts[$key + 1] ?? null; |
| 122 | + if ($this->isSetAccessibleMethodCall($nextStmt)) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($assign->var)]); |
| 127 | + } else { |
| 128 | + $scope = ScopeFetcher::fetch($stmt); |
| 129 | + $variable = new Variable($this->variableNaming->createCountedValueName('reflection', $scope)); |
| 130 | + |
| 131 | + $previousStmts = [ |
| 132 | + new Expression(new Assign($variable, $new)), |
| 133 | + $this->createSetAccessibleExpression($variable), |
| 134 | + ]; |
| 135 | + |
| 136 | + $stmt->expr = $variable; |
| 137 | + array_splice($node->stmts, $key - 2, 0, $previousStmts); |
105 | 138 | } |
106 | 139 |
|
107 | | - array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($assign->var)]); |
108 | | - |
109 | 140 | $hasChanged = true; |
110 | 141 | } |
111 | 142 |
|
|
0 commit comments