forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveReflectionSetAccessibleCallsRector.php
More file actions
88 lines (75 loc) · 2.57 KB
/
RemoveReflectionSetAccessibleCallsRector.php
File metadata and controls
88 lines (75 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace Rector\Php81\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeVisitor;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* As of PHP 8.1.0, calling `Reflection*::setAccessible()` has no effect.
*
* @see https://www.php.net/manual/en/reflectionmethod.setaccessible.php
* @see https://www.php.net/manual/en/reflectionproperty.setaccessible.php
* @see \Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\RemoveReflectionSetAccessibleCallsRectorTest
*/
final class RemoveReflectionSetAccessibleCallsRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
*/
public function refactor(Node $node): ?int
{
if ($node->expr instanceof MethodCall === false) {
return null;
}
$methodCall = $node->expr;
if ($this->isName($methodCall->name, 'setAccessible') === false) {
return null;
}
if ($this->isObjectType($methodCall->var, new ObjectType('ReflectionProperty'))
|| $this->isObjectType($methodCall->var, new ObjectType('ReflectionMethod'))
) {
return NodeVisitor::REMOVE_NODE;
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove Reflection::setAccessible() calls', [
new CodeSample(
<<<'CODE_SAMPLE'
$reflectionProperty = new ReflectionProperty($object, 'property');
$reflectionProperty->setAccessible(true);
$value = $reflectionProperty->getValue($object);
$reflectionMethod = new ReflectionMethod($object, 'method');
$reflectionMethod->setAccessible(false);
$reflectionMethod->invoke($object);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$reflectionProperty = new ReflectionProperty($object, 'property');
$value = $reflectionProperty->getValue($object);
$reflectionMethod = new ReflectionMethod($object, 'method');
$reflectionMethod->invoke($object);
CODE_SAMPLE
),
]);
}
public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_81;
}
}