@@ -988,71 +988,74 @@ private function specifyTypesForCountFuncCall(
988
988
return null ;
989
989
}
990
990
991
- $ resultType = TypeTraverser::map ($ type , static function (Type $ type , callable $ traverse ) use ($ sizeType , $ context ) {
992
- if ($ type instanceof UnionType) {
993
- return $ traverse ($ type );
994
- }
995
-
996
- $ isSizeSuperTypeOfArraySize = $ sizeType ->isSuperTypeOf ($ type ->getArraySize ());
991
+ $ resultTypes = [];
992
+ $ innerTypes = $ type instanceof UnionType ? $ type ->getTypes () : [$ type ];
993
+ foreach ($ innerTypes as $ innerType ) {
994
+ $ isSizeSuperTypeOfArraySize = $ sizeType ->isSuperTypeOf ($ innerType ->getArraySize ());
997
995
if ($ isSizeSuperTypeOfArraySize ->no ()) {
998
- return new NeverType () ;
996
+ continue ;
999
997
}
1000
998
if ($ context ->falsey () && $ isSizeSuperTypeOfArraySize ->maybe ()) {
1001
- return new NeverType () ;
999
+ continue ;
1002
1000
}
1003
1001
1004
- if ($ type ->isList ()->yes ()) {
1005
- if (
1006
- $ sizeType instanceof ConstantIntegerType
1007
- && $ sizeType ->getValue () < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
1008
- ) {
1009
- // turn optional offsets non-optional
1010
- $ valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty ();
1011
- for ($ i = 0 ; $ i < $ sizeType ->getValue (); $ i ++) {
1012
- $ offsetType = new ConstantIntegerType ($ i );
1013
- $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ type ->getOffsetValueType ($ offsetType ));
1014
- }
1015
- return $ valueTypesBuilder ->getArray ();
1002
+ if (
1003
+ $ innerType ->isList ()->yes ()
1004
+ && $ sizeType instanceof ConstantIntegerType
1005
+ && $ sizeType ->getValue () < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
1006
+ ) {
1007
+ // turn optional offsets non-optional
1008
+ $ valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty ();
1009
+ for ($ i = 0 ; $ i < $ sizeType ->getValue (); $ i ++) {
1010
+ $ offsetType = new ConstantIntegerType ($ i );
1011
+ $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ innerType ->getOffsetValueType ($ offsetType ));
1016
1012
}
1013
+ $ resultTypes [] = $ valueTypesBuilder ->getArray ();
1014
+ continue ;
1015
+ }
1017
1016
1018
- if (
1019
- $ sizeType instanceof IntegerRangeType
1020
- && $ sizeType ->getMin () !== null
1021
- ) {
1022
- // turn optional offsets non-optional
1023
- $ valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty ();
1024
- for ($ i = 0 ; $ i < $ sizeType ->getMin (); $ i ++) {
1017
+ if (
1018
+ $ innerType ->isList ()->yes ()
1019
+ && $ sizeType instanceof IntegerRangeType
1020
+ && $ sizeType ->getMin () !== null
1021
+ ) {
1022
+ // turn optional offsets non-optional
1023
+ $ valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty ();
1024
+ for ($ i = 0 ; $ i < $ sizeType ->getMin (); $ i ++) {
1025
+ $ offsetType = new ConstantIntegerType ($ i );
1026
+ $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ innerType ->getOffsetValueType ($ offsetType ));
1027
+ }
1028
+ if ($ sizeType ->getMax () !== null ) {
1029
+ for ($ i = $ sizeType ->getMin (); $ i < $ sizeType ->getMax (); $ i ++) {
1025
1030
$ offsetType = new ConstantIntegerType ($ i );
1026
- $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ type ->getOffsetValueType ($ offsetType ));
1031
+ $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ innerType ->getOffsetValueType ($ offsetType ), true );
1027
1032
}
1028
- if ($ sizeType ->getMax () !== null ) {
1029
- for ($ i = $ sizeType ->getMin (); $ i < $ sizeType ->getMax (); $ i ++) {
1030
- $ offsetType = new ConstantIntegerType ($ i );
1031
- $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ type ->getOffsetValueType ($ offsetType ), true );
1032
- }
1033
- } elseif ($ type ->isConstantArray ()->yes ()) {
1034
- for ($ i = $ sizeType ->getMin ();; $ i ++) {
1035
- $ offsetType = new ConstantIntegerType ($ i );
1036
- $ hasOffset = $ type ->hasOffsetValueType ($ offsetType );
1037
- if ($ hasOffset ->no ()) {
1038
- break ;
1039
- }
1040
- $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ type ->getOffsetValueType ($ offsetType ), !$ hasOffset ->yes ());
1033
+ } elseif ($ innerType ->isConstantArray ()->yes ()) {
1034
+ for ($ i = $ sizeType ->getMin ();; $ i ++) {
1035
+ $ offsetType = new ConstantIntegerType ($ i );
1036
+ $ hasOffset = $ innerType ->hasOffsetValueType ($ offsetType );
1037
+ if ($ hasOffset ->no ()) {
1038
+ break ;
1041
1039
}
1042
- } else {
1043
- return TypeCombinator::intersect ($ type , new NonEmptyArrayType ());
1040
+ $ valueTypesBuilder ->setOffsetValueType ($ offsetType , $ innerType ->getOffsetValueType ($ offsetType ), !$ hasOffset ->yes ());
1044
1041
}
1045
-
1046
- return $ valueTypesBuilder ->getArray ();
1042
+ } else {
1043
+ $ resultTypes [] = TypeCombinator::intersect ($ innerType , new NonEmptyArrayType ());
1044
+ continue ;
1047
1045
}
1048
1046
1049
- return $ context ->truthy () ? $ type : new NeverType ();
1047
+ $ resultTypes [] = $ valueTypesBuilder ->getArray ();
1048
+ continue ;
1050
1049
}
1051
1050
1052
- return $ context ->truthy () ? TypeCombinator::intersect ($ type , new NonEmptyArrayType ()) : new NeverType ();
1053
- });
1051
+ if (!$ context ->truthy ()) {
1052
+ continue ;
1053
+ }
1054
+
1055
+ $ resultTypes [] = $ innerType ;
1056
+ }
1054
1057
1055
- return $ this ->create ($ countFuncCall ->getArgs ()[0 ]->value , $ resultType , $ context , $ scope )->setRootExpr ($ rootExpr );
1058
+ return $ this ->create ($ countFuncCall ->getArgs ()[0 ]->value , TypeCombinator:: union (... $ resultTypes ) , $ context , $ scope )->setRootExpr ($ rootExpr );
1056
1059
}
1057
1060
1058
1061
private function specifyTypesForConstantBinaryExpression (
0 commit comments