Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Php70] Handle ArrayDimFetch on WrapVariableVariableNameInCurlyBracesRector #6533

Merged
merged 2 commits into from
Dec 8, 2024
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,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
Loading