Skip to content

Commit 3ca042a

Browse files
authored
[DowngradePhp81] Handle on return on DowngradeSetAccessibleReflectionPropertyRector (#297)
* [DowngradePhp81] Handle on return on DowngradeSetAccessibleReflectionPropertyRector * implemented * implemented
1 parent 9d80512 commit 3ca042a

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;
4+
5+
class OnReturn
6+
{
7+
public function run($object)
8+
{
9+
return new \ReflectionMethod($object, 'bar');
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;
18+
19+
class OnReturn
20+
{
21+
public function run($object)
22+
{
23+
$reflection = new \ReflectionMethod($object, 'bar');
24+
$reflection->setAccessible(true);
25+
return $reflection;
26+
}
27+
}
28+
29+
?>

rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php

Lines changed: 46 additions & 15 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) {
84-
continue;
85-
}
86-
87-
if (! $stmt->expr instanceof Assign) {
92+
if (! $stmt instanceof Expression && ! $stmt instanceof Return_) {
8893
continue;
8994
}
9095

91-
$assign = $stmt->expr;
92-
if (! $assign->expr instanceof New_) {
93-
continue;
96+
if ($stmt instanceof Expression) {
97+
if (! $stmt->expr instanceof Assign) {
98+
continue;
99+
}
100+
101+
$assign = $stmt->expr;
102+
if (! $assign->expr instanceof New_) {
103+
continue;
104+
}
105+
106+
$new = $assign->expr;
107+
$variable = $assign->var;
108+
} else {
109+
if (! $stmt->expr instanceof New_) {
110+
continue;
111+
}
112+
113+
$new = $stmt->expr;
114+
$scope = ScopeFetcher::fetch($stmt);
115+
$variable = new Variable($this->variableNaming->createCountedValueName('reflection', $scope));
94116
}
95117

96-
$new = $assign->expr;
97118
if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) {
98119
continue;
99120
}
100121

101-
// next stmts should be setAccessible() call
102-
$nextStmt = $node->stmts[$key + 1] ?? null;
103-
if ($this->isSetAccessibleMethodCall($nextStmt)) {
104-
continue;
122+
if ($stmt instanceof Expression) {
123+
// next stmts should be setAccessible() call
124+
$nextStmt = $node->stmts[$key + 1] ?? null;
125+
if ($this->isSetAccessibleMethodCall($nextStmt)) {
126+
continue;
127+
}
128+
129+
array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($variable)]);
130+
} else {
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)