Repository navigation
Add range() throw type extension - #6539
Conversation
10cdacf to
2bfc19a
Compare
8c793b0 to
a864786
Compare
|
This pull request has been marked as ready for review. |
a864786 to
5a7b919
Compare
|
This pull request has been marked as ready for review. |
7bc920f to
37f4ee3
Compare
|
This pull request has been marked as ready for review. |
ff5c6a8 to
af1075f
Compare
There was a problem hiding this comment.
sorry for coming back to this PR so late. I just rebased it.
ondrej recently added float ranges support to PHPStan, so this might be something which can be made use of here?
did you have a real world use-case which motivated this PR?
I am asking because I see the PR is very precise and detailled. I wonder whether its worth having all the php version specific details here and whether it helps the analysis to give more useful results
|
I have no real-world case for it. PHP 8.3 changed when Float ranges fit the return type in #6492 better: a long range of floats could become |
|
please resolve conflicts and rebase |
af1075f to
4ac2edb
Compare
|
This pull request has been marked as ready for review. |
|
//cc @SanderMuller |
4ac2edb to
3768b18
Compare
| if (!$type->isConstantScalarValue()->yes()) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
I think we don't need this IF beforehand
There was a problem hiding this comment.
Dropped the check in 1e42f83. getConstantScalarValues() is empty unless every member of the type is a constant, so an empty result is enough to bail out. Without it a range(1, 10, $int) call would loop over no steps and be treated as non-throwing.
SanderMuller
left a comment
There was a problem hiding this comment.
I compared this with real PHP 8.0, 8.2, 8.3 and 8.5 on a grid of 20,480 calls. Each call has the form try { $r = range(a, b, c); } catch (\ValueError $e) {}. The boundaries are ints, floats, numeric and non-numeric strings, INF, -INF, NAN, PHP_INT_MAX, PHP_INT_MIN, 1e300 and 2 ** 53 + 1. The steps include 0, negative steps, INF, NAN, PHP_INT_MIN and 2 ** 60.
- I analysed it with a single
phpVersionfrom 80000 to 80500, and with the ranges 8.0–8.2, 8.0–8.5 and 8.2–8.3. No catch is reported as dead when one of those versions throws. This is the same with PHPStan on PHP 8.2 and on PHP 8.5. - With
phpVersion: 80500, 3,840 of the 4,412 calls that do not throw get a dead catch on PHP 8.5, and 857 on PHP 8.2. This matches the runtime verdict in your description.
One problem: with phpVersion: {min: 70400, max: 80500}, every catch is reported as dead, also around range(1, 10, 0) and range(1, $i). In the grid that is 16,121 calls that throw on at least one PHP 8 version. The extension gets maybe from throwsValueErrorForInternalFunctions(), but $functionReflection->getThrowType() is null for this version range. When the extension returns null, the call has no throw point. On 2.3.x these catches are not reported. I tried this at the four places that return the declared type:
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);With it, no catch in the grid is wrongly dead for 7.4–8.5, and 857 calls keep a dead catch, as for 8.0–8.5. testRangeThrowType still passes. ArrayChunkFunctionThrowTypeExtension on 2.3.x has the same problem: array_chunk([1], $i) gets a dead catch with this version range.
About the inline question on isConstantScalarValue(): the check is needed. For int, getConstantScalarValues() returns [], so without the check the loops do not run and the extension returns VoidType. I removed it, and range(1, $i), range($s, 'z') and range(1, 10, $f) all got a dead catch.
testRangeThrowType fails without the extension and passes with it. testRangeThrowTypeBeforePhp8 only runs on PHP 7, so I did not run it.
On 3768b18 the full test suite, make phpstan and phpcs pass. The five red CI jobs are also red on #7050 or on the 2.3.x tip. Performance: I analysed the file with the 20,480 calls 8 times on each side. 2.3.x takes 12.31 s (σ 0.37 s) and this PR 12.38 s (σ 0.02 s), which is within the noise.
|
The last change was not covered by a test |
|
You're right, and I could not write one: the change has no reachable difference. |
|
The version range problem from my review is still there on b2e8a92. With |
|
Fixed in 6914c88 with your fallback. For a version range that starts before PHP 8 the reflection of |
|
Thanks. The version range case is fixed. On 6914c88 no catch in the grid is dead around a call that throws on PHP 8, also with the 7.4 to 8.5 range. The PHP 7.4 jobs of "Tests with old PHPUnit" now fail in From the CI log, the dead catches on PHP 7.4 are now on lines 14, 23, 29, 35, 88 and 136. Lines 41, 47, 53, 60, 67, 74, 81, 95, 102, 109, 115 and 128 are no longer reported. I could not run the test on PHP 7.4 here. With On 6914c88 the full test suite on PHP 8.5, |
PhpStorm stubs declare `@throws \ValueError` on range(), so every call became an explicit throw point and a catch around a valid call was never reported as dead. The extension returns void on PHP 7, which does not throw, and on PHP 8 when the native argument types are constant numbers or strings, RangeFunctionArgumentsHelper finds that no analysed PHP version rejects them, and the range has fewer items than RANGE_SIZE_LIMIT, the HT_MAX_SIZE of a 32-bit PHP. A range whose number of items does not fit into a float keeps the throw point as well. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
A guard on both sides of PHP 8.0 leaves undecided whether range() throws a ValueError at all, and a boundary that is only partly a constant leaves the arguments undecided. Both keep the throw point.
…fore PHP 8 The reflection of range() has no @throws when the analysed PHP version range starts before PHP 8, so returning its throw type left the call without a throw point, and catches around a call that throws on PHP 8 were reported as dead.
…n PHP 7
After if (PHP_VERSION_ID < 80000) { return; } the rest of the method is analysed as PHP 8 with the reflection of PHP 7, which has no @throws. Since the ValueError fallback, the catches around the calls that throw on PHP 8 are no longer dead there.
|
Updated the PHP 7 expectations in dbdb6b9 to the lines from the CI log: 14, 23, 29, 35, 88 and 136. After the |
6914c88 to
dbdb6b9
Compare
|
//cc @SanderMuller please have another look |
SanderMuller
left a comment
There was a problem hiding this comment.
The PHP 7 expectations in dbdb6b9 are right. After the PHP_VERSION_ID < 80000 guard, the rest of doFoo() is analysed as PHP 8, so the fallback keeps the ValueError. Line 136 is dead on a PHP 7 runtime, which is correct because PHP 7 has no ValueError.
The src change is the same as on 6914c88. testRangeThrowType fails without it and passes with it. The PHP 7.4 jobs of "Tests with old PHPUnit" pass now.
The three red jobs do not come from this PR. phpstan-laravel and phpstan-dba are red on every open PR against 2.3.x. The Windows failure is ResultCacheManagerCollectedDataTest, which ab33112 on 2.3.x fixes.
Performance is the same as in my earlier measurement on 3768b18. The only src change since then is the ValueError fallback.
|
thank you! |
PhpStorm stubs declare
@throws \ValueErroronrange(), so every call is an explicit throw point and PHPStan does not report a catch around a valid call as dead:On PHP 8.0+,
range()throwsValueErrorfor:0, or one wider than the range, which covers an infinite step;PHP_INT_MIN(8.3+);PHP 7 reported these with a warning and a
falsereturn value.The extension looks at native types. It returns
VoidTypeon PHP 7, and on PHP 8 when it can prove the call succeeds:RangeFunctionArgumentsHelperfinds that no analysed PHP version rejects the arguments. It checks the PHP 8.3 rules and the older ones separately. For a range of at mostARRAY_COUNT_LIMITitems it also uses the verdict of callingrange(), but only when PHPStan itself runs on PHP 8.3 or newer;RANGE_SIZE_LIMIT, theHT_MAX_SIZEof a 32-bit PHP. A range whose number of items does not fit into a float, such asrange(-1.0e308, 1.0e308), keeps the throw point.Otherwise it keeps the declared
ValueError. Two cases stay undecided on purpose:range('1', 'a', 30)is a character range on 8.3, while 8.2 turns'1'into a number and'a'into 0, and throws because 30 exceeds the range 1..0.range(0, 2 ** 60 - 1, 2 ** 60)throws on 8.2.Since the extension reads
$scope->getPhpVersion(), I split the test data byPHP_VERSION_ID. As in the sibling tests, a second method expects every catch to be dead on PHP 7, which has noValueError, and the test expectsrange('a', 'z')to be decided for PHP 8.3 only when it runs on PHP 8.3 or newer. On PHP 7 the self-analysis reports the catch inRangeFunctionArgumentsHelper::callRange()as dead, sobuild/baseline-pre-8.0.neonignores it.RangeFunctionArgumentsHelperTestcovers the cases that depend on the PHP running PHPStan. The tests fail without the extension.#6492 changes the return type of
range()and uses the same helper. I cherry-picked the helper commits into it, so the two PRs merge in either order.