From 70605936b150b2b589ef4fb3c1275667ff690b26 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 8 Dec 2024 10:06:48 +0700 Subject: [PATCH] [Php70] Handle ArrayDimFetch on WrapVariableVariableNameInCurlyBracesRector (#6533) * [Php70] Handle ArrayDimFetch on WrapVariableVariableNameInCurlyBracesRector * check only on variable, ensure patch on target --- ...array_dim_fetch_variable_variables.php.inc | 14 ++++++++ .../Fixture/with_array_dim_fetch.php.inc | 33 +++++++++++++++++++ ...ariableVariableNameInCurlyBracesRector.php | 20 +++++++++-- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/skip_wrapped_array_dim_fetch_variable_variables.php.inc create mode 100644 rules-tests/Php70/Rector/Variable/WrapVariableVariableNameInCurlyBracesRector/Fixture/with_array_dim_fetch.php.inc 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) {