Skip to content

Commit 62c599d

Browse files
committed
implemented
1 parent ed7f7f9 commit 62c599d

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
use PhpParser\Node\Expr\Assign;
1010
use PhpParser\Node\Expr\MethodCall;
1111
use PhpParser\Node\Expr\New_;
12+
use PhpParser\Node\Expr\Variable;
1213
use PhpParser\Node\Stmt;
1314
use PhpParser\Node\Stmt\Expression;
15+
use PhpParser\Node\Stmt\Return_;
1416
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
17+
use Rector\Naming\Naming\VariableNaming;
18+
use Rector\PHPStan\ScopeFetcher;
1519
use Rector\Rector\AbstractRector;
1620
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1721
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -23,6 +27,11 @@
2327
*/
2428
final class DowngradeSetAccessibleReflectionPropertyRector extends AbstractRector
2529
{
30+
public function __construct(
31+
private readonly VariableNaming $variableNaming
32+
) {
33+
}
34+
2635
public function getRuleDefinition(): RuleDefinition
2736
{
2837
return new RuleDefinition(
@@ -80,32 +89,54 @@ public function refactor(Node $node): ?Node
8089
$hasChanged = false;
8190

8291
foreach ($node->stmts as $key => $stmt) {
83-
if (! $stmt instanceof Expression) {
92+
if (! $stmt instanceof Expression && ! $stmt instanceof Return_) {
8493
continue;
8594
}
8695

87-
if (! $stmt->expr instanceof Assign) {
88-
continue;
89-
}
96+
if ($stmt instanceof Expression) {
97+
if (! $stmt->expr instanceof Assign) {
98+
continue;
99+
}
90100

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;
94113
}
95114

96-
$new = $assign->expr;
97115
if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) {
98116
continue;
99117
}
100118

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);
105138
}
106139

107-
array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($assign->var)]);
108-
109140
$hasChanged = true;
110141
}
111142

0 commit comments

Comments
 (0)