1212use PhpParser \Node \Expr \FuncCall ;
1313use PhpParser \Node \Expr \Ternary ;
1414use PhpParser \Node \Expr \Variable ;
15- use PHPStan \Type \ArrayType ;
16- use PHPStan \Type \Constant \ConstantArrayType ;
17- use Rector \NodeTypeResolver \TypeAnalyzer \ArrayTypeAnalyzer ;
1815use Rector \Php \PhpVersionProvider ;
1916use Rector \Rector \AbstractRector ;
2017use Rector \ValueObject \PhpVersionFeature ;
2926final class ArraySpreadInsteadOfArrayMergeRector extends AbstractRector implements MinPhpVersionInterface
3027{
3128 public function __construct (
32- private readonly ArrayTypeAnalyzer $ arrayTypeAnalyzer ,
3329 private readonly PhpVersionProvider $ phpVersionProvider ,
3430 ) {
3531 }
@@ -117,7 +113,7 @@ private function refactorArray(FuncCall $funcCall): ?Array_
117113 }
118114
119115 $ value = $ arg ->value ;
120- if ($ this ->shouldSkipArrayForInvalidTypeOrKeys ($ value )) {
116+ if ($ this ->shouldSkipArrayForInvalidKeys ($ value )) {
121117 return null ;
122118 }
123119
@@ -134,29 +130,16 @@ private function refactorArray(FuncCall $funcCall): ?Array_
134130 return $ array ;
135131 }
136132
137- private function shouldSkipArrayForInvalidTypeOrKeys (Expr $ expr ): bool
133+ private function shouldSkipArrayForInvalidKeys (Expr $ expr ): bool
138134 {
139- // we have no idea what it is → cannot change it
140- if (! $ this ->arrayTypeAnalyzer ->isArrayType ($ expr )) {
141- return true ;
142- }
143-
144- $ arrayStaticType = $ this ->getType ($ expr );
145- if (! $ arrayStaticType instanceof ArrayType && ! $ arrayStaticType instanceof ConstantArrayType) {
146- return true ;
147- }
135+ $ type = $ this ->getType ($ expr );
148136
149- return ! $ this ->isArrayKeyTypeAllowed ($ arrayStaticType );
150- }
151-
152- private function isArrayKeyTypeAllowed (ArrayType |ConstantArrayType $ arrayType ): bool
153- {
154- if ($ arrayType ->getIterableKeyType ()->isInteger ()->yes ()) {
155- return true ;
137+ if ($ type ->getIterableKeyType ()->isInteger ()->yes ()) {
138+ return false ;
156139 }
157140
158141 // php 8.1+ allow mixed key: int, string, and null
159- return $ this ->phpVersionProvider ->isAtLeastPhpVersion (PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS );
142+ return ! $ this ->phpVersionProvider ->isAtLeastPhpVersion (PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS );
160143 }
161144
162145 private function resolveValue (Expr $ expr ): Expr
0 commit comments