Skip to content

Commit c293bf4

Browse files
authored
Support eq and notEq
1 parent 426488d commit c293bf4

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

README.md

+2
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

+9
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/ImpossibleCheckTypeMethodCallRuleTest.php

+78
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,84 @@ public function testExtension(): void
6363
]);
6464
}
6565

66+
public function testEqNotEq(): void
67+
{
68+
$this->analyse([__DIR__ . '/data/impossible-check-eq-not-eq.php'], [
69+
[
70+
'Call to static method Webmozart\Assert\Assert::eq() with stdClass and stdClass will always evaluate to true.',
71+
14,
72+
],
73+
[
74+
'Call to static method Webmozart\Assert\Assert::notEq() with stdClass and stdClass will always evaluate to false.',
75+
15,
76+
],
77+
/*[
78+
'Call to static method Webmozart\Assert\Assert::eq() with 1 and \'1\' will always evaluate to true.',
79+
37,
80+
],
81+
[
82+
'Call to static method Webmozart\Assert\Assert::notEq() with 1 and \'1\' will always evaluate to false.',
83+
38,
84+
],*/
85+
[
86+
'Call to static method Webmozart\Assert\Assert::eq() with 1 and true will always evaluate to true.',
87+
39,
88+
],
89+
[
90+
'Call to static method Webmozart\Assert\Assert::notEq() with 1 and true will always evaluate to false.',
91+
40,
92+
],
93+
[
94+
'Call to static method Webmozart\Assert\Assert::eq() with \'php\' and true will always evaluate to true.',
95+
41,
96+
],
97+
[
98+
'Call to static method Webmozart\Assert\Assert::notEq() with \'php\' and true will always evaluate to false.',
99+
42,
100+
],
101+
[
102+
'Call to static method Webmozart\Assert\Assert::eq() with \'\' and false will always evaluate to true.',
103+
43,
104+
],
105+
[
106+
'Call to static method Webmozart\Assert\Assert::notEq() with \'\' and false will always evaluate to false.',
107+
44,
108+
],
109+
[
110+
'Call to static method Webmozart\Assert\Assert::eq() with 1 and 1 will always evaluate to true.',
111+
46,
112+
],
113+
[
114+
'Call to static method Webmozart\Assert\Assert::notEq() with 1 and 1 will always evaluate to false.',
115+
47,
116+
],
117+
[
118+
'Call to static method Webmozart\Assert\Assert::eq() with true and true will always evaluate to true.',
119+
48,
120+
],
121+
[
122+
'Call to static method Webmozart\Assert\Assert::notEq() with true and true will always evaluate to false.',
123+
49,
124+
],
125+
[
126+
'Call to static method Webmozart\Assert\Assert::eq() with \'php\' and \'php\' will always evaluate to true.',
127+
50,
128+
],
129+
[
130+
'Call to static method Webmozart\Assert\Assert::notEq() with \'php\' and \'php\' will always evaluate to false.',
131+
51,
132+
],
133+
[
134+
'Call to static method Webmozart\Assert\Assert::eq() with stdClass and null will always evaluate to false.',
135+
61,
136+
],
137+
[
138+
'Call to static method Webmozart\Assert\Assert::notEq() with stdClass and null will always evaluate to true.',
139+
62,
140+
],
141+
]);
142+
}
143+
66144
public function testBug8(): void
67145
{
68146
$this->analyse([__DIR__ . '/data/bug-8.php'], [

tests/Type/WebMozartAssert/data/comparison.php

+33
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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace WebmozartAssertImpossibleCheckEqNotEq;
4+
5+
use DateTimeInterface;
6+
use stdClass;
7+
use Webmozart\Assert\Assert;
8+
9+
class ImpossibleCheckEqNotEq
10+
{
11+
12+
public function sameInstancesAreAlwaysEqual(stdClass $a, stdClass $b): void
13+
{
14+
Assert::eq($a, $a); // will always evaluate to true
15+
Assert::notEq($b, $b); // will always evaluate to false
16+
}
17+
18+
public function instancesOfTheSameTypeAreNotIdenticalButCouldBeEqual(stdClass $a, stdClass $b, stdClass $c, stdClass $d): void
19+
{
20+
Assert::eq($a, $b);
21+
Assert::notEq($c, $d);
22+
23+
Assert::eq(self::createStdClass(), self::createStdClass());
24+
Assert::notEq(self::createStdClass(), self::createStdClass());
25+
}
26+
27+
public function looseVariableComparisonsAreNotSupported(stdClass $a, DateTimeInterface $b, stdClass $c, DateTimeInterface $d, string $e, int $f, string $g, int $h): void
28+
{
29+
Assert::eq($a, $b);
30+
Assert::notEq($c, $d);
31+
Assert::eq($e, $f);
32+
Assert::notEq($g, $h);
33+
}
34+
35+
public function constantComparisons(): void
36+
{
37+
Assert::eq(1, '1'); // will always evaluate to true
38+
Assert::notEq(1, '1'); // will always evaluate to false
39+
Assert::eq(1, true); // will always evaluate to true
40+
Assert::notEq(1, true); // will always evaluate to false
41+
Assert::eq('php', true); // will always evaluate to true
42+
Assert::notEq('php', true); // will always evaluate to false
43+
Assert::eq('', false); // will always evaluate to true
44+
Assert::notEq('', false); // will always evaluate to false
45+
46+
Assert::eq(1, 1); // will always evaluate to true
47+
Assert::notEq(1, 1); // will always evaluate to false
48+
Assert::eq(true, true); // will always evaluate to true
49+
Assert::notEq(true, true); // will always evaluate to false
50+
Assert::eq('php', 'php'); // will always evaluate to true
51+
Assert::notEq('php', 'php'); // will always evaluate to false
52+
}
53+
54+
public function instancesOfDifferentTypesAreNeverEqual(stdClass $a, stdClass $b, stdClass $c, stdClass $d, stdClass $e, stdClass $f): void
55+
{
56+
Assert::eq($a, new stdClass());
57+
Assert::eq($b, self::createStdClass());
58+
Assert::notEq($c, new stdClass());
59+
Assert::notEq($d, self::createStdClass());
60+
61+
Assert::eq($e, null); // will always evaluate to false
62+
Assert::notEq($f, null); // will always evaluate to true
63+
}
64+
65+
public static function createStdClass(): stdClass
66+
{
67+
return new stdClass();
68+
}
69+
70+
}

0 commit comments

Comments
 (0)