Skip to content

Commit

Permalink
Support isNotA
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Feb 27, 2022
1 parent 3937f41 commit 055f798
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ This extension specifies types of values passed to:
* `Assert::resource`
* `Assert::isCallable`
* `Assert::isArray`
* `Assert::isTraversable` (deprecated, use `isIterable` or `isInstanceOf` instead)
* `Assert::isIterable`
* `Assert::isCountable`
* `Assert::isInstanceOf`
* `Assert::isInstanceOfAny`
* `Assert::notInstanceOf`
* `Assert::isAOf`
* `Assert::isAnyOf`
* `Assert::isNotA`
* `Assert::subclassOf`
* `Assert::true`
* `Assert::false`
Expand Down
6 changes: 6 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ private static function getExpressionResolvers(): array
[$value]
);
},
'isTraversable' => static function (Scope $scope, Arg $value): Expr {
return self::$resolvers['isIterable']($scope, $value);
},
'isIterable' => static function (Scope $scope, Arg $expr): Expr {
return new BooleanOr(
new FuncCall(
Expand Down Expand Up @@ -414,6 +417,9 @@ private static function getExpressionResolvers(): array
'isAnyOf' => static function (Scope $scope, Arg $value, Arg $classes): ?Expr {
return self::buildAnyOfExpr($scope, $value, $classes, self::$resolvers['isAOf']);
},
'isNotA' => static function (Scope $scope, Arg $value, Arg $class): Expr {
return new BooleanNot(self::$resolvers['isAOf']($scope, $value, $class));
},
'implementsInterface' => static function (Scope $scope, Arg $expr, Arg $class): ?Expr {
$classType = $scope->getType($class->value);
if (!$classType instanceof ConstantStringType) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Type/WebMozartAssert/data/type.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ public function isArray($a, $b): void
\PHPStan\Testing\assertType('array|null', $b);
}

public function isTraversable($a, $b): void
{
Assert::isTraversable($a);
\PHPStan\Testing\assertType('array|Traversable', $a);

Assert::nullOrIsTraversable($b);
\PHPStan\Testing\assertType('array|Traversable|null', $b);
}

public function isIterable($a, $b): void
{
Assert::isIterable($a);
Expand Down Expand Up @@ -249,6 +258,23 @@ public function isAnyOf($a, $b): void
\PHPStan\Testing\assertType('\'PHPStan\\\Type\\\WebMozartAssert\\\Bar\'', Bar::class);
}

/**
* @param DateTimeImmutable|stdClass $a
* @param class-string<DateTimeImmutable>|class-string<stdClass> $b
* @param DateTimeImmutable|stdClass|null $c
*/
public function isNotA(object $a, string $b, ?object $c): void
{
Assert::isNotA($a, stdClass::class);
\PHPStan\Testing\assertType('DateTimeImmutable', $a);

Assert::isNotA($b, stdClass::class);
\PHPStan\Testing\assertType('class-string<DateTimeImmutable>', $b); // https://github.com/phpstan/phpstan/issues/6704

Assert::nullOrIsNotA($c, stdClass::class);
\PHPStan\Testing\assertType('DateTimeImmutable|null', $c);
}

public function isArrayAccessible($a, $b): void
{
Assert::isArrayAccessible($a);
Expand Down

0 comments on commit 055f798

Please sign in to comment.