Skip to content

Commit

Permalink
Support isNonEmptyList and isNonEmptyMap
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Feb 23, 2022
1 parent 586d049 commit d1c78f6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ This extension specifies types of values passed to:
* `Assert::maxCount`
* `Assert::countBetween`
* `Assert::isList`
* `Assert::isNonEmptyList`
* `Assert::isMap`
* `Assert::isNonEmptyMap`
* `Assert::inArray`
* `Assert::oneOf`
* `Assert::methodExists`
Expand Down
18 changes: 18 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ private static function getExpressionResolvers(): array
)
);
},
'isNonEmptyList' => static function (Scope $scope, Arg $expr): Expr {
return new BooleanAnd(
self::$resolvers['isList']($scope, $expr),
new NotIdentical(
$expr->value,
new Array_()
)
);
},
'isMap' => static function (Scope $scope, Arg $expr): Expr {
return new BooleanAnd(
new FuncCall(
Expand All @@ -346,6 +355,15 @@ private static function getExpressionResolvers(): array
)
);
},
'isNonEmptyMap' => static function (Scope $scope, Arg $expr): Expr {
return new BooleanAnd(
self::$resolvers['isMap']($scope, $expr),
new NotIdentical(
$expr->value,
new Array_()
)
);
},
'isCountable' => static function (Scope $scope, Arg $expr): Expr {
return new BooleanOr(
new FuncCall(
Expand Down
18 changes: 18 additions & 0 deletions tests/Type/WebMozartAssert/data/array.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ public function isList($a, $b): void
\PHPStan\Testing\assertType('array<int, mixed>|null', $b);
}

public function isNonEmptyList($a, $b): void
{
Assert::isNonEmptyList($a);
\PHPStan\Testing\assertType('non-empty-array<int, mixed>', $a);

Assert::nullOrIsNonEmptyList($b);
\PHPStan\Testing\assertType('non-empty-array<int, mixed>|null', $b);
}

public function isMap($a, $b): void
{
Assert::isMap($a);
Expand All @@ -115,4 +124,13 @@ public function isMap($a, $b): void
\PHPStan\Testing\assertType('array<string, mixed>|null', $b);
}

public function isNonEmptyMap($a, $b): void
{
Assert::isNonEmptyMap($a);
\PHPStan\Testing\assertType('non-empty-array<string, mixed>', $a);

Assert::nullOrIsNonEmptyMap($b);
\PHPStan\Testing\assertType('non-empty-array<string, mixed>|null', $b);
}

}

0 comments on commit d1c78f6

Please sign in to comment.