Skip to content

Commit 0927a46

Browse files
committed
[Analyzer] Attach SimpleScope via decorating node visitor; rules read it at Expression level
- SimpleScopeNodeVisitor (DecoratingNodeVisitorInterface) resolves a SimpleScope per function-like and attaches it to inner statements via AttributeKey::SIMPLE_SCOPE - RemoveReflectionSetAccessibleCallsRector subscribes to Expression again and reads the attached scope (no in-rule attribute writing, per rector-rules) - SimpleScope::isObjectType(Expr, string ...) folds getType + instanceof + isInstanceOf - add fixture covering setAccessible() nested in an if () {} block
1 parent 9f65b28 commit 0927a46

7 files changed

Lines changed: 122 additions & 58 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\Fixture;
4+
5+
use ReflectionProperty;
6+
7+
final class InsideIfCondition
8+
{
9+
public function run(bool $flag): void
10+
{
11+
$reflectionProperty = new ReflectionProperty('Foo', 'bar');
12+
13+
if ($flag) {
14+
$reflectionProperty->setAccessible(true);
15+
$reflectionProperty->getValue($this);
16+
}
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\Fixture;
25+
26+
use ReflectionProperty;
27+
28+
final class InsideIfCondition
29+
{
30+
public function run(bool $flag): void
31+
{
32+
$reflectionProperty = new ReflectionProperty('Foo', 'bar');
33+
34+
if ($flag) {
35+
$reflectionProperty->getValue($this);
36+
}
37+
}
38+
}
39+
40+
?>

‎rules/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector.php‎

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
namespace Rector\Php81\Rector\MethodCall;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr\Closure;
98
use PhpParser\Node\Expr\MethodCall;
10-
use PhpParser\Node\Stmt\ClassMethod;
119
use PhpParser\Node\Stmt\Expression;
12-
use PhpParser\Node\Stmt\Function_;
1310
use PhpParser\NodeVisitor;
1411
use Rector\Analyzer\SimpleScope\SimpleScope;
15-
use Rector\Analyzer\SimpleScope\SimpleScopeResolver;
16-
use Rector\Analyzer\SimpleType\ObjectType;
12+
use Rector\NodeTypeResolver\Node\AttributeKey;
1713
use Rector\Rector\AbstractRector;
1814
use Rector\ValueObject\PhpVersion;
1915
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -23,54 +19,46 @@
2319
/**
2420
* As of PHP 8.1.0, calling `Reflection*::setAccessible()` has no effect.
2521
*
26-
* Type resolution is done via the PHPStan-free SimpleScope.
22+
* Caller type comes from the PHPStan-free SimpleScope attached by SimpleScopeNodeVisitor.
2723
*
2824
* @see https://www.php.net/manual/en/reflectionmethod.setaccessible.php
2925
* @see https://www.php.net/manual/en/reflectionproperty.setaccessible.php
3026
* @see \Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\RemoveReflectionSetAccessibleCallsRectorTest
3127
*/
3228
final class RemoveReflectionSetAccessibleCallsRector extends AbstractRector implements MinPhpVersionInterface
3329
{
34-
public function __construct(
35-
private readonly SimpleScopeResolver $simpleScopeResolver
36-
) {
37-
}
38-
3930
/**
4031
* @return array<class-string<Node>>
4132
*/
4233
public function getNodeTypes(): array
4334
{
44-
return [ClassMethod::class, Function_::class, Closure::class];
35+
return [Expression::class];
4536
}
4637

4738
/**
48-
* @param ClassMethod|Function_|Closure $node
39+
* @param Expression $node
4940
*/
50-
public function refactor(Node $node): ?Node
41+
public function refactor(Node $node): ?int
5142
{
52-
$stmts = $node->stmts;
53-
if ($stmts === null) {
43+
if (! $node->expr instanceof MethodCall) {
5444
return null;
5545
}
5646

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+
}
6451

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+
}
6856

69-
if (! $hasChanged) {
57+
if (! $simpleScope->isObjectType($methodCall->var, 'ReflectionProperty', 'ReflectionMethod')) {
7058
return null;
7159
}
7260

73-
return $node;
61+
return NodeVisitor::REMOVE_NODE;
7462
}
7563

7664
public function getRuleDefinition(): RuleDefinition
@@ -102,27 +90,4 @@ public function provideMinPhpVersion(): int
10290
{
10391
return PhpVersion::PHP_81;
10492
}
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-
}
12893
}

‎rules/Simple/Rector/ClassMethod/DemoObjectMethodCallRenameRector.php‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Stmt\ClassMethod;
1111
use Rector\Analyzer\SimpleScope\SimpleScope;
1212
use Rector\Analyzer\SimpleScope\SimpleScopeResolver;
13-
use Rector\Analyzer\SimpleType\ObjectType;
1413
use Rector\Rector\AbstractRector;
1514
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1615
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -99,12 +98,7 @@ private function refactorMethodCall(Node $node, SimpleScope $simpleScope): bool
9998
return false;
10099
}
101100

