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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;

class OnGetMethods
{
public function run($object)
{
$reflectionClass = new \ReflectionClass($object);
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
$reflectionMethod->invoke($object);
}
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;

class OnGetMethods
{
public function run($object)
{
$reflectionClass = new \ReflectionClass($object);
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
if (PHP_VERSION_ID < 80100) {
$reflectionMethod->setAccessible(true);
}
$reflectionMethod->invoke($object);
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;

class OnGetProperties
{
public function run($object)
{
$reflectionObject = new \ReflectionObject($object);
foreach ($reflectionObject->getProperties() as $reflectionProperty) {
echo $reflectionProperty->getValue($object);
}
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;

class OnGetProperties
{
public function run($object)
{
$reflectionObject = new \ReflectionObject($object);
foreach ($reflectionObject->getProperties() as $reflectionProperty) {
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}
echo $reflectionProperty->getValue($object);
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;

class SkipAlreadyAccessibleInForeach
{
public function run($object)
{
$reflectionObject = new \ReflectionObject($object);
foreach ($reflectionObject->getProperties() as $reflectionProperty) {
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}
echo $reflectionProperty->getValue($object);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;

class SomeCollection
{
public function getProperties(): array
{
return [];
}
}

class SkipNonReflectionGetProperties
{
public function run()
{
$collection = new SomeCollection();
foreach ($collection->getProperties() as $property) {
echo $property;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\Naming\Naming\VariableNaming;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PHPStan\ScopeFetcher;
Expand Down Expand Up @@ -69,6 +71,38 @@ public function run($object)
return $reflectionProperty->getValue($object);
}
}
CODE_SAMPLE
),

new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($object)
{
$reflectionObject = new ReflectionObject($object);
foreach ($reflectionObject->getProperties() as $reflectionProperty) {
echo $reflectionProperty->getValue($object);
}
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($object)
{
$reflectionObject = new ReflectionObject($object);
foreach ($reflectionObject->getProperties() as $reflectionProperty) {
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}
echo $reflectionProperty->getValue($object);
}
}
}
CODE_SAMPLE
),

Expand Down Expand Up @@ -96,6 +130,14 @@ public function refactor(Node $node): ?Node
$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof Foreach_) {
if ($this->refactorForeach($stmt)) {
$hasChanged = true;
}

continue;
}

if (! $stmt instanceof Expression && ! $stmt instanceof Return_) {
continue;
}
Expand Down Expand Up @@ -158,6 +200,45 @@ public function refactor(Node $node): ?Node
return null;
}

private function refactorForeach(Foreach_ $foreach): bool
{
if (! $foreach->valueVar instanceof Variable) {
return false;
}

if (! $this->isReflectionMembersCall($foreach->expr)) {
return false;
}

$firstStmt = $foreach->stmts[0] ?? null;
if ($this->isSetAccessibleMethodCall($firstStmt) || $this->isSetAccessibleIfMethodCall($firstStmt)) {
return false;
}

array_unshift($foreach->stmts, $this->createSetAccessibleExpression($foreach->valueVar));

return true;
}

private function isReflectionMembersCall(Expr $expr): bool
{
if (! $expr instanceof MethodCall) {
return false;
}

if (! $this->isNames($expr->name, ['getProperties', 'getMethods'])) {
return false;
}

$callerType = $this->nodeTypeResolver->getType($expr->var);
if (! $callerType instanceof ObjectType) {
return false;
}

return $callerType->isInstanceOf('ReflectionClass')
->yes();
}

private function createSetAccessibleExpression(Expr $expr): If_
{
$args = [$this->nodeFactory->createArg($this->nodeFactory->createTrue())];
Expand Down
Loading