Skip to content

Commit

Permalink
Add support for count
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored and ondrejmirtes committed Nov 29, 2021
1 parent 2cd9a91 commit a00dad3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ This extension specifies types of values passed to:
* `Assert::classExists`
* `Assert::interfaceExists`
* `Assert::validArrayKey`
* `Assert::count`
* `Assert::minCount`
* `Assert::inArray`
* `Assert::oneOf`
Expand Down
9 changes: 9 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@ private static function getExpressionResolvers(): array
[$class]
);
},
'count' => function (Scope $scope, Arg $array, Arg $number): \PhpParser\Node\Expr {
return new \PhpParser\Node\Expr\BinaryOp\Identical(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('count'),
[$array]
),
$number->value
);
},
'minCount' => function (Scope $scope, Arg $array, Arg $number): \PhpParser\Node\Expr {
return new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
new \PhpParser\Node\Expr\FuncCall(
Expand Down
8 changes: 8 additions & 0 deletions tests/Type/WebMozartAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,

Assert::interfaceExists($ag);
\PHPStan\Testing\assertType('class-string', $ag);

/** @var int[] $at */
$at = doFoo();
Assert::count($at, 1);
$au = array_pop($at);
$av = array_pop($at);
\PHPStan\Testing\assertType('int', $au);
\PHPStan\Testing\assertType('int|null', $av);
}

}
Expand Down

0 comments on commit a00dad3

Please sign in to comment.