2929use PHPStan \Type \Type ;
3030use PHPStan \Type \TypeCombinator ;
3131use PHPStan \Type \UnionType ;
32- use ValueError ;
3332use function abs ;
3433use function count ;
3534use function floor ;
3938use function is_string ;
4039use function max ;
4140use function min ;
42- use function range ;
4341
4442#[AutowiredService]
4543final class RangeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -100,7 +98,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
10098 $ rangeLength = self ::getRangeLength ($ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue ());
10199 if ($ rangeLength !== null && $ rangeLength > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT ) {
102100 // without calling range() nothing rejects a negative step on an increasing range, which PHP 8.3 does
103- if (self ::isNegativeStepOnIncreasingRange ($ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue ())) {
101+ if (RangeFunctionArgumentsHelper ::isNegativeStepOnIncreasingRange ($ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue ())) {
104102 if ($ hasStricterRange ->yes ()) {
105103 $ throwingCombinations ++;
106104 continue ;
@@ -114,15 +112,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
114112 return self ::getLongRangeType ($ hasStricterRange , $ startConstant , $ endConstant , $ stepConstant , $ stepType , null );
115113 }
116114
117- try {
118- $ rangeValues = @range ($ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue ());
119- } catch (ValueError ) {
120- $ rangeValues = false ;
121- }
122-
123- // PHP 7 reports an invalid step by returning false instead of throwing a ValueError
115+ $ rangeValues = RangeFunctionArgumentsHelper ::callRange ($ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue ());
124116 if ($ rangeValues === false ) {
125- $ fails = self :: failsOnAnalysedVersion ($ hasStricterRange , $ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue ());
117+ $ fails = RangeFunctionArgumentsHelper :: rejects ($ hasStricterRange , $ startConstant ->getValue (), $ endConstant ->getValue (), $ stepConstant ->getValue (), true );
126118 if (!$ fails ->yes ()) {
127119 // the analysed PHP version might accept the step, but only the runtime's verdict is known
128120 $ hasUnknownCombination = true ;
@@ -202,48 +194,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
202194 );
203195 }
204196
205- /**
206- * The constant arguments are folded by calling range() on the PHP version PHPStan
207- * itself runs on, so a failure only says that the runtime rejects the step.
208- * This checks whether the analysed PHP versions reject it as well.
209- */
210- private static function failsOnAnalysedVersion (TrinaryLogic $ hasStricterRange , int |float |string $ start , int |float |string $ end , int |float $ step ): TrinaryLogic
211- {
212- if (self ::failsBeforePhp83 ($ start , $ end , $ step )) {
213- return TrinaryLogic ::createYes ();
214- }
215-
216- return $ hasStricterRange ;
217- }
218-
219- /**
220- * PHP 7.x reported these steps with a warning and false, PHP 8.0-8.2 with a ValueError.
221- */
222- private static function failsBeforePhp83 (int |float |string $ start , int |float |string $ end , int |float $ step ): bool
223- {
224- if (is_string ($ start ) || is_string ($ end )) {
225- // how a string boundary was coerced before PHP 8.3 is not modelled here
226- return false ;
227- }
228-
229- $ start = (float ) $ start ;
230- $ end = (float ) $ end ;
231- if (!is_finite ($ start ) || !is_finite ($ end )) {
232- return false ;
233- }
234-
235- if ($ start === $ end ) {
236- // a step of 0 was only rejected for integer boundaries in this case
237- return false ;
238- }
239-
240- // the sign of the step used to be ignored, so the step was only rejected
241- // when it was 0 or wider than the range itself
242- $ step = abs ((float ) $ step );
243-
244- return $ step === 0.0 || abs ($ end - $ start ) < $ step ;
245- }
246-
247197 /**
248198 * The number of items range() creates for numeric arguments, or null when
249199 * only calling it tells - for a character range, which has at most 256
@@ -271,15 +221,6 @@ private static function getRangeLength(int|float|string $start, int|float|string
271221 return floor ($ length ) + 1 ;
272222 }
273223
274- private static function isNegativeStepOnIncreasingRange (int |float |string $ start , int |float |string $ end , int |float $ step ): bool
275- {
276- // a non-numeric string next to a number is 0
277- $ startNumber = is_numeric ($ start ) ? (float ) $ start : 0.0 ;
278- $ endNumber = is_numeric ($ end ) ? (float ) $ end : 0.0 ;
279-
280- return $ step < 0 && $ startNumber < $ endNumber ;
281- }
282-
283224 /**
284225 * @param non-empty-list<int|float|string>|null $rangeValues null when range() was not called
285226 */
0 commit comments