Skip to content

Commit 2d84438

Browse files
committed
feat: added RemoveDeprecatedReflectionSetAccessibleCallsRector
1 parent eb07bff commit 2d84438

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
*
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\Php85\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

Comments
 (0)