Skip to content

Commit

Permalink
Implement assertEmpty extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 24, 2022
1 parent b808cb8 commit f5b7eb6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\PHPUnit\Assert;

use Closure;
use Countable;
use EmptyIterator;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
Expand Down Expand Up @@ -156,6 +158,15 @@ private static function getExpressionResolvers(): array
new ConstFetch(new Name('null'))
);
},
'Empty' => static function (Scope $scope, Arg $actual): Expr\BinaryOp\BooleanOr {
return new Expr\BinaryOp\BooleanOr(
new Instanceof_($actual->value, new Name(EmptyIterator::class)),
new Expr\BinaryOp\BooleanOr(
new Instanceof_($actual->value, new Name(Countable::class)),
new Expr\Empty_($actual->value)
)
);
},
'IsArray' => static function (Scope $scope, Arg $actual): FuncCall {
return new FuncCall(new Name('is_array'), [$actual]);
},
Expand Down
7 changes: 7 additions & 0 deletions tests/Type/PHPUnit/data/assert-function.php
Expand Up @@ -4,6 +4,7 @@

use function PHPStan\Testing\assertType;
use function PHPUnit\Framework\assertArrayHasKey;
use function PHPUnit\Framework\assertEmpty;
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertObjectHasAttribute;

Expand Down Expand Up @@ -36,4 +37,10 @@ public function objectHasAttribute(object $a): void
assertType("object&hasProperty(property)", $a);
}

public function testEmpty($a): void
{
assertEmpty($a);
assertType("0|0.0|''|'0'|array{}|Countable|EmptyIterator|false|null", $a);
}

}

0 comments on commit f5b7eb6

Please sign in to comment.