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
Expand Up @@ -22,7 +22,9 @@ class OnReflectionMethod
public function run($object)
{
$reflectionMethod = new \ReflectionMethod($object, 'bar');
$reflectionMethod->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$reflectionMethod->setAccessible(true);
}
return $reflectionMethod->__invoke($object, []);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class OnReturn
public function run($object)
{
$reflection = new \ReflectionMethod($object, 'bar');
$reflection->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$reflection->setAccessible(true);
}
return $reflection;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

final class SkipAlreadyIfPhpVersion
{
public function run($object)
{
$reflectionProperty = new \ReflectionProperty($object, 'bar');
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}

return $reflectionProperty->getValue($object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class SomeClass
public function run($object)
{
$reflectionProperty = new \ReflectionProperty($object, 'bar');
$reflectionProperty->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}

return $reflectionProperty->getValue($object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Naming\Naming\VariableNaming;
Expand Down Expand Up @@ -57,7 +62,9 @@ class SomeClass
public function run($object)
{
$reflectionProperty = new ReflectionProperty($object, 'bar');
$reflectionProperty->setAccessible(true);
if (PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}

return $reflectionProperty->getValue($object);
}
Expand Down Expand Up @@ -126,6 +133,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->isSetAccessibleIfMethodCall($nextStmt)) {
continue;
}

array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($variable)]);
} else {
$previousStmts = [
Expand All @@ -147,12 +158,17 @@ public function refactor(Node $node): ?Node
return null;
}

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

$setAccessibleMethodCall = $this->nodeFactory->createMethodCall($expr, 'setAccessible', $args);
return new Expression($setAccessibleMethodCall);

return new If_(
new Smaller(new ConstFetch(new Name('PHP_VERSION_ID')), new Int_(80100)),
[
'stmts' => [new Expression($setAccessibleMethodCall)],
]
);
}

private function isSetAccessibleMethodCall(?Stmt $stmt): bool
Expand All @@ -169,4 +185,29 @@ private function isSetAccessibleMethodCall(?Stmt $stmt): bool

return $this->isName($methodCall->name, 'setAccessible');
}

private function isSetAccessibleIfMethodCall(?Stmt $stmt): bool
{
if (! $stmt instanceof If_) {
return false;
}

if (! $stmt->cond instanceof Smaller) {
return false;
}

if (! $stmt->cond->left instanceof ConstFetch || ! $this->isName($stmt->cond->left->name, 'PHP_VERSION_ID')) {
return false;
}

if (! $stmt->cond->right instanceof Int_ || $stmt->cond->right->value !== 80100) {
return false;
}

if (count($stmt->stmts) !== 1) {
return false;
}

return $this->isSetAccessibleMethodCall($stmt->stmts[0]);
}
}