Skip to content

Commit edadebb

Browse files
Skip nested array dim fetch assigns in InlineArrayReturnAssignRector (#7380)
* Fix nested keys assign in InlineArrayReturnAssignRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 646a732 commit edadebb

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class SkipNestedDimFetch
6+
{
7+
public static function getData(): array
8+
{
9+
$data = [];
10+
11+
$data['info'] = [
12+
'one' => 123,
13+
];
14+
15+
$data['info']['nested'] = 123;
16+
17+
return $data;
18+
}
19+
}
20+
21+
?>

rules-tests/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector/Fixture/with_unary.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ final class WithUnary
2727
}
2828
}
2929

30-
?>
30+
?>

rules/CodeQuality/NodeAnalyzer/VariableDimFetchAssignResolver.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
namespace Rector\CodeQuality\NodeAnalyzer;
66

77
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\Array_;
89
use PhpParser\Node\Expr\ArrayDimFetch;
910
use PhpParser\Node\Expr\Assign;
1011
use PhpParser\Node\Stmt;
1112
use PhpParser\Node\Stmt\Expression;
1213
use PhpParser\Node\Stmt\Return_;
1314
use Rector\CodeQuality\ValueObject\KeyAndExpr;
15+
use Rector\Exception\NotImplementedYetException;
1416
use Rector\NodeAnalyzer\ExprAnalyzer;
1517
use Rector\PhpParser\Node\Value\ValueResolver;
1618

@@ -86,7 +88,11 @@ private function setNestedKeysExpr(array &$exprsByKeys, array $keys, Expr $expr)
8688
$keys = array_reverse($keys);
8789

8890
foreach ($keys as $key) {
89-
// create intermediate arrays automatically
91+
if ($reference instanceof Array_) {
92+
// currently it fails here with Cannot use object of type PhpParser\Node\Expr\Array_ as array
93+
throw new NotImplementedYetException();
94+
}
95+
9096
$reference = &$reference[$key];
9197
}
9298

rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpParser\Node\Stmt\Return_;
1717
use Rector\CodeQuality\NodeAnalyzer\VariableDimFetchAssignResolver;
1818
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
19+
use Rector\Exception\NotImplementedYetException;
1920
use Rector\Rector\AbstractRector;
2021
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2122
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -111,12 +112,18 @@ public function refactor(Node $node): ?Node
111112
return null;
112113
}
113114

114-
$keysAndExprsByKey = $this->variableDimFetchAssignResolver->resolveFromStmtsAndVariable(
115-
$stmts,
116-
$emptyArrayAssign,
117-
);
115+
try {
116+
$keysAndExprsByKey = $this->variableDimFetchAssignResolver->resolveFromStmtsAndVariable(
117+
$stmts,
118+
$emptyArrayAssign,
119+
);
120+
} catch (NotImplementedYetException) {
121+
// dim fetch assign of nested arrays is hard to resolve
122+
return null;
123+
}
118124

119125
if ($keysAndExprsByKey === []) {
126+
120127
return null;
121128
}
122129

0 commit comments

Comments
 (0)