Skip to content

Conversation

jlherren
Copy link
Contributor

Note that this PR has a side-effect that might need to be addressed, consider this code:

$array = rand() === 0 ? hash_algos() : false;
for ($i = 0; $i < count($array); $i++) {
	echo $array[$i] . "\n"; // <-- this line
}

The change in output is as follows:

- Cannot access offset int on array|false.
+ Cannot access offset int<min, 9223372036854775806> on array|false.

While technically correct, this is not very pretty.

Perhaps this can be fixed by adjusting how IntegerRangeType is represented. Currently, int<0, max> (return value of count()) is internally represented by min=0, max=PHP_INT_MAX, which is technically a bounded interval. If it were to use min=0, max=null instead, then the intended meaning of an open interval can be represented more accurately and the new code in this PR could make use of that information. If this sounds good I'll have a go at it.

@ondrejmirtes
Copy link
Member

Yes, please do the min=0, max=null refactoring first. We should honor the usual math rules that INF minus 1 is still INF :) So int<min, 9223372036854775806> should really be just an int.

@ondrejmirtes ondrejmirtes force-pushed the master branch 3 times, most recently from d45166a to 3526237 Compare December 12, 2020 10:56
@jlherren jlherren force-pushed the improve-comparisons branch from 7c9f17c to 49cb8aa Compare December 23, 2020 09:28
@jlherren
Copy link
Contributor Author

I rebased this branch and the issue I mentioned is now fixed.

@ondrejmirtes
Copy link
Member

Linking the fixed issue: phpstan/phpstan#577

I'm gonna review this once the time allows, hopefull soon :)

@ondrejmirtes
Copy link
Member

Yes, please do. Can you also take a look at #416 - does it make sense to you?

@ondrejmirtes
Copy link
Member

Thank you!

@ondrejmirtes ondrejmirtes merged commit 377e49c into phpstan:master Jan 4, 2021
@jlherren jlherren deleted the improve-comparisons branch January 5, 2021 07:56
@jlherren jlherren restored the improve-comparisons branch January 5, 2021 07:57
@jlherren jlherren deleted the improve-comparisons branch January 5, 2021 07:57
@jlherren jlherren restored the improve-comparisons branch January 5, 2021 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants