Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector;
use Rector\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector;
use Rector\Php81\Rector\MethodCall\SpatieEnumMethodCallToEnumConstRector;
use Rector\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
Expand All @@ -24,5 +25,6 @@
SpatieEnumMethodCallToEnumConstRector::class,
NullToStrictStringFuncCallArgRector::class,
FirstClassCallableRector::class,
RemoveReflectionSetAccessibleCallsRector::class,
]);
};
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);
}
}

?>
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';
}
}
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);
};
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;
}
}
7 changes: 1 addition & 6 deletions src/Util/Reflection/PrivatesAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,20 @@ public function callPrivateMethod(object|string $object, string $methodName, arr
public function getPrivateProperty(object $object, string $propertyName): mixed
{
$reflectionProperty = $this->resolvePropertyReflection($object, $propertyName);
$reflectionProperty->setAccessible(true);

return $reflectionProperty->getValue($object);
}

public function setPrivateProperty(object $object, string $propertyName, mixed $value): void
{
$reflectionProperty = $this->resolvePropertyReflection($object, $propertyName);
$reflectionProperty->setAccessible(true);

$reflectionProperty->setValue($object, $value);
}

private function createAccessibleMethodReflection(object $object, string $methodName): ReflectionMethod
{
$reflectionMethod = new ReflectionMethod($object, $methodName);
$reflectionMethod->setAccessible(true);

return $reflectionMethod;
return new ReflectionMethod($object, $methodName);
Copy link
Member

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 return yet, only Expression, I will look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: DowngradeSetAccessibleReflectionPropertyRector has "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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that isNames() for :)

if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Copy link
Member

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 :)

}

private function resolvePropertyReflection(object $object, string $propertyName): ReflectionProperty
Expand Down