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,29 @@
<?php

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

class OnReturn
{
public function run($object)
{
return new \ReflectionMethod($object, 'bar');
}
}

?>
-----
<?php

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

class OnReturn
{
public function run($object)
{
$reflection = new \ReflectionMethod($object, 'bar');
$reflection->setAccessible(true);
return $reflection;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Naming\Naming\VariableNaming;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -23,6 +27,11 @@
*/
final class DowngradeSetAccessibleReflectionPropertyRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -80,32 +89,54 @@ public function refactor(Node $node): ?Node
$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
if (! $stmt instanceof Expression && ! $stmt instanceof Return_) {
continue;
}

$assign = $stmt->expr;
if (! $assign->expr instanceof New_) {
continue;
if ($stmt instanceof Expression) {
if (! $stmt->expr instanceof Assign) {
continue;
}

$assign = $stmt->expr;
if (! $assign->expr instanceof New_) {
continue;
}

$new = $assign->expr;
$variable = $assign->var;
} else {
if (! $stmt->expr instanceof New_) {
continue;
}

$new = $stmt->expr;
$scope = ScopeFetcher::fetch($stmt);
$variable = new Variable($this->variableNaming->createCountedValueName('reflection', $scope));
}

$new = $assign->expr;
if (! $this->isNames($new->class, ['ReflectionProperty', 'ReflectionMethod'])) {
continue;
}

// next stmts should be setAccessible() call
$nextStmt = $node->stmts[$key + 1] ?? null;
if ($this->isSetAccessibleMethodCall($nextStmt)) {
continue;
if ($stmt instanceof Expression) {
// next stmts should be setAccessible() call
$nextStmt = $node->stmts[$key + 1] ?? null;
if ($this->isSetAccessibleMethodCall($nextStmt)) {
continue;
}

array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($variable)]);
} else {
$previousStmts = [
new Expression(new Assign($variable, $new)),
$this->createSetAccessibleExpression($variable),
];

$stmt->expr = $variable;
array_splice($node->stmts, $key - 2, 0, $previousStmts);
}

array_splice($node->stmts, $key + 1, 0, [$this->createSetAccessibleExpression($assign->var)]);

$hasChanged = true;
}

Expand Down