Skip to content

Commit

Permalink
fix downgrade array spread
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 19, 2024
1 parent c05b162 commit 7b530e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IterableType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -85,7 +84,7 @@ private function createArrayMergeFuncCall(array $arrayItems, MutatingScope $muta
return $this->createArgFromSpreadArrayItem($mutatingScope, $arrayItem);
}

return new Arg($arrayItem);
return new Arg($arrayItem->value);
}, $arrayItems);

return new FuncCall(new Name('array_merge'), $args);
Expand Down Expand Up @@ -121,13 +120,13 @@ private function createArgFromSpreadArrayItem(MutatingScope $mutatingScope, Arra
}
}

$iteratorToArrayFuncCall = new FuncCall(new Name('iterator_to_array'), [new Arg($arrayItem)]);
$iteratorToArrayFuncCall = new FuncCall(new Name('iterator_to_array'), [new Arg($arrayItem->value)]);

// If we know it is an array, then print it directly
// Otherwise PHPStan throws an error:
// "Else branch is unreachable because ternary operator condition is always true."
if ($type instanceof ArrayType) {
return new Arg($arrayItem);
if ($type->isArray()->yes()) {
return new Arg($arrayItem->value);
}

// If it is iterable, then directly return `iterator_to_array`
Expand All @@ -136,8 +135,8 @@ private function createArgFromSpreadArrayItem(MutatingScope $mutatingScope, Arra
}

// Print a ternary, handling either an array or an iterator
$inArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($arrayItem)]);
return new Arg(new Ternary($inArrayFuncCall, $arrayItem, $iteratorToArrayFuncCall));
$inArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($arrayItem->value)]);
return new Arg(new Ternary($inArrayFuncCall, $arrayItem->value, $iteratorToArrayFuncCall));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DowngradePhp81\Rector\Array_;

use PHPStan\Type\Constant\ConstantArrayType;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
Expand Down Expand Up @@ -89,10 +90,11 @@ private function shouldSkipArray(Array_ $array): bool
}

$type = $this->nodeTypeResolver->getType($item->value);
if (! $type instanceof ArrayType) {
if (! $type->isArray()->yes()) {
continue;
}

/** @var ArrayType|ConstantArrayType $type */
$keyType = $type->getKeyType();
if ($keyType instanceof IntegerType) {
return true;
Expand Down

0 comments on commit 7b530e2

Please sign in to comment.