Skip to content

Commit 9eda0cb

Browse files
committed
Support eq and notEq
1 parent 9424683 commit 9eda0cb

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ This extension specifies types of values passed to:
5757
* `Assert::notFalse`
5858
* `Assert::null`
5959
* `Assert::notNull`
60+
* `Assert::eq`
61+
* `Assert::notEq`
6062
* `Assert::same`
6163
* `Assert::notSame`
6264
* `Assert::greaterThan`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,15 @@ private static function getExpressionResolvers(): array
483483
new ConstFetch(new Name('null'))
484484
);
485485
},
486+
'eq' => static function (Scope $scope, Arg $value, Arg $value2): Expr {
487+
return new Equal(
488+
$value->value,
489+
$value2->value
490+
);
491+
},
492+
'notEq' => static function (Scope $scope, Arg $value, Arg $value2): Expr {
493+
return new BooleanNot(self::$resolvers['eq']($scope, $value, $value2));
494+
},
486495
'same' => static function (Scope $scope, Arg $value1, Arg $value2): Expr {
487496
return new Identical(
488497
$value1->value,

tests/Type/WebMozartAssert/data/comparison.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,39 @@ public function notNull(?int $a): void
4343
\PHPStan\Testing\assertType('int', $a);
4444
}
4545

46+
/**
47+
* @param non-empty-string $b2
48+
*/
49+
public function eq(?bool $a, string $b1, string $b2, $c, ?bool $d): void
50+
{
51+
Assert::eq($a, null);
52+
\PHPStan\Testing\assertType('false|null', $a);
53+
54+
Assert::eq($b1, $b2);
55+
\PHPStan\Testing\assertType('non-empty-string', $b1);
56+
57+
Assert::eq($c, false);
58+
\PHPStan\Testing\assertType('0|0.0|\'\'|\'0\'|array{}|false|null', $c);
59+
60+
Assert::nullOrEq($d, true);
61+
\PHPStan\Testing\assertType('true|null', $d);
62+
}
63+
64+
public function notEq(?bool $a, string $b, $c, ?bool $d): void
65+
{
66+
Assert::notEq($a, null);
67+
\PHPStan\Testing\assertType('true', $a);
68+
69+
Assert::notEq($b, '');
70+
\PHPStan\Testing\assertType('non-empty-string', $b);
71+
72+
Assert::notEq($c, true);
73+
\PHPStan\Testing\assertType('0|0.0|\'\'|\'0\'|array{}|false|null', $c);
74+
75+
Assert::nullOrNotEq($d, true);
76+
\PHPStan\Testing\assertType('false|null', $d);
77+
}
78+
4679
public function same($a, $b): void
4780
{
4881
Assert::same($a, 1);

0 commit comments

Comments
 (0)