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