Skip to content

Commit

Permalink
[Php70] Handle ArrayDimFetch on WrapVariableVariableNameInCurlyBraces…
Browse files Browse the repository at this point in the history
…Rector (#6533)

* [Php70] Handle ArrayDimFetch on WrapVariableVariableNameInCurlyBracesRector

* check only on variable, ensure patch on target
  • Loading branch information
samsonasik authored Dec 8, 2024
1 parent cbc295a commit 7060593
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php70\Rector\Variable\WrapVariableVariableNameInCurlyBracesRector\Fixture;

final class SkipWrappedArrayDimFetchVariableVariables
{
public function run()
{
$baz = 'bat';
$foo = array('bar' => 'baz');

echo ${$foo['bar']};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php70\Rector\Variable\WrapVariableVariableNameInCurlyBracesRector\Fixture;

final class WithArrayDimFetch
{
public function run()
{
$baz = 'bat';
$foo = array('bar' => 'baz');

echo $$foo['bar'];
}
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\Variable\WrapVariableVariableNameInCurlyBracesRector\Fixture;

final class WithArrayDimFetch
{
public function run()
{
$baz = 'bat';
$foo = array('bar' => 'baz');

echo ${$foo['bar']};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Php70\Rector\Variable;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -52,14 +53,29 @@ function run($foo)
*/
public function getNodeTypes(): array
{
return [Variable::class];
return [ArrayDimFetch::class, Variable::class];
}

/**
* @param Variable $node
* @param ArrayDimFetch|Variable $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof ArrayDimFetch) {
if (! $node->var instanceof Variable) {
return null;
}

if (is_string($node->var->name)) {
return null;
}

return new Variable(new ArrayDimFetch(
$node->var->name,
$node->dim
));
}

$nodeName = $node->name;

if (! $nodeName instanceof PropertyFetch && ! $nodeName instanceof Variable) {
Expand Down

0 comments on commit 7060593

Please sign in to comment.