Skip to content

Implement assertGreaterThanOrEqual() and assertLessThanOrEqual() type narrowing - #359

Merged
staabm merged 2 commits into
phpstan:2.1.xfrom
harrismalik:assert-or-equal-narrowing
Nov 24, 2026
Merged

staabm merged 2 commits into
phpstan:2.1.xfrom
harrismalik:assert-or-equal-narrowing

Conversation

@harrismalik

Copy link
Copy Markdown
Contributor

Closes #350


#351 left out the *OrEqual variants because PHPUnit checks equality with its own comparator. As proposed in #350 (comment), this narrows only when both arguments are int or float. With any other type on either side (numeric strings, nullable, mixed, arrays, objects) nothing is narrowed.

One difference remains: where PHPUnit's comparator treats INF and -INF as equal (seen on 9.6.39 and 10.5.67), assertGreaterThanOrEqual(INF, -INF) and assertLessThanOrEqual(-INF, INF) pass but are reported as always false.

Comment on lines +81 to +92
/**
* @param 1|5|9.5 $a
* @param 1.5|5|9 $c
*/
public function integersAndFloatsOrEqual($a, $c): void
{
self::assertGreaterThanOrEqual(5.0, $a);
assertType('5|9.5', $a);

self::assertLessThanOrEqual(5.0, $c);
assertType('1.5|5', $c);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we support float we should also test with INF and NAN

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests in 00749f3: INF and -INF against int and 1.5|5, and NAN against int ranges, each as expected and as actual. Float parameters and plain int against NAN are left out, since PHPStan 2.3.0 and 2.3.7 infer different types for the first and both infer a type that is too wide for the second. PHPUnit 9.6 and 10.5 treat INF and -INF as equal, so assertGreaterThanOrEqual(INF, $float) passes for -INF while PHPStan 2.3.7 narrows $float to INF. Should the guard skip narrowing when both sides can be infinite?

),
'GreaterThanOrEqual' => static function (Scope $scope, Arg $expected, Arg $actual): ?Expr\BinaryOp\GreaterOrEqual {
if (!self::isIntOrFloat($scope, $expected) || !self::isIntOrFloat($scope, $actual)) {
return null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a test with values different from int|float

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added notNarrowedActualTypesOrEqual (string, numeric string, array, object and mixed) and notNarrowedExpectedTypesOrEqual (array and mixed) in 00749f3. A nullable int as actual and '5' as expected were already covered by notNarrowedActualOrEqual and notNarrowedExpectedOrEqual.

@staabm
staabm merged commit 056385f into phpstan:2.1.x Nov 24, 2026
114 checks passed
@staabm

staabm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thank you!

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.

assertGreaterThan() does not narrow array

2 participants