diff --git a/src/Rector/ArrayDimFetch/SessionVariableToSessionFacadeRector.php b/src/Rector/ArrayDimFetch/SessionVariableToSessionFacadeRector.php index 3a090819..a7ec69ac 100644 --- a/src/Rector/ArrayDimFetch/SessionVariableToSessionFacadeRector.php +++ b/src/Rector/ArrayDimFetch/SessionVariableToSessionFacadeRector.php @@ -2,6 +2,7 @@ namespace RectorLaravel\Rector\ArrayDimFetch; +use Override; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -13,7 +14,6 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Unset_; -use PhpParser\NodeVisitor; use RectorLaravel\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -23,6 +23,8 @@ */ class SessionVariableToSessionFacadeRector extends AbstractRector { + private const string IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR = 'is_inside_array_dim_fetch_with_dim_not_expr'; + public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -54,6 +56,37 @@ public function getRuleDefinition(): RuleDefinition ); } + #[Override] + public function beforeTraverse(array $nodes): array + { + parent::beforeTraverse($nodes); + + $this->traverseNodesWithCallable($nodes, function (Node $node) { + if (! $node instanceof ArrayDimFetch) { + return null; + } + + if (! $node->dim instanceof Expr) { + $node->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true); + $this->traverseNodesWithCallable($node, function (Node $subNode) { + if (! $subNode instanceof Variable) { + return null; + } + + $subNode->setAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR, true); + + return $subNode; + }); + + return $node; + } + + return null; + }); + + return $nodes; + } + public function getNodeTypes(): array { return [ @@ -68,9 +101,8 @@ public function getNodeTypes(): array /** * @param ArrayDimFetch|Assign|FuncCall|Isset_|Unset_|Variable $node - * @return StaticCall|Expression|1|null */ - public function refactor(Node $node): StaticCall|Expression|int|null + public function refactor(Node $node): StaticCall|Expression|null { if ($node instanceof ArrayDimFetch) { return $this->processDimFetch($node); @@ -100,17 +132,14 @@ public function refactor(Node $node): StaticCall|Expression|int|null return $this->processAssign($node); } - /** - * @return StaticCall|1|null - */ - public function processDimFetch(ArrayDimFetch $arrayDimFetch): StaticCall|int|null + public function processDimFetch(ArrayDimFetch $arrayDimFetch): ?StaticCall { if (! $this->isName($arrayDimFetch->var, '_SESSION')) { return null; } if (! $arrayDimFetch->dim instanceof Expr) { - return NodeVisitor::DONT_TRAVERSE_CHILDREN; + return null; } return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'get', [ @@ -163,10 +192,7 @@ private function processFunction(FuncCall $funcCall): ?StaticCall return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', $replacementMethod); } - /** - * @return StaticCall|1|null - */ - private function processIsset(Isset_ $isset): StaticCall|int|null + private function processIsset(Isset_ $isset): ?StaticCall { if (count($isset->vars) < 1) { return null; @@ -183,7 +209,7 @@ private function processIsset(Isset_ $isset): StaticCall|int|null } if (! $var->dim instanceof Expr) { - return NodeVisitor::DONT_TRAVERSE_CHILDREN; + return null; } return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'has', [ @@ -191,10 +217,7 @@ private function processIsset(Isset_ $isset): StaticCall|int|null ]); } - /** - * @return StaticCall|1|null - */ - private function processUnset(Unset_ $unset): StaticCall|int|null + private function processUnset(Unset_ $unset): ?StaticCall { if (count($unset->vars) < 1) { return null; @@ -211,7 +234,7 @@ private function processUnset(Unset_ $unset): StaticCall|int|null } if (! $var->dim instanceof Expr) { - return NodeVisitor::DONT_TRAVERSE_CHILDREN; + return null; } return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'forget', [ @@ -221,6 +244,10 @@ private function processUnset(Unset_ $unset): StaticCall|int|null private function processVariable(Variable $variable): ?StaticCall { + if ($variable->getAttribute(self::IS_INSIDE_ARRAY_DIM_FETCH_WITH_DIM_NOT_EXPR) === true) { + return null; + } + if (! $this->isName($variable, '_SESSION')) { return null; }