Skip to content

Commit 11cc4ba

Browse files
herndlmondrejmirtes
authored andcommitted
Support isNotA
1 parent 5b5916d commit 11cc4ba

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This extension specifies types of values passed to:
5050
* `Assert::notInstanceOf`
5151
* `Assert::isAOf`
5252
* `Assert::isAnyOf`
53+
* `Assert::isNotA`
5354
* `Assert::subclassOf`
5455
* `Assert::true`
5556
* `Assert::false`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+3
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ private static function getExpressionResolvers(): array
417417
'isAnyOf' => static function (Scope $scope, Arg $value, Arg $classes): ?Expr {
418418
return self::buildAnyOfExpr($scope, $value, $classes, self::$resolvers['isAOf']);
419419
},
420+
'isNotA' => static function (Scope $scope, Arg $value, Arg $class): Expr {
421+
return new BooleanNot(self::$resolvers['isAOf']($scope, $value, $class));
422+
},
420423
'implementsInterface' => static function (Scope $scope, Arg $expr, Arg $class): ?Expr {
421424
$classType = $scope->getType($class->value);
422425
if (!$classType instanceof ConstantStringType) {

tests/Type/WebMozartAssert/data/type.php

+23
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,29 @@ public function isAnyOf($a, $b): void
258258
\PHPStan\Testing\assertType('\'PHPStan\\\Type\\\WebMozartAssert\\\Bar\'', Bar::class);
259259
}
260260

261+
/**
262+
* @param DateTimeImmutable|stdClass $a
263+
* @param class-string<DateTimeImmutable>|class-string<stdClass> $b
264+
* @param DateTimeImmutable|stdClass|null $c
265+
*/
266+
public function isNotA(object $a, string $b, ?object $c): void
267+
{
268+
Assert::isNotA($a, stdClass::class);
269+
\PHPStan\Testing\assertType('DateTimeImmutable', $a);
270+
271+
Assert::isNotA($b, stdClass::class);
272+
\PHPStan\Testing\assertType('class-string<DateTimeImmutable>', $b);
273+
274+
Assert::nullOrIsNotA($c, stdClass::class);
275+
\PHPStan\Testing\assertType('DateTimeImmutable|null', $c);
276+
277+
Assert::isNotA(Foo::class, stdClass::class);
278+
\PHPStan\Testing\assertType('\'PHPStan\\\Type\\\WebMozartAssert\\\Foo\'', Foo::class);
279+
280+
Assert::isNotA(Bar::class, stdClass::class);
281+
\PHPStan\Testing\assertType('*NEVER*', Bar::class);
282+
}
283+
261284
public function isArrayAccessible($a, $b): void
262285
{
263286
Assert::isArrayAccessible($a);

0 commit comments

Comments
 (0)