44
55use PHPStan \Analyser \Scope ;
66use PHPStan \DependencyInjection \AutowiredService ;
7- use PHPStan \Php \PhpVersion ;
87use PHPStan \TrinaryLogic ;
98use PHPStan \Type \Accessory \AccessoryArrayListType ;
109use PHPStan \Type \Accessory \NonEmptyArrayType ;
2120final class ArrayColumnHelper
2221{
2322
24- public function __construct (
25- private PhpVersion $ phpVersion ,
26- )
27- {
28- }
29-
3023 /**
3124 * @return array{Type, TrinaryLogic}
3225 */
@@ -74,7 +67,7 @@ public function handleAnyArray(Type $arrayType, Type $columnType, Type $indexTyp
7467 }
7568
7669 $ returnKeyType = $ this ->getReturnIndexType ($ arrayType , $ indexType , $ scope );
77- $ returnType = new ArrayType ($ this ->castToArrayKeyType ($ returnKeyType ), $ returnValueType );
70+ $ returnType = new ArrayType ($ this ->castToArrayKeyType ($ returnKeyType, $ scope ), $ returnValueType );
7871
7972 if ($ iterableAtLeastOnce ->yes ()) {
8073 $ returnType = TypeCombinator::intersect ($ returnType , new NonEmptyArrayType ());
@@ -113,7 +106,7 @@ public function handleConstantArray(ConstantArrayType $arrayType, Type $columnTy
113106 }
114107
115108 if ($ keyType !== null ) {
116- $ keyType = $ this ->castToArrayKeyType ($ keyType );
109+ $ keyType = $ this ->castToArrayKeyType ($ keyType, $ scope );
117110 }
118111 $ builder ->setOffsetValueType ($ keyType , $ valueType , $ arrayType ->isOptionalKey ($ i ));
119112 }
@@ -131,9 +124,9 @@ public function handleConstantArray(ConstantArrayType $arrayType, Type $columnTy
131124 if ($ unsealedKeyFromIndex instanceof NeverType) {
132125 $ unsealedKey = $ unsealedTypes [0 ];
133126 } elseif ($ unsealedKeyCertainty ->yes ()) {
134- $ unsealedKey = $ this ->castToArrayKeyType ($ unsealedKeyFromIndex );
127+ $ unsealedKey = $ this ->castToArrayKeyType ($ unsealedKeyFromIndex, $ scope );
135128 } else {
136- $ unsealedKey = $ this ->castToArrayKeyType (TypeCombinator::union ($ unsealedKeyFromIndex , new IntegerType ()));
129+ $ unsealedKey = $ this ->castToArrayKeyType (TypeCombinator::union ($ unsealedKeyFromIndex , new IntegerType ()), $ scope );
137130 }
138131 } else {
139132 // `null` indexType keeps integer-keyed list semantics —
@@ -221,18 +214,23 @@ private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $
221214 return [TypeCombinator::union (...$ returnTypes ), $ certainty ];
222215 }
223216
224- private function castToArrayKeyType (Type $ type ): Type
217+ private function castToArrayKeyType (Type $ type, Scope $ scope ): Type
225218 {
219+ $ throwsTypeError = $ scope ->getPhpVersion ()->throwsTypeErrorForInternalFunctions ();
226220 $ isArray = $ type ->isArray ();
227221 if ($ isArray ->yes ()) {
228- return $ this ->phpVersion ->throwsTypeErrorForInternalFunctions () ? new NeverType () : new IntegerType ();
222+ if ($ throwsTypeError ->yes ()) {
223+ return new NeverType ();
224+ }
225+
226+ return new IntegerType ();
229227 }
230228 if ($ isArray ->no ()) {
231229 return $ type ->toArrayKey ();
232230 }
233231 $ withoutArrayType = TypeCombinator::remove ($ type , new ArrayType (new MixedType (), new MixedType ()));
234232 $ keyType = $ withoutArrayType ->toArrayKey ();
235- if ($ this -> phpVersion -> throwsTypeErrorForInternalFunctions ()) {
233+ if ($ throwsTypeError -> yes ()) {
236234 return $ keyType ;
237235 }
238236 return TypeCombinator::union ($ keyType , new IntegerType ());
0 commit comments