-
-
Notifications
You must be signed in to change notification settings - Fork 431
[Php81] added RemoveReflectionSetAccessibleCallsRector
#7085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samsonasik
merged 3 commits into
rectorphp:main
from
NickSdot:feat/remove-setAccessible-rector
Jul 23, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector/Fixture/fixture.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\Fixture; | ||
|
|
||
| use ReflectionMethod; | ||
| use ReflectionProperty; | ||
|
|
||
| final class Fixture | ||
| { | ||
| public function run(): void | ||
| { | ||
| $reflectionProperty = new ReflectionProperty($this, 'privateProperty'); | ||
| $reflectionProperty->setAccessible(true); | ||
| $value = $reflectionProperty->getValue($this); | ||
|
|
||
| $reflectionMethod = new ReflectionMethod($this, 'privateMethod'); | ||
| $reflectionMethod->setAccessible(false); | ||
| $reflectionMethod->invoke($this); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\Fixture; | ||
|
|
||
| use ReflectionMethod; | ||
| use ReflectionProperty; | ||
|
|
||
| final class Fixture | ||
| { | ||
| public function run(): void | ||
| { | ||
| $reflectionProperty = new ReflectionProperty($this, 'privateProperty'); | ||
| $value = $reflectionProperty->getValue($this); | ||
|
|
||
| $reflectionMethod = new ReflectionMethod($this, 'privateMethod'); | ||
| $reflectionMethod->invoke($this); | ||
| } | ||
| } | ||
|
|
||
| ?> |
31 changes: 31 additions & 0 deletions
31
...RemoveReflectionSetAccessibleCallsRector/RemoveReflectionSetAccessibleCallsRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class RemoveReflectionSetAccessibleCallsRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function testRule(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| /** | ||
| * @return Iterator<array<string>> | ||
| */ | ||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...p81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector; | ||
| use Rector\ValueObject\PhpVersion; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(RemoveReflectionSetAccessibleCallsRector::class); | ||
|
|
||
| $rectorConfig->phpVersion(PhpVersion::PHP_85); | ||
| }; |
88 changes: 88 additions & 0 deletions
88
rules/Php81/Rector/MethodCall/RemoveReflectionSetAccessibleCallsRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the downgrade rule seems not handling on
returnyet, onlyExpression, I will look into it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note:
DowngradeSetAccessibleReflectionPropertyRectorhas "Property" in the name. Your last PR rectorphp/rector-downgrade-php#296 added handling for methods. Just pointing it out in case that's relevant.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that
isNames()for :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created PR to handle on direct return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just meant that the rule name now no longer exactly fits to what the rule does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that can be refactored next (if needed :) ), but we need to ensure downgrade working first :)