Skip to content

Commit 7b530e2

Browse files
committed
fix downgrade array spread
1 parent c05b162 commit 7b530e2

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use PhpParser\Node\Expr\Variable;
1414
use PhpParser\Node\Name;
1515
use PHPStan\Analyser\MutatingScope;
16-
use PHPStan\Type\ArrayType;
1716
use PHPStan\Type\IterableType;
1817
use PHPStan\Type\ObjectType;
1918
use PHPStan\Type\Type;
@@ -85,7 +84,7 @@ private function createArrayMergeFuncCall(array $arrayItems, MutatingScope $muta
8584
return $this->createArgFromSpreadArrayItem($mutatingScope, $arrayItem);
8685
}
8786

88-
return new Arg($arrayItem);
87+
return new Arg($arrayItem->value);
8988
}, $arrayItems);
9089

9190
return new FuncCall(new Name('array_merge'), $args);
@@ -121,13 +120,13 @@ private function createArgFromSpreadArrayItem(MutatingScope $mutatingScope, Arra
121120
}
122121
}
123122

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

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

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

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

143142
/**

rules/DowngradePhp81/Rector/Array_/DowngradeArraySpreadStringKeyRector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\DowngradePhp81\Rector\Array_;
66

7+
use PHPStan\Type\Constant\ConstantArrayType;
78
use PhpParser\Node;
89
use PhpParser\Node\ArrayItem;
910
use PhpParser\Node\Expr\Array_;
@@ -89,10 +90,11 @@ private function shouldSkipArray(Array_ $array): bool
8990
}
9091

9192
$type = $this->nodeTypeResolver->getType($item->value);
92-
if (! $type instanceof ArrayType) {
93+
if (! $type->isArray()->yes()) {
9394
continue;
9495
}
9596

97+
/** @var ArrayType|ConstantArrayType $type */
9698
$keyType = $type->getKeyType();
9799
if ($keyType instanceof IntegerType) {
98100
return true;

0 commit comments

Comments
 (0)