Skip to content

Support eq and notEq #126

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

Merged
merged 6 commits into from
Mar 10, 2022
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ This extension specifies types of values passed to:
* `Assert::notFalse`
* `Assert::null`
* `Assert::notNull`
* `Assert::eq`
* `Assert::notEq`
* `Assert::same`
* `Assert::notSame`
* `Assert::greaterThan`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.1 || ^8.0",
"phpstan/phpstan": "^1.4.8"
"phpstan/phpstan": "^1.4.9"
},
"require-dev": {
"nikic/php-parser": "^4.13.0",
Expand Down
9 changes: 9 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ private static function getExpressionResolvers(): array
new ConstFetch(new Name('null'))
);
},
'eq' => static function (Scope $scope, Arg $value, Arg $value2): Expr {
return new Equal(
$value->value,
$value2->value
);
},
'notEq' => static function (Scope $scope, Arg $value, Arg $value2): Expr {
return new BooleanNot(self::$resolvers['eq']($scope, $value, $value2));
},
'same' => static function (Scope $scope, Arg $value1, Arg $value2): Expr {
return new Identical(
$value1->value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,84 @@ public function testExtension(): void
]);
}

public function testEqNotEq(): void
{
$this->analyse([__DIR__ . '/data/impossible-check-eq-not-eq.php'], [
[
'Call to static method Webmozart\Assert\Assert::eq() with stdClass and stdClass will always evaluate to true.',
14,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with stdClass and stdClass will always evaluate to false.',
15,
],
/*[
'Call to static method Webmozart\Assert\Assert::eq() with 1 and \'1\' will always evaluate to true.',
37,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with 1 and \'1\' will always evaluate to false.',
38,
],*/
[
'Call to static method Webmozart\Assert\Assert::eq() with 1 and true will always evaluate to true.',
39,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with 1 and true will always evaluate to false.',
40,
],
[
'Call to static method Webmozart\Assert\Assert::eq() with \'php\' and true will always evaluate to true.',
41,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with \'php\' and true will always evaluate to false.',
42,
],
[
'Call to static method Webmozart\Assert\Assert::eq() with \'\' and false will always evaluate to true.',
43,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with \'\' and false will always evaluate to false.',
44,
],
[
'Call to static method Webmozart\Assert\Assert::eq() with 1 and 1 will always evaluate to true.',
46,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with 1 and 1 will always evaluate to false.',
47,
],
[
'Call to static method Webmozart\Assert\Assert::eq() with true and true will always evaluate to true.',
48,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with true and true will always evaluate to false.',
49,
],
[
'Call to static method Webmozart\Assert\Assert::eq() with \'php\' and \'php\' will always evaluate to true.',
50,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with \'php\' and \'php\' will always evaluate to false.',
51,
],
[
'Call to static method Webmozart\Assert\Assert::eq() with stdClass and null will always evaluate to false.',
61,
],
[
'Call to static method Webmozart\Assert\Assert::notEq() with stdClass and null will always evaluate to true.',
62,
],
]);
}

public function testBug8(): void
{
$this->analyse([__DIR__ . '/data/bug-8.php'], [
Expand Down
33 changes: 33 additions & 0 deletions tests/Type/WebMozartAssert/data/comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ public function notNull(?int $a): void
\PHPStan\Testing\assertType('int', $a);
}

/**
* @param non-empty-string $b2
*/
public function eq(?bool $a, string $b1, string $b2, $c, ?bool $d): void
{
Assert::eq($a, null);
\PHPStan\Testing\assertType('false|null', $a);

Assert::eq($b1, $b2);
\PHPStan\Testing\assertType('non-empty-string', $b1);

Assert::eq($c, false);
\PHPStan\Testing\assertType('0|0.0|\'\'|\'0\'|array{}|false|null', $c);

Assert::nullOrEq($d, true);
\PHPStan\Testing\assertType('true|null', $d);
}

public function notEq(?bool $a, string $b, $c, ?bool $d): void
{
Assert::notEq($a, null);
\PHPStan\Testing\assertType('true', $a);

Assert::notEq($b, '');
\PHPStan\Testing\assertType('non-empty-string', $b);

Assert::notEq($c, true);
\PHPStan\Testing\assertType('0|0.0|\'\'|\'0\'|array{}|false|null', $c);

Assert::nullOrNotEq($d, true);
\PHPStan\Testing\assertType('false|null', $d);
}

public function same($a, $b): void
{
Assert::same($a, 1);
Expand Down
70 changes: 70 additions & 0 deletions tests/Type/WebMozartAssert/data/impossible-check-eq-not-eq.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php declare(strict_types=1);

namespace WebmozartAssertImpossibleCheckEqNotEq;

use DateTimeInterface;
use stdClass;
use Webmozart\Assert\Assert;

class ImpossibleCheckEqNotEq
{

public function sameInstancesAreAlwaysEqual(stdClass $a, stdClass $b): void
{
Assert::eq($a, $a); // will always evaluate to true
Assert::notEq($b, $b); // will always evaluate to false
}

public function instancesOfTheSameTypeAreNotIdenticalButCouldBeEqual(stdClass $a, stdClass $b, stdClass $c, stdClass $d): void
{
Assert::eq($a, $b);
Assert::notEq($c, $d);

Assert::eq(self::createStdClass(), self::createStdClass());
Assert::notEq(self::createStdClass(), self::createStdClass());
}

public function looseVariableComparisonsAreNotSupported(stdClass $a, DateTimeInterface $b, stdClass $c, DateTimeInterface $d, string $e, int $f, string $g, int $h): void
{
Assert::eq($a, $b);
Assert::notEq($c, $d);
Assert::eq($e, $f);
Assert::notEq($g, $h);
}

public function constantComparisons(): void
{
Assert::eq(1, '1'); // will always evaluate to true
Assert::notEq(1, '1'); // will always evaluate to false
Assert::eq(1, true); // will always evaluate to true
Assert::notEq(1, true); // will always evaluate to false
Assert::eq('php', true); // will always evaluate to true
Assert::notEq('php', true); // will always evaluate to false
Assert::eq('', false); // will always evaluate to true
Assert::notEq('', false); // will always evaluate to false

Assert::eq(1, 1); // will always evaluate to true
Assert::notEq(1, 1); // will always evaluate to false
Assert::eq(true, true); // will always evaluate to true
Assert::notEq(true, true); // will always evaluate to false
Assert::eq('php', 'php'); // will always evaluate to true
Assert::notEq('php', 'php'); // will always evaluate to false
}

public function instancesOfDifferentTypesAreNeverEqual(stdClass $a, stdClass $b, stdClass $c, stdClass $d, stdClass $e, stdClass $f): void
{
Assert::eq($a, new stdClass());
Assert::eq($b, self::createStdClass());
Assert::notEq($c, new stdClass());
Assert::notEq($d, self::createStdClass());

Assert::eq($e, null); // will always evaluate to false
Assert::notEq($f, null); // will always evaluate to true
}

public static function createStdClass(): stdClass
{
return new stdClass();
}

}