Skip to content

Commit

Permalink
[CodeQuality] Skip nested array on InlineArrayReturnAssignRector (#5680
Browse files Browse the repository at this point in the history
)

* add failing test for InlineArrayReturnAssignRector when using dynamic array definition

* Test auto commit after update github actions (#5676)

* Test auto commit after update github actions

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>

* [CodeQuality] Skip nested array on InlineArrayReturnAssignRector

* [ci-review] Rector Rectify

---------

Co-authored-by: Kévin Grenier <kgrenier@campings.com>
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
3 people committed Mar 3, 2024
1 parent ce70534 commit c591bd2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;

final class SkipNestedArray
{
public function run()
{
$arr = [];
$arr['foo']['bar']['baz'] = ['a' => 1, 'b' => 2];

return $arr;
}
}

Expand Up @@ -44,6 +44,10 @@ public function resolveFromStmtsAndVariable(array $stmts, Variable $variable): a
$assign = $stmtExpr;

$keyExpr = $this->matchKeyOnArrayDimFetchOfVariable($assign, $variable);
if ($keyExpr instanceof ArrayDimFetch) {
return [];
}

$keysAndExprs[] = new KeyAndExpr($keyExpr, $assign->expr, $stmt->getComments());
}

Expand All @@ -63,7 +67,7 @@ private function matchKeyOnArrayDimFetchOfVariable(Assign $assign, Variable $var
}

$arrayDimFetch = $assign->var;
if (! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $variable)) {
if ($arrayDimFetch->var instanceof Variable && ! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $variable)) {
return null;
}

Expand All @@ -76,6 +80,10 @@ private function matchKeyOnArrayDimFetchOfVariable(Assign $assign, Variable $var
return null;
}

if ($arrayDimFetch->var instanceof ArrayDimFetch) {
return $arrayDimFetch->var;
}

return $arrayDimFetch->dim;
}

Expand Down

0 comments on commit c591bd2

Please sign in to comment.