Skip to content

Commit 4087338

Browse files
committed
feat: added RemoveDeprecatedReflectionSetAccessibleCallsRector
1 parent eb07bff commit 4087338

File tree

5 files changed

+180
-1
lines changed

5 files changed

+180
-1
lines changed

config/set/php85.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
7+
use Rector\Php85\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector;
78

89
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->rules([ArrayFirstLastRector::class]);
10+
$rectorConfig->rules(
11+
[
12+
ArrayFirstLastRector::class,
13+
RemoveReflectionSetAccessibleCallsRector::class,
14+
]
15+
);
1016
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php85\Rector\MethodCall\RemoveDeprecatedReflectionSetAccessibleCalls\Fixture;
4+
5+
use ReflectionMethod;
6+
use ReflectionProperty;
7+
8+
final class Fixture
9+
{
10+
public function run(): void
11+
{
12+
$reflectionProperty = new ReflectionProperty($this, 'privateProperty');
13+
$reflectionProperty->setAccessible(true);
14+
$value = $reflectionProperty->getValue($this);
15+
16+
$reflectionMethod = new ReflectionMethod($this, 'privateMethod');
17+
$reflectionMethod->setAccessible(false);
18+
$reflectionMethod->invoke($this);
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\Tests\Php85\Rector\MethodCall\RemoveDeprecatedReflectionSetAccessibleCalls\Fixture;
27+
28+
use ReflectionMethod;
29+
use ReflectionProperty;
30+
31+
final class Fixture
32+
{
33+
public function run(): void
34+
{
35+
$reflectionProperty = new ReflectionProperty($this, 'privateProperty');
36+
$value = $reflectionProperty->getValue($this);
37+
38+
$reflectionMethod = new ReflectionMethod($this, 'privateMethod');
39+
$reflectionMethod->invoke($this);
40+
}
41+
}
42+
43+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
9+
10+
final class RemoveReflectionSetAccessibleCallsRectorTest extends AbstractRectorTestCase
11+
{
12+
#[DataProvider('provideData')]
13+
public function testRule(string $filePath): void
14+
{
15+
$this->doTestFile($filePath);
16+
}
17+
18+
/**
19+
* @return \Iterator<array<string>>
20+
*/
21+
public static function provideData(): \Iterator
22+
{
23+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
24+
}
25+
26+
public function provideConfigFilePath(): string
27+
{
28+
return __DIR__ . '/config/configured_rule.php';
29+
}
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php85\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rule(RemoveReflectionSetAccessibleCallsRector::class);
11+
12+
$rectorConfig->phpVersion(PhpVersion::PHP_85);
13+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Php85\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+
* @see https://www.php.net/manual/en/reflectionmethod.setaccessible.php
21+
* @see https://www.php.net/manual/en/reflectionproperty.setaccessible.php
22+
* @see \Rector\Tests\Php85\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector\RemoveReflectionSetAccessibleCallsTest
23+
*/
24+
final class RemoveReflectionSetAccessibleCallsRector extends AbstractRector implements MinPhpVersionInterface
25+
{
26+
/**
27+
* @return array<class-string<Node>>
28+
*/
29+
public function getNodeTypes(): array
30+
{
31+
return [Expression::class];
32+
}
33+
34+
/**
35+
* @param Expression $node
36+
*/
37+
public function refactor(Node $node): ?int
38+
{
39+
if ($node->expr instanceof MethodCall === false) {
40+
return null;
41+
}
42+
43+
$methodCall = $node->expr;
44+
45+
if ($this->isName($methodCall->name, 'setAccessible') === false) {
46+
return null;
47+
}
48+
49+
if ($this->isObjectType($methodCall->var, new ObjectType('ReflectionProperty')) === true
50+
|| $this->isObjectType($methodCall->var, new ObjectType('ReflectionMethod')) === true
51+
) {
52+
return NodeVisitor::REMOVE_NODE;
53+
}
54+
55+
return null;
56+
}
57+
58+
public function getRuleDefinition(): RuleDefinition
59+
{
60+
return new RuleDefinition('Remove Reflection::setAccessible() calls', [
61+
new CodeSample(
62+
<<<'CODE_SAMPLE'
63+
$reflectionProperty = new ReflectionProperty($object, 'property');
64+
$reflectionProperty->setAccessible(true);
65+
$value = $reflectionProperty->getValue($object);
66+
67+
$reflectionMethod = new ReflectionMethod($object, 'method');
68+
$reflectionMethod->setAccessible(false);
69+
$reflectionMethod->invoke($object);
70+
CODE_SAMPLE
71+
,
72+
<<<'CODE_SAMPLE'
73+
$reflectionProperty = new ReflectionProperty($object, 'property');
74+
$value = $reflectionProperty->getValue($object);
75+
76+
$reflectionMethod = new ReflectionMethod($object, 'method');
77+
$reflectionMethod->invoke($object);
78+
CODE_SAMPLE
79+
),
80+
]);
81+
}
82+
83+
public function provideMinPhpVersion(): int
84+
{
85+
return PhpVersion::PHP_85;
86+
}
87+
}

0 commit comments

Comments
 (0)