@@ -109,7 +109,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
109109 }
110110 }
111111
112- return self ::getLongRangeType ($ hasStricterRange , $ startConstant , $ endConstant , $ stepConstant , $ stepType , null );
112+ $ constantReturnTypes [] = self ::getLongRangeType ($ hasStricterRange , $ startConstant , $ endConstant , $ stepConstant , $ stepType , null );
113+ continue ;
113114 }
114115
115116 $ rangeValues = RangeFunctionArgumentsHelper ::callRange ($ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue ());
@@ -127,8 +128,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
127128 }
128129
129130 if (count ($ rangeValues ) > self ::RANGE_LENGTH_THRESHOLD ) {
130- return self ::getLongRangeType ($ hasStricterRange , $ startConstant , $ endConstant , $ stepConstant , $ stepType , $ rangeValues );
131+ $ constantReturnTypes [] = self ::getLongRangeType ($ hasStricterRange , $ startConstant , $ endConstant , $ stepConstant , $ stepType , $ rangeValues );
132+ continue ;
131133 }
134+
132135 $ arrayBuilder = ConstantArrayTypeBuilder ::createEmpty ();
133136 foreach ($ rangeValues as $ value ) {
134137 $ arrayBuilder ->setOffsetValueType (null , $ scope ->getTypeFromValue ($ value ));
@@ -139,26 +142,29 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
139142 }
140143 }
141144
142- if ($ hasUnknownCombination ) {
143- $ constantReturnTypes = [];
144- }
145+ if (!$ hasUnknownCombination && !$ hasSkippedCombination ) {
146+ if (count ($ constantReturnTypes ) > 0 ) {
147+ return TypeCombinator ::union (...$ constantReturnTypes );
148+ }
145149
146- if (count ($ constantReturnTypes ) > 0 ) {
147- return TypeCombinator ::union (...$ constantReturnTypes );
150+ // nothing is returned when every combination of the constant arguments throws
151+ if (
152+ $ constantCombinations > 0
153+ && $ throwingCombinations === $ constantCombinations
154+ && $ startType ->isConstantScalarValue ()->yes ()
155+ && $ endType ->isConstantScalarValue ()->yes ()
156+ && $ stepType ->isConstantScalarValue ()->yes ()
157+ ) {
158+ return new NeverType ();
159+ }
148160 }
149161
150- // nothing is returned when every combination of the constant arguments throws
151- if (
152- $ constantCombinations > 0
153- && $ throwingCombinations === $ constantCombinations
154- && !$ hasSkippedCombination
155- && $ startType ->isConstantScalarValue ()->yes ()
156- && $ endType ->isConstantScalarValue ()->yes ()
157- && $ stepType ->isConstantScalarValue ()->yes ()
158- ) {
159- return new NeverType ();
160- }
162+ // the general type covers the combinations that could not be decided, the rest keep their own types
163+ return TypeCombinator ::union (self ::getGeneralType ($ startType , $ endType , $ stepType ), ...$ constantReturnTypes );
164+ }
161165
166+ private static function getGeneralType (Type $ startType , Type $ endType , Type $ stepType ): Type
167+ {
162168 $ argType = TypeCombinator ::union ($ startType , $ endType );
163169 $ isInteger = $ argType ->isInteger ()->yes ();
164170 $ isStepInteger = $ stepType ->isInteger ()->yes ();
@@ -232,6 +238,41 @@ private static function getLongRangeType(
232238 Type $ stepType ,
233239 ?array $ rangeValues ,
234240 ): Type
241+ {
242+ $ type = self ::getLongRangeItemsType ($ startConstant , $ endConstant , $ stepConstant , $ stepType , $ rangeValues );
243+ if (
244+ $ hasStricterRange ->yes ()
245+ || (
246+ !$ startConstant instanceof ConstantFloatType
247+ && !$ endConstant instanceof ConstantFloatType
248+ && !$ stepConstant instanceof ConstantFloatType
249+ )
250+ ) {
251+ return $ type ;
252+ }
253+
254+ // before PHP 8.3 a float argument produced floats even when none of the arguments had a fractional
255+ // part, and even for two strings, which then counted as 0
256+ $ floatListType = self ::getNonEmptyListOfType (new FloatType ());
257+ if ($ hasStricterRange ->no ()) {
258+ return $ floatListType ;
259+ }
260+
261+ return TypeCombinator ::union ($ type , $ floatListType );
262+ }
263+
264+ /**
265+ * The type of the items the runtime returned, or without them what PHP 8.3 returns.
266+ *
267+ * @param non-empty-list<int|float|string>|null $rangeValues
268+ */
269+ private static function getLongRangeItemsType (
270+ ConstantIntegerType |ConstantFloatType |ConstantStringType $ startConstant ,
271+ ConstantIntegerType |ConstantFloatType |ConstantStringType $ endConstant ,
272+ ConstantIntegerType |ConstantFloatType $ stepConstant ,
273+ Type $ stepType ,
274+ ?array $ rangeValues ,
275+ ): Type
235276 {
236277 $ floatListType = self ::getNonEmptyListOfType (new FloatType ());
237278
@@ -252,6 +293,10 @@ private static function getLongRangeType(
252293 if (is_float ($ firstValue ) || is_float ($ lastValue )) {
253294 return $ floatListType ;
254295 }
296+
297+ $ bounds = $ startConstant instanceof ConstantIntegerType && $ endConstant instanceof ConstantIntegerType
298+ ? [$ startConstant ->getValue (), $ endConstant ->getValue ()]
299+ : [$ firstValue , $ lastValue ];
255300 } elseif ($ startConstant instanceof ConstantFloatType || $ endConstant instanceof ConstantFloatType) {
256301 return $ floatListType ;
257302 } elseif (!$ startConstant instanceof ConstantIntegerType || !$ endConstant instanceof ConstantIntegerType) {
@@ -266,40 +311,11 @@ private static function getLongRangeType(
266311 // a step with a fractional part produces floats
267312 return $ floatListType ;
268313 } else {
269- $ firstValue = $ startConstant ->getValue ();
270- $ lastValue = $ endConstant ->getValue ();
271- }
272-
273- if ($ startConstant instanceof ConstantIntegerType && $ endConstant instanceof ConstantIntegerType) {
274- $ firstValue = $ startConstant ->getValue ();
275- $ lastValue = $ endConstant ->getValue ();
314+ $ bounds = [$ startConstant ->getValue (), $ endConstant ->getValue ()];
276315 }
277316
278317 // the sequence is monotonic, so the first and the last value are its bounds
279- $ intListType = self ::getNonEmptyListOfType (IntegerRangeType ::fromInterval (
280- min ($ firstValue , $ lastValue ),
281- max ($ firstValue , $ lastValue ),
282- ));
283-
284- if (
285- !$ startConstant instanceof ConstantFloatType
286- && !$ endConstant instanceof ConstantFloatType
287- && !$ stepConstant instanceof ConstantFloatType
288- ) {
289- return $ intListType ;
290- }
291-
292- // before PHP 8.3 a float argument produced floats even when none of
293- // the arguments had a fractional part
294- if ($ hasStricterRange ->yes ()) {
295- return $ intListType ;
296- }
297-
298- if ($ hasStricterRange ->no ()) {
299- return $ floatListType ;
300- }
301-
302- return TypeCombinator ::union ($ intListType , $ floatListType );
318+ return self ::getNonEmptyListOfType (IntegerRangeType ::fromInterval (min ($ bounds ), max ($ bounds )));
303319 }
304320
305321 private static function getNonEmptyListOfType (Type $ type ): IntersectionType
0 commit comments