Skip to content

Commit f5b7eb6

Browse files
committed
Implement assertEmpty extension
1 parent b808cb8 commit f5b7eb6

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PHPStan\Type\PHPUnit\Assert;
44

55
use Closure;
6+
use Countable;
7+
use EmptyIterator;
68
use PhpParser\Node\Arg;
79
use PhpParser\Node\Expr;
810
use PhpParser\Node\Expr\BinaryOp\Identical;
@@ -156,6 +158,15 @@ private static function getExpressionResolvers(): array
156158
new ConstFetch(new Name('null'))
157159
);
158160
},
161+
'Empty' => static function (Scope $scope, Arg $actual): Expr\BinaryOp\BooleanOr {
162+
return new Expr\BinaryOp\BooleanOr(
163+
new Instanceof_($actual->value, new Name(EmptyIterator::class)),
164+
new Expr\BinaryOp\BooleanOr(
165+
new Instanceof_($actual->value, new Name(Countable::class)),
166+
new Expr\Empty_($actual->value)
167+
)
168+
);
169+
},
159170
'IsArray' => static function (Scope $scope, Arg $actual): FuncCall {
160171
return new FuncCall(new Name('is_array'), [$actual]);
161172
},

tests/Type/PHPUnit/data/assert-function.php

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use function PHPStan\Testing\assertType;
66
use function PHPUnit\Framework\assertArrayHasKey;
7+
use function PHPUnit\Framework\assertEmpty;
78
use function PHPUnit\Framework\assertInstanceOf;
89
use function PHPUnit\Framework\assertObjectHasAttribute;
910

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

40+
public function testEmpty($a): void
41+
{
42+
assertEmpty($a);
43+
assertType("0|0.0|''|'0'|array{}|Countable|EmptyIterator|false|null", $a);
44+
}
45+
3946
}

0 commit comments

Comments
 (0)