diff --git a/rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/skip_wrapped_array_dim_fetch_variable_variables.php.inc b/rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/skip_wrapped_array_dim_fetch_variable_variables.php.inc new file mode 100644 index 00000000000..ff8f26e4442 --- /dev/null +++ b/rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/skip_wrapped_array_dim_fetch_variable_variables.php.inc @@ -0,0 +1,14 @@ + 'baz'); + + echo ${$foo['bar']}; + } +} diff --git a/rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/with_array_dim_fetch.php.inc b/rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/with_array_dim_fetch.php.inc new file mode 100644 index 00000000000..96228bf97d7 --- /dev/null +++ b/rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/with_array_dim_fetch.php.inc @@ -0,0 +1,33 @@ + 'baz'); + + echo $$foo['bar']; + } +} + +?> +----- + 'baz'); + + echo ${$foo['bar']}; + } +} + +?> diff --git a/rules/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector.php b/rules/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector.php index db673c7950c..4a30121f78f 100644 --- a/rules/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector.php +++ b/rules/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector.php @@ -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; @@ -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) {