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,39 @@
<?php

namespace Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\Fixture;

use ArrayIterator;

final class PropertySameNameAsVariable
{
private ArrayIterator $items;

public function run()
{
$items = ['apple'];

return [...$this->items, ...$items];
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\Fixture;

use ArrayIterator;

final class PropertySameNameAsVariable
{
private ArrayIterator $items;

public function run()
{
$items = ['apple'];

return array_merge(iterator_to_array($this->items), $items);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)]);

Expand Down
Loading