1919use PHPStan \Type \Constant \ConstantIntegerType ;
2020use PHPStan \Type \Constant \ConstantStringType ;
2121use PHPStan \Type \DynamicFunctionReturnTypeExtension ;
22+ use PHPStan \Type \FloatRangeType ;
2223use PHPStan \Type \FloatType ;
2324use PHPStan \Type \GeneralizePrecision ;
2425use PHPStan \Type \IntegerRangeType ;
@@ -250,7 +251,7 @@ private static function getLongRangeType(
250251
251252 // before PHP 8.3 a float argument produced floats even when none of the arguments had a fractional
252253 // part, and even for two strings, of which only a numeric one kept its value
253- $ floatListType = self ::getNonEmptyListOfType ( new FloatType ());
254+ $ floatListType = self ::getFloatListType ( $ startConstant -> getValue (), $ endConstant -> getValue ());
254255 if ($ hasStricterRange ->no ()) {
255256 return $ floatListType ;
256257 }
@@ -272,8 +273,6 @@ private static function getLongRangeItemsType(
272273 ?array $ rangeValues ,
273274 ): Type
274275 {
275- $ floatListType = self ::getNonEmptyListOfType (new FloatType ());
276-
277276 if ($ rangeValues !== null ) {
278277 // range() only ever returns values of a single type
279278 $ firstValue = $ rangeValues [0 ];
@@ -289,14 +288,14 @@ private static function getLongRangeItemsType(
289288 }
290289
291290 if (is_float ($ firstValue ) || is_float ($ lastValue )) {
292- return $ floatListType ;
291+ return self :: getFloatListType ( $ firstValue , $ lastValue ) ;
293292 }
294293
295294 $ bounds = $ startConstant instanceof ConstantIntegerType && $ endConstant instanceof ConstantIntegerType
296295 ? [$ startConstant ->getValue (), $ endConstant ->getValue ()]
297296 : [$ firstValue , $ lastValue ];
298297 } elseif ($ startConstant instanceof ConstantFloatType || $ endConstant instanceof ConstantFloatType) {
299- return $ floatListType ;
298+ return self :: getFloatListType ( $ startConstant -> getValue (), $ endConstant -> getValue ()) ;
300299 } elseif (!$ startConstant instanceof ConstantIntegerType || !$ endConstant instanceof ConstantIntegerType) {
301300 return self ::getNonEmptyListOfType (
302301 TypeCombinator ::union (
@@ -307,7 +306,7 @@ private static function getLongRangeItemsType(
307306 );
308307 } elseif (floor ($ stepConstant ->getValue ()) !== (float ) $ stepConstant ->getValue ()) {
309308 // a step with a fractional part produces floats
310- return $ floatListType ;
309+ return self :: getFloatListType ( $ startConstant -> getValue (), $ endConstant -> getValue ()) ;
311310 } else {
312311 $ bounds = [$ startConstant ->getValue (), $ endConstant ->getValue ()];
313312 }
@@ -329,6 +328,22 @@ private static function getLongRangeItemsType(
329328 return self ::getNonEmptyListOfType (IntegerRangeType ::fromInterval ($ low , $ high ), $ maxKey );
330329 }
331330
331+ /**
332+ * range() stops before it passes the end, so the floats lie between the boundaries,
333+ * which a finite range length keeps finite. A string boundary leaves them unbounded.
334+ */
335+ private static function getFloatListType (int |float |string $ start , int |float |string $ end ): IntersectionType
336+ {
337+ if (is_string ($ start ) || is_string ($ end )) {
338+ return self ::getNonEmptyListOfType (new FloatType ());
339+ }
340+
341+ return self ::getNonEmptyListOfType (FloatRangeType ::fromInterval (
342+ min ((float ) $ start , (float ) $ end ),
343+ max ((float ) $ start , (float ) $ end ),
344+ ));
345+ }
346+
332347 private static function getNonEmptyListOfType (Type $ type , ?int $ maxKey = null ): IntersectionType
333348 {
334349 return new IntersectionType ([
0 commit comments