21
21
use PHPStan \Type \BenevolentUnionType ;
22
22
use PHPStan \Type \Constant \ConstantArrayType ;
23
23
use PHPStan \Type \Constant \ConstantArrayTypeBuilder ;
24
+ use PHPStan \Type \Constant \ConstantBooleanType ;
24
25
use PHPStan \Type \DynamicFunctionReturnTypeExtension ;
25
26
use PHPStan \Type \MixedType ;
26
27
use PHPStan \Type \NeverType ;
@@ -170,12 +171,12 @@ private function filterByTruthyValue(Scope $scope, Error|Variable|null $itemVar,
170
171
$ builder = ConstantArrayTypeBuilder::createEmpty ();
171
172
foreach ($ constantArray ->getKeyTypes () as $ i => $ keyType ) {
172
173
$ itemType = $ constantArray ->getValueTypes ()[$ i ];
173
- [$ newKeyType , $ newItemType ] = $ this ->processKeyAndItemType ($ scope , $ keyType , $ itemType , $ itemVar , $ keyVar , $ expr );
174
+ [$ newKeyType , $ newItemType, $ optional ] = $ this ->processKeyAndItemType ($ scope , $ keyType , $ itemType , $ itemVar , $ keyVar , $ expr );
174
175
if ($ newKeyType instanceof NeverType || $ newItemType instanceof NeverType) {
175
176
continue ;
176
177
}
177
178
if ($ itemType ->equals ($ newItemType ) && $ keyType ->equals ($ newKeyType )) {
178
- $ builder ->setOffsetValueType ($ keyType , $ itemType );
179
+ $ builder ->setOffsetValueType ($ keyType , $ itemType, $ optional );
179
180
continue ;
180
181
}
181
182
@@ -198,7 +199,7 @@ private function filterByTruthyValue(Scope $scope, Error|Variable|null $itemVar,
198
199
}
199
200
200
201
/**
201
- * @return array{Type, Type}
202
+ * @return array{Type, Type, bool }
202
203
*/
203
204
private function processKeyAndItemType (MutatingScope $ scope , Type $ keyType , Type $ itemType , Error |Variable |null $ itemVar , Error |Variable |null $ keyVar , Expr $ expr ): array
204
205
{
@@ -220,11 +221,13 @@ private function processKeyAndItemType(MutatingScope $scope, Type $keyType, Type
220
221
$ scope = $ scope ->assignVariable ($ keyVarName , $ keyType );
221
222
}
222
223
224
+ $ booleanResult = $ scope ->getType ($ expr )->toBoolean ();
223
225
$ scope = $ scope ->filterByTruthyValue ($ expr );
224
226
225
227
return [
226
228
$ keyVarName !== null ? $ scope ->getVariableType ($ keyVarName ) : $ keyType ,
227
229
$ itemVarName !== null ? $ scope ->getVariableType ($ itemVarName ) : $ itemType ,
230
+ !$ booleanResult instanceof ConstantBooleanType,
228
231
];
229
232
}
230
233
0 commit comments