99use PHPStan \TrinaryLogic ;
1010use PHPStan \Type \Accessory \AccessoryArrayListType ;
1111use PHPStan \Type \Accessory \HasOffsetType ;
12+ use PHPStan \Type \Accessory \HasOffsetValueType ;
1213use PHPStan \Type \Accessory \NonEmptyArrayType ;
1314use PHPStan \Type \ArrayType ;
1415use PHPStan \Type \Constant \ConstantArrayType ;
2021use PHPStan \Type \NeverType ;
2122use PHPStan \Type \Type ;
2223use PHPStan \Type \TypeCombinator ;
24+ use PHPStan \Type \TypeUtils ;
2325use function array_keys ;
2426use function count ;
2527use function in_array ;
@@ -74,7 +76,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
7476 static fn (Type $ argType ) => $ argType ->isConstantArray (),
7577 );
7678
77- $ nonOptionalConstKeys = [];
79+ $ offsetTypes = [];
7880 $ newArrayBuilder = null ;
7981 if ($ allConstant ->yes ()) {
8082 $ newArrayBuilder = ConstantArrayTypeBuilder::createEmpty ();
@@ -90,11 +92,24 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
9092 continue ;
9193 }
9294
93- $ nonOptionalConstKeys [ ] = $ keyType ;
95+ $ offsetTypes [ $ keyType -> getValue () ] = new HasOffsetType ( $ keyType) ;
9496 }
9597 }
9698
9799 if ($ newArrayBuilder === null ) {
100+ foreach (TypeUtils::getAccessoryTypes ($ argType ) as $ accessoryType ) {
101+ if (
102+ !($ accessoryType instanceof HasOffsetType)
103+ && !($ accessoryType instanceof HasOffsetValueType)
104+ ) {
105+ continue ;
106+ }
107+
108+ $ offsetType = $ accessoryType ->getOffsetType ();
109+ $ offsetTypes [$ offsetType ->getValue ()] = new HasOffsetType ($ offsetType );
110+
111+ }
112+
98113 continue ;
99114 }
100115
@@ -136,11 +151,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
136151 return new ConstantArrayType ([], []);
137152 }
138153
139- $ offsetTypes = [];
140- foreach ($ nonOptionalConstKeys as $ constKey ) {
141- $ offsetTypes [] = new HasOffsetType ($ constKey );
142- }
143-
144154 $ arrayType = new ArrayType (
145155 $ keyType ,
146156 TypeCombinator::union (...$ valueTypes ),
0 commit comments