Skip to content

Fix non-integer range comparisons #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -485,31 +486,56 @@ private static function getExpressionResolvers(): array
$value2->value
);
},
'greaterThan' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
'greaterThan' => static function (Scope $scope, Arg $value, Arg $limit): ?Expr {
$valueType = $scope->getType($value->value);
if ((new IntegerType())->isSuperTypeOf($valueType)->no()) {
return null;
}

return new Greater(
$value->value,
$limit->value
);
},
'greaterThanEq' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
'greaterThanEq' => static function (Scope $scope, Arg $value, Arg $limit): ?Expr {
$valueType = $scope->getType($value->value);
if ((new IntegerType())->isSuperTypeOf($valueType)->no()) {
return null;
}

return new GreaterOrEqual(
$value->value,
$limit->value
);
},
'lessThan' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
'lessThan' => static function (Scope $scope, Arg $value, Arg $limit): ?Expr {
$valueType = $scope->getType($value->value);
if ((new IntegerType())->isSuperTypeOf($valueType)->no()) {
return null;
}

return new Smaller(
$value->value,
$limit->value
);
},
'lessThanEq' => static function (Scope $scope, Arg $value, Arg $limit): Expr {
'lessThanEq' => static function (Scope $scope, Arg $value, Arg $limit): ?Expr {
$valueType = $scope->getType($value->value);
if ((new IntegerType())->isSuperTypeOf($valueType)->no()) {
return null;
}

return new SmallerOrEqual(
$value->value,
$limit->value
);
},
'range' => static function (Scope $scope, Arg $value, Arg $min, Arg $max): Expr {
'range' => static function (Scope $scope, Arg $value, Arg $min, Arg $max): ?Expr {
$valueType = $scope->getType($value->value);
if ((new IntegerType())->isSuperTypeOf($valueType)->no()) {
Copy link
Member

Choose a reason for hiding this comment

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

What about mixed or float|int? Do they demonstrate this bug or not?

Copy link
Contributor Author

@herndlm herndlm Mar 5, 2022

Choose a reason for hiding this comment

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

I assume you're trying to find out if I should use !(new IntegerType())->isSuperTypeOf($valueType)->yes() instead? :)
Apparently they are not affected, but I need to be able to use it with int|null to not break e.g. nullOrRange. If that makes sense. I'm not sure what the best solution for that is. I'm not sure if I fully understood already why it doesn't work, I need to take a closer look

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aaah, I understand why. At that point, when the expression is built, it is still the int|null union of course..

return null;
}

return new BooleanAnd(
new GreaterOrEqual(
$value->value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public function testBug85(): void
$this->analyse([__DIR__ . '/data/bug-85.php'], []);
}

public function testBug118(): void
{
$this->analyse([__DIR__ . '/data/bug-118.php'], []);
}

public function testBug119(): void
{
$this->analyse([__DIR__ . '/data/bug-119.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
Expand Down
14 changes: 14 additions & 0 deletions tests/Type/WebMozartAssert/data/bug-118.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace WebmozartAssertBug118;

use DateTime;
use Webmozart\Assert\Assert;

function test(float $a, DateTime $b): void
{
Assert::range($a, 0, 1);
Assert::range($b, 123456789, 9876543321);
}
17 changes: 17 additions & 0 deletions tests/Type/WebMozartAssert/data/bug-119.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace WebmozartAssertBug119;

use DateTime;
use Webmozart\Assert\Assert;

function test(float $a, float $b, float $c, float $d, DateTime $e): void
{
Assert::greaterThan($a, 0);
Assert::greaterThanEq($b, 0);
Assert::lessThan($c, 0);
Assert::lessThanEq($d, 0);
Assert::greaterThanEq($e, 639828000);
}