diff --git a/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/property_same_name_as_variable.php.inc b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/property_same_name_as_variable.php.inc new file mode 100644 index 00000000..3011e192 --- /dev/null +++ b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/property_same_name_as_variable.php.inc @@ -0,0 +1,39 @@ +items, ...$items]; + } +} + +?> +----- +items), $items); + } +} + +?> diff --git a/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php b/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php index f2166018..e0e3c6c0 100644 --- a/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php +++ b/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php @@ -9,23 +9,14 @@ use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; -use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PHPStan\Analyser\MutatingScope; use PHPStan\Type\IterableType; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; -use Rector\Exception\ShouldNotHappenException; -use Rector\NodeNameResolver\NodeNameResolver; -use Rector\NodeTypeResolver\Node\AttributeKey; final readonly class ArrayMergeFromArraySpreadFactory { - public function __construct( - private NodeNameResolver $nodeNameResolver - ) { - } - public function createFromArray(Array_ $array, MutatingScope $mutatingScope): FuncCall { $newArrayItems = $this->dissolveArrayItems($array); @@ -100,24 +91,7 @@ private function createArrayItemFromArray(array $items): ArrayItem private function createArgFromSpreadArrayItem(MutatingScope $mutatingScope, ArrayItem $arrayItem): Arg { - // By now every item is a variable - /** @var Variable $variable */ - $variable = $arrayItem->value; - - $variableName = $this->nodeNameResolver->getName($variable) ?? ''; - - // If the variable is not in scope, it's one we just added. - // Then get the type from the attribute - if ($mutatingScope->hasVariableType($variableName)->yes()) { - $type = $mutatingScope->getVariableType($variableName); - } else { - $originalNode = $arrayItem->getAttribute(AttributeKey::ORIGINAL_NODE); - if ($originalNode instanceof ArrayItem) { - $type = $mutatingScope->getType($originalNode->value); - } else { - throw new ShouldNotHappenException(); - } - } + $type = $mutatingScope->getType($arrayItem->value); $iteratorToArrayFuncCall = new FuncCall(new Name('iterator_to_array'), [new Arg($arrayItem->value)]);