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