Skip to content

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

Open
harrismalik wants to merge 1 commit into
phpstan:2.1.xfrom
harrismalik:assert-or-equal-narrowing
Open

harrismalik wants to merge 1 commit into
phpstan:2.1.xfrom
harrismalik:assert-or-equal-narrowing

Conversation

@harrismalik

Copy link
Copy Markdown

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

),
'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

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