Repository navigation
Implement assertGreaterThanOrEqual() and assertLessThanOrEqual() type narrowing - #359
Open
harrismalik wants to merge 1 commit into
Open
harrismalik wants to merge 1 commit into
harrismalik wants to merge 1 commit into
Conversation
staabm
reviewed
Nov 24, 2026
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); | ||
| } |
Contributor
There was a problem hiding this comment.
since we support float we should also test with INF and NAN
staabm
reviewed
Nov 24, 2026
| ), | ||
| 'GreaterThanOrEqual' => static function (Scope $scope, Arg $expected, Arg $actual): ?Expr\BinaryOp\GreaterOrEqual { | ||
| if (!self::isIntOrFloat($scope, $expected) || !self::isIntOrFloat($scope, $actual)) { | ||
| return null; |
Contributor
There was a problem hiding this comment.
needs a test with values different from int|float
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #350
#351 left out the
*OrEqualvariants 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
INFand-INFas equal (seen on 9.6.39 and 10.5.67),assertGreaterThanOrEqual(INF, -INF)andassertLessThanOrEqual(-INF, INF)pass but are reported as always false.