102-
$simpleType = $simpleScope->getType($node->var);
103-
if (! $simpleType instanceof ObjectType) {
104-
return false;
105-
}
106-
107-
if (! $simpleType->isInstanceOf(self::TARGET_CLASS)) {
101+
if (! $simpleScope->isObjectType($node->var, self::TARGET_CLASS)) {
108102
return false;
109103
}
110104

‎src/Analyzer/SimpleScope/SimpleScope.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public function setVariableType(string $name, SimpleTypeInterface $simpleType):
3434
$this->variableTypes[$name] = $simpleType;
3535
}
3636

37+
public function isObjectType(Expr $expr, string ...$classNames): bool
38+
{
39+
$simpleType = $this->getType($expr);
40+
if (! $simpleType instanceof ObjectType) {
41+
return false;
42+
}
43+
44+
return $simpleType->isInstanceOf(...$classNames);
45+
}
46+
3747
public function getType(Expr $expr): SimpleTypeInterface
3848
{
3949
if ($expr instanceof String_) {

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
use Rector\PhpParser\NodeVisitor\LocalVariableScopeNodeVisitor;
6464
use Rector\PhpParser\NodeVisitor\NameAndArgNodeVisitor;
6565
use Rector\PhpParser\NodeVisitor\PhpVersionConditionNodeVisitor;
66+
use Rector\PhpParser\NodeVisitor\SimpleScopeNodeVisitor;
6667
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
6768
use Rector\PHPStanStaticTypeMapper\TypeMapper\ArrayTypeMapper;
6869
use Rector\PHPStanStaticTypeMapper\TypeMapper\ConditionalTypeForParameterMapper;
@@ -101,6 +102,7 @@ final class LazyContainerFactory
101102
LocalVariableScopeNodeVisitor::class,
102103
NameAndArgNodeVisitor::class,
103104
DefaultValueNodeVisitor::class,
105+
SimpleScopeNodeVisitor::class,
104106
];
105107

106108
/**

‎src/NodeTypeResolver/Node/AttributeKey.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ final class AttributeKey
2121
*/
2222
public const string SCOPE = 'scope';
2323

24+
/**
25+
* PHPStan-free scope attached by SimpleScopeNodeVisitor
26+
*/
27+
public const string SIMPLE_SCOPE = 'simpleScope';
28+
2429
/**
2530
* Internal php-parser name.
2631
* Do not change this even if you want!
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PhpParser\NodeVisitor;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\FunctionLike;
9+
use PhpParser\NodeVisitorAbstract;
10+
use Rector\Analyzer\SimpleScope\SimpleScopeResolver;
11+
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
12+
use Rector\NodeTypeResolver\Node\AttributeKey;
13+
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
14+
15+
/**
16+
* Resolves a PHPStan-free SimpleScope per function-like and attaches it to the
17+
* statements inside, so rules can read the caller type without walking the tree.
18+
*/
19+
final class SimpleScopeNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
20+
{
21+
public function __construct(
22+
private readonly SimpleScopeResolver $simpleScopeResolver,
23+
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
24+
) {
25+
}
26+
27+
public function enterNode(Node $node): ?Node
28+
{
29+
if (! $node instanceof FunctionLike) {
30+
return null;
31+
}
32+
33+
$stmts = $node->getStmts();
34+
if ($stmts === null) {
35+
return null;
36+
}
37+
38+
$simpleScope = $this->simpleScopeResolver->resolve([$node]);
39+
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, static function (Node $subNode) use (
40+
$simpleScope
41+
): null {
42+
$subNode->setAttribute(AttributeKey::SIMPLE_SCOPE, $simpleScope);
43+
return null;
44+
});
45+
46+
return null;
47+
}
48+
}

0 commit comments

Comments
 (0)