|
5 | 5 | namespace Rector\Php81\Rector\MethodCall; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Expr\Closure; |
9 | 8 | use PhpParser\Node\Expr\MethodCall; |
10 | | -use PhpParser\Node\Stmt\ClassMethod; |
11 | 9 | use PhpParser\Node\Stmt\Expression; |
12 | | -use PhpParser\Node\Stmt\Function_; |
13 | 10 | use PhpParser\NodeVisitor; |
14 | 11 | use Rector\Analyzer\SimpleScope\SimpleScope; |
15 | | -use Rector\Analyzer\SimpleScope\SimpleScopeResolver; |
16 | | -use Rector\Analyzer\SimpleType\ObjectType; |
| 12 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
17 | 13 | use Rector\Rector\AbstractRector; |
18 | 14 | use Rector\ValueObject\PhpVersion; |
19 | 15 | use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
|
23 | 19 | /** |
24 | 20 | * As of PHP 8.1.0, calling `Reflection*::setAccessible()` has no effect. |
25 | 21 | * |
26 | | - * Type resolution is done via the PHPStan-free SimpleScope. |
| 22 | + * Caller type comes from the PHPStan-free SimpleScope attached by SimpleScopeNodeVisitor. |
27 | 23 | * |
28 | 24 | * @see https://www.php.net/manual/en/reflectionmethod.setaccessible.php |
29 | 25 | * @see https://www.php.net/manual/en/reflectionproperty.setaccessible.php |
30 | 26 | * @see \Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\RemoveReflectionSetAccessibleCallsRectorTest |
31 | 27 | */ |
32 | 28 | final class RemoveReflectionSetAccessibleCallsRector extends AbstractRector implements MinPhpVersionInterface |
33 | 29 | { |
34 | | - public function __construct( |
35 | | - private readonly SimpleScopeResolver $simpleScopeResolver |
36 | | - ) { |
37 | | - } |
38 | | - |
39 | 30 | /** |
40 | 31 | * @return array<class-string<Node>> |
41 | 32 | */ |
42 | 33 | public function getNodeTypes(): array |
43 | 34 | { |
44 | | - return [ClassMethod::class, Function_::class, Closure::class]; |
| 35 | + return [Expression::class]; |
45 | 36 | } |
46 | 37 |
|
47 | 38 | /** |
48 | | - * @param ClassMethod|Function_|Closure $node |
| 39 | + * @param Expression $node |
49 | 40 | */ |
50 | | - public function refactor(Node $node): ?Node |
| 41 | + public function refactor(Node $node): ?int |
51 | 42 | { |
52 | | - $stmts = $node->stmts; |
53 | | - if ($stmts === null) { |
| 43 | + if (! $node->expr instanceof MethodCall) { |
54 | 44 | return null; |
55 | 45 | } |
56 | 46 |
|
57 | | - $simpleScope = $this->simpleScopeResolver->resolve([$node]); |
58 | | - |
59 | | - $hasChanged = false; |
60 | | - $this->traverseNodesWithCallable($node, function (Node $subNode) use ($simpleScope, &$hasChanged): ?int { |
61 | | - if (! $this->isReflectionSetAccessibleCall($subNode, $simpleScope)) { |
62 | | - return null; |
63 | | - } |
| 47 | + $methodCall = $node->expr; |
| 48 | + if (! $this->isName($methodCall->name, 'setAccessible')) { |
| 49 | + return null; |
| 50 | + } |
64 | 51 |
|
65 | | - $hasChanged = true; |
66 | | - return NodeVisitor::REMOVE_NODE; |
67 | | - }); |
| 52 | + $simpleScope = $node->getAttribute(AttributeKey::SIMPLE_SCOPE); |
| 53 | + if (! $simpleScope instanceof SimpleScope) { |
| 54 | + return null; |
| 55 | + } |
68 | 56 |
|
69 | | - if (! $hasChanged) { |
| 57 | + if (! $simpleScope->isObjectType($methodCall->var, 'ReflectionProperty', 'ReflectionMethod')) { |
70 | 58 | return null; |
71 | 59 | } |
72 | 60 |
|
73 | | - return $node; |
| 61 | + return NodeVisitor::REMOVE_NODE; |
74 | 62 | } |
75 | 63 |
|
76 | 64 | public function getRuleDefinition(): RuleDefinition |
@@ -102,27 +90,4 @@ public function provideMinPhpVersion(): int |
102 | 90 | { |
103 | 91 | return PhpVersion::PHP_81; |
104 | 92 | } |
105 | | - |
106 | | - private function isReflectionSetAccessibleCall(Node $node, SimpleScope $simpleScope): bool |
107 | | - { |
108 | | - if (! $node instanceof Expression) { |
109 | | - return false; |
110 | | - } |
111 | | - |
112 | | - if (! $node->expr instanceof MethodCall) { |
113 | | - return false; |
114 | | - } |
115 | | - |
116 | | - $methodCall = $node->expr; |
117 | | - if (! $this->isName($methodCall->name, 'setAccessible')) { |
118 | | - return false; |
119 | | - } |
120 | | - |
121 | | - $simpleType = $simpleScope->getType($methodCall->var); |
122 | | - if (! $simpleType instanceof ObjectType) { |
123 | | - return false; |
124 | | - } |
125 | | - |
126 | | - return $simpleType->isInstanceOf('ReflectionProperty', 'ReflectionMethod'); |
127 | | - } |
128 | 93 | } |
0 commit comments