Feature request
https://phpstan.org/r/98ff7c32-c78d-4f60-b7fa-acac1bbcf1ac
Computing intdiv on values currently only returns int as type in phpstan. However, we could have more precise types. A typical pattern where this happens is when dealing with pagination settings. The code snippet shared above is taken almost verbatim from doctrine/orm#12643 ("almost" because I reduced it to the bare minimum reproducer).
The mathematical rules give us those guarantees:
- the result is 0 iff the first argument is 0
- if both operands are the same sign, the result is positive
- if both operands are of opposite signs, the result is negative
This could allow a return type extension to inspect the int range of operands so that the return type is one of those:
0 (if num1 is known to be 0)
int<1, max> (if num1 is not 0 and both operands are ranges that are of a same known sign)
int<min, -1> (if num1 is not 0 and both operands are ranges that are of a opposite known sign)
int<0, max> (if num1 can be 0 and both operands are ranges that are of a same known sign for non-zero values of num1)
int<min, 0> (if num1 can be 0 and both operands are ranges that are of a opposite known sign for non-zero values of num1)
int<min, -1>|int<1, max> when num1 cannot be 0 without guarantees on the signs of operands
int for other cases
Did PHPStan help you today? Did it make you happy in any way?
No response
Feature request
https://phpstan.org/r/98ff7c32-c78d-4f60-b7fa-acac1bbcf1ac
Computing
intdivon values currently only returnsintas type in phpstan. However, we could have more precise types. A typical pattern where this happens is when dealing with pagination settings. The code snippet shared above is taken almost verbatim from doctrine/orm#12643 ("almost" because I reduced it to the bare minimum reproducer).The mathematical rules give us those guarantees:
This could allow a return type extension to inspect the int range of operands so that the return type is one of those:
0(ifnum1is known to be0)int<1, max>(ifnum1is not0and both operands are ranges that are of a same known sign)int<min, -1>(ifnum1is not0and both operands are ranges that are of a opposite known sign)int<0, max>(ifnum1can be0and both operands are ranges that are of a same known sign for non-zero values ofnum1)int<min, 0>(ifnum1can be0and both operands are ranges that are of a opposite known sign for non-zero values ofnum1)int<min, -1>|int<1, max>whennum1cannot be0without guarantees on the signs of operandsintfor other casesDid PHPStan help you today? Did it make you happy in any way?
No response