Provide refined type for the int range returned by intdiv
Author: stofCreated Oct 1, 2026Updated Oct 1, 2026
Labelsfeature-request
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 https://github.com/doctrine/orm/pull/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(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 cases
Did PHPStan help you today? Did it make you happy in any way?
No response
Source: phpstan/phpstan