Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a00dad3

Browse files
villfaondrejmirtes
authored andcommittedNov 29, 2021
Add support for count
1 parent 2cd9a91 commit a00dad3

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This extension specifies types of values passed to:
5656
* `Assert::classExists`
5757
* `Assert::interfaceExists`
5858
* `Assert::validArrayKey`
59+
* `Assert::count`
5960
* `Assert::minCount`
6061
* `Assert::inArray`
6162
* `Assert::oneOf`

‎src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@ private static function getExpressionResolvers(): array
395395
[$class]
396396
);
397397
},
398+
'count' => function (Scope $scope, Arg $array, Arg $number): \PhpParser\Node\Expr {
399+
return new \PhpParser\Node\Expr\BinaryOp\Identical(
400+
new \PhpParser\Node\Expr\FuncCall(
401+
new \PhpParser\Node\Name('count'),
402+
[$array]
403+
),
404+
$number->value
405+
);
406+
},
398407
'minCount' => function (Scope $scope, Arg $array, Arg $number): \PhpParser\Node\Expr {
399408
return new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
400409
new \PhpParser\Node\Expr\FuncCall(

‎tests/Type/WebMozartAssert/data/data.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
174174

175175
Assert::interfaceExists($ag);
176176
\PHPStan\Testing\assertType('class-string', $ag);
177+
178+
/** @var int[] $at */
179+
$at = doFoo();
180+
Assert::count($at, 1);
181+
$au = array_pop($at);
182+
$av = array_pop($at);
183+
\PHPStan\Testing\assertType('int', $au);
184+
\PHPStan\Testing\assertType('int|null', $av);
177185
}
178186

179187
}

0 commit comments

Comments
 (0)
Please sign in to comment.