Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

final class SkipDynamicKey
{
public function create($variable, $type, $data)
{
$someVar[sprintf('%', $type)] = 0.0;
$someVar[$type] = 0.0;

return $someVar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\CodeQuality\ValueObject\KeyAndExpr;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\PhpParser\Node\Value\ValueResolver;

final readonly class VariableDimFetchAssignResolver
{
public function __construct(
private ExprAnalyzer $exprAnalyzer,
private ValueResolver $valueResolver
) {
}
Expand Down Expand Up @@ -54,6 +56,10 @@ public function resolveFromStmtsAndVariable(array $stmts, ?Assign $emptyArrayAss

$arrayDimFetch = $assign->var;
while ($arrayDimFetch instanceof ArrayDimFetch) {
if ($arrayDimFetch->dim instanceof Expr && $this->exprAnalyzer->isDynamicExpr($arrayDimFetch->dim)) {
return [];
}

$dimValues[] = $arrayDimFetch->dim instanceof Expr ? $this->valueResolver->getValue(
$arrayDimFetch->dim
) : $key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function refactor(Node $node): ?Node
}

// init maybe from before if
if ($emptyArrayAssign === null && ! $node instanceof FunctionLike) {
if (!$emptyArrayAssign instanceof Assign && ! $node instanceof FunctionLike) {
return null;
}

Expand Down
Loading