Skip to content

Commit 11ecf41

Browse files
herndlmvillfa
authored andcommitted
Support remaining array count assertions
Co-authored-by: Fabien Villepinte <[email protected]>
1 parent 7db7d85 commit 11ecf41

File tree

3 files changed

+77
-10
lines changed

3 files changed

+77
-10
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ This extension specifies types of values passed to:
6262
* `Assert::validArrayKey`
6363
* `Assert::count`
6464
* `Assert::minCount`
65+
* `Assert::maxCount`
66+
* `Assert::countBetween`
6567
* `Assert::inArray`
6668
* `Assert::oneOf`
6769
* `Assert::methodExists`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,40 @@ private static function getExpressionResolvers(): array
436436
$number->value
437437
);
438438
},
439-
'minCount' => function (Scope $scope, Arg $array, Arg $number): \PhpParser\Node\Expr {
439+
'minCount' => function (Scope $scope, Arg $array, Arg $min): \PhpParser\Node\Expr {
440440
return new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
441441
new \PhpParser\Node\Expr\FuncCall(
442442
new \PhpParser\Node\Name('count'),
443443
[$array]
444444
),
445-
$number->value
445+
$min->value
446+
);
447+
},
448+
'maxCount' => function (Scope $scope, Arg $array, Arg $max): \PhpParser\Node\Expr {
449+
return new \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual(
450+
new \PhpParser\Node\Expr\FuncCall(
451+
new \PhpParser\Node\Name('count'),
452+
[$array]
453+
),
454+
$max->value
455+
);
456+
},
457+
'countBetween' => function (Scope $scope, Arg $array, Arg $min, Arg $max): \PhpParser\Node\Expr {
458+
return new BooleanAnd(
459+
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
460+
new \PhpParser\Node\Expr\FuncCall(
461+
new \PhpParser\Node\Name('count'),
462+
[$array]
463+
),
464+
$min->value
465+
),
466+
new \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual(
467+
new \PhpParser\Node\Expr\FuncCall(
468+
new \PhpParser\Node\Name('count'),
469+
[$array]
470+
),
471+
$max->value
472+
)
446473
);
447474
},
448475
'length' => function (Scope $scope, Arg $value, Arg $length): \PhpParser\Node\Expr {

tests/Type/WebMozartAssert/data/array.php

+46-8
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,62 @@ public function validArrayKey($a, bool $b): void
3636

3737
/**
3838
* @param int[] $a
39+
* @param int[] $b
3940
*/
40-
public function count(array $a): void
41+
public function count(array $a, array $b): void
4142
{
4243
Assert::count($a, 1);
43-
$a1 = array_pop($a);
44-
$a2 = array_pop($a);
45-
\PHPStan\Testing\assertType('int', $a1);
46-
\PHPStan\Testing\assertType('int|null', $a2);
44+
\PHPStan\Testing\assertType('non-empty-array<int>', $a);
45+
46+
Assert::count($b, 0);
47+
\PHPStan\Testing\assertType('array{}', $b);
4748
}
4849

4950
/**
5051
* @param int[] $a
52+
* @param int[] $b
5153
*/
52-
public function minCount(array $a): void
54+
public function minCount(array $a, array $b): void
5355
{
5456
Assert::minCount($a, 1);
55-
$a1 = array_pop($a);
56-
\PHPStan\Testing\assertType('int', $a1);
57+
\PHPStan\Testing\assertType('non-empty-array<int>', $a);
58+
59+
Assert::minCount($b, 0);
60+
\PHPStan\Testing\assertType('array<int>', $b);
61+
}
62+
63+
/**
64+
* @param int[] $a
65+
* @param int[] $b
66+
*/
67+
public function maxCount(array $a, array $b): void
68+
{
69+
Assert::maxCount($a, 1);
70+
\PHPStan\Testing\assertType('array<int>', $a);
71+
72+
Assert::maxCount($b, 0);
73+
\PHPStan\Testing\assertType('array{}', $b);
74+
}
75+
76+
/**
77+
* @param int[] $a
78+
* @param int[] $b
79+
* @param int[] $c
80+
* @param int[] $d
81+
*/
82+
public function countBetween(array $a, array $b, array $c, array $d): void
83+
{
84+
Assert::countBetween($a, 1, 2);
85+
\PHPStan\Testing\assertType('non-empty-array<int>', $a);
86+
87+
Assert::countBetween($b, 0, 2);
88+
\PHPStan\Testing\assertType('array<int>', $b);
89+
90+
Assert::countBetween($c, 0, 0);
91+
\PHPStan\Testing\assertType('array{}', $c);
92+
93+
Assert::countBetween($d, 2, 0);
94+
\PHPStan\Testing\assertType('*NEVER*', $d);
5795
}
5896

5997
public function isList($a): void

0 commit comments

Comments
 (0)