Skip to content

Commit 7741746

Browse files
authored
[DowngradePhp81] Add PHP_VERSION_ID < 80100 check on DowngradeSetAccessibleReflectionPropertyRector (#306)
* [DowngradePhp81] Add PHP_VERSION_ID < 80100 check on DowngradeSetAccessibleReflectionPropertyRector * fix phpstan
1 parent 775b806 commit 7741746

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

rules-tests/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector/Fixture/on_reflection_method.php.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class OnReflectionMethod
2222
public function run($object)
2323
{
2424
$reflectionMethod = new \ReflectionMethod($object, 'bar');
25-
$reflectionMethod->setAccessible(true);
25+
if (PHP_VERSION_ID < 80100) {
26+
$reflectionMethod->setAccessible(true);
27+
}
2628
return $reflectionMethod->__invoke($object, []);
2729
}
2830
}

rules-tests/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector/Fixture/on_return.php.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class OnReturn
2121
public function run($object)
2222
{
2323
$reflection = new \ReflectionMethod($object, 'bar');
24-
$reflection->setAccessible(true);
24+
if (PHP_VERSION_ID < 80100) {
25+
$reflection->setAccessible(true);
26+
}
2527
return $reflection;
2628
}
2729
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector\Fixture;
4+
5+
final class SkipAlreadyIfPhpVersion
6+
{
7+
public function run($object)
8+
{
9+
$reflectionProperty = new \ReflectionProperty($object, 'bar');
10+
if (PHP_VERSION_ID < 80100) {
11+
$reflectionProperty->setAccessible(true);
12+
}
13+
14+
return $reflectionProperty->getValue($object);
15+
}
16+
}

rules-tests/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector/Fixture/some_class.php.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class SomeClass
2323
public function run($object)
2424
{
2525
$reflectionProperty = new \ReflectionProperty($object, 'bar');
26-
$reflectionProperty->setAccessible(true);
26+
if (PHP_VERSION_ID < 80100) {
27+
$reflectionProperty->setAccessible(true);
28+
}
2729

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

rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\Assign;
10+
use PhpParser\Node\Expr\BinaryOp\Smaller;
11+
use PhpParser\Node\Expr\ConstFetch;
1012
use PhpParser\Node\Expr\MethodCall;
1113
use PhpParser\Node\Expr\New_;
1214
use PhpParser\Node\Expr\Variable;
15+
use PhpParser\Node\Name;
16+
use PhpParser\Node\Scalar\Int_;
1317
use PhpParser\Node\Stmt;
1418
use PhpParser\Node\Stmt\Expression;
19+
use PhpParser\Node\Stmt\If_;
1520
use PhpParser\Node\Stmt\Return_;
1621
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1722
use Rector\Naming\Naming\VariableNaming;
@@ -57,7 +62,9 @@ class SomeClass
5762
public function run($object)
5863
{
5964
$reflectionProperty = new ReflectionProperty($object, 'bar');
60-
$reflectionProperty->setAccessible(true);
65+
if (PHP_VERSION_ID < 80100) {
66+
$reflectionProperty->setAccessible(true);
67+
}
6168
6269
return $reflectionProperty->getValue($object);
6370
}
@@ -126,6 +133,10 @@ public function refactor(Node $node): ?Node
126133
continue;
127134
}
128135

136+
if ($this->isSetAccessibleIfMethodCall($nextStmt)) {
137+
continue;
138+
}
139+
129140
array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($variable)]);
130141
} else {
131142
$previousStmts = [
@@ -147,12 +158,17 @@ public function refactor(Node $node): ?Node
147158
return null;
148159
}
149160

150-
private function createSetAccessibleExpression(Expr $expr): Expression
161+
private function createSetAccessibleExpression(Expr $expr): If_
151162
{
152163
$args = [$this->nodeFactory->createArg($this->nodeFactory->createTrue())];
153-
154164
$setAccessibleMethodCall = $this->nodeFactory->createMethodCall($expr, 'setAccessible', $args);
155-
return new Expression($setAccessibleMethodCall);
165+
166+
return new If_(
167+
new Smaller(new ConstFetch(new Name('PHP_VERSION_ID')), new Int_(80100)),
168+
[
169+
'stmts' => [new Expression($setAccessibleMethodCall)],
170+
]
171+
);
156172
}
157173

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

170186
return $this->isName($methodCall->name, 'setAccessible');
171187
}
188+
189+
private function isSetAccessibleIfMethodCall(?Stmt $stmt): bool
190+
{
191+
if (! $stmt instanceof If_) {
192+
return false;
193+
}
194+
195+
if (! $stmt->cond instanceof Smaller) {
196+
return false;
197+
}
198+
199+
if (! $stmt->cond->left instanceof ConstFetch || ! $this->isName($stmt->cond->left->name, 'PHP_VERSION_ID')) {
200+
return false;
201+
}
202+
203+
if (! $stmt->cond->right instanceof Int_ || $stmt->cond->right->value !== 80100) {
204+
return false;
205+
}
206+
207+
if (count($stmt->stmts) !== 1) {
208+
return false;
209+
}
210+
211+
return $this->isSetAccessibleMethodCall($stmt->stmts[0]);
212+
}
172213
}

0 commit comments

Comments
 (0)