Skip to content

Commit c98145f

Browse files
[CodeQuality] Skip dynamic key on InlineArrayReturnAssignRector (#7275)
* [CodeQuality] Skip dynamic key on InlineArrayReturnAssignRector * Fix * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent f649c51 commit c98145f

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class SkipDynamicKey
6+
{
7+
public function create($variable, $type, $data)
8+
{
9+
$someVar[sprintf('%', $type)] = 0.0;
10+
$someVar[$type] = 0.0;
11+
12+
return $someVar;
13+
}
14+
}

rules/CodeQuality/NodeAnalyzer/VariableDimFetchAssignResolver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use PhpParser\Node\Stmt\Expression;
1212
use PhpParser\Node\Stmt\Return_;
1313
use Rector\CodeQuality\ValueObject\KeyAndExpr;
14+
use Rector\NodeAnalyzer\ExprAnalyzer;
1415
use Rector\PhpParser\Node\Value\ValueResolver;
1516

1617
final readonly class VariableDimFetchAssignResolver
1718
{
1819
public function __construct(
20+
private ExprAnalyzer $exprAnalyzer,
1921
private ValueResolver $valueResolver
2022
) {
2123
}
@@ -54,6 +56,10 @@ public function resolveFromStmtsAndVariable(array $stmts, ?Assign $emptyArrayAss
5456

5557
$arrayDimFetch = $assign->var;
5658
while ($arrayDimFetch instanceof ArrayDimFetch) {
59+
if ($arrayDimFetch->dim instanceof Expr && $this->exprAnalyzer->isDynamicExpr($arrayDimFetch->dim)) {
60+
return [];
61+
}
62+
5763
$dimValues[] = $arrayDimFetch->dim instanceof Expr ? $this->valueResolver->getValue(
5864
$arrayDimFetch->dim
5965
) : $key;

rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function refactor(Node $node): ?Node
107107
}
108108

109109
// init maybe from before if
110-
if ($emptyArrayAssign === null && ! $node instanceof FunctionLike) {
110+
if (!$emptyArrayAssign instanceof Assign && ! $node instanceof FunctionLike) {
111111
return null;
112112
}
113113

0 commit comments

Comments
 (0)