From 1526c4f20067dfdae36f3bb6353885ff461e5380 Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Sun, 24 Nov 2019 14:05:40 +0100 Subject: [PATCH] Add non empty assertions for list and map (#153) --- README.md | 2 ++ src/Assert.php | 32 ++++++++++++++++++- tests/AssertTest.php | 14 ++++++++ .../static-analysis/assert-isNonEmptyList.php | 17 ++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/static-analysis/assert-isNonEmptyList.php diff --git a/README.md b/README.md index 6407ee59..615c8b27 100644 --- a/README.md +++ b/README.md @@ -196,7 +196,9 @@ Method | Description `maxCount($array, $max, $message = '')` | Check that an array contains at most a certain number of elements `countBetween($array, $min, $max, $message = '')` | Check that an array has a count in the given range `isList($array, $message = '')` | Check that an array is a non-associative list +`isNonEmptyList($array, $message = '')` | Check that an array is a non-associative list, and not empty `isMap($array, $message = '')` | Check that an array is associative and has strings as keys +`isNonEmptyMap($array, $message = '')` | Check that an array is associative and has strings as keys, and is not empty ### Function Assertions diff --git a/src/Assert.php b/src/Assert.php index b4027793..dd9ec92a 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -102,7 +102,9 @@ * @method static void nullOrMinCount($value, $min, $message = '') * @method static void nullOrMaxCount($value, $max, $message = '') * @method static void nullOrIsList($value, $message = '') + * @method static void nullOrIsNonEmptyList($value, $message = '') * @method static void nullOrIsMap($value, $message = '') + * @method static void nullOrIsNonEmptyMap($value, $message = '') * @method static void nullOrCountBetween($value, $min, $max, $message = '') * @method static void nullOrUuid($values, $message = '') * @method static void nullOrThrows($expression, $class = 'Exception', $message = '') @@ -186,7 +188,9 @@ * @method static void allMaxCount($values, $max, $message = '') * @method static void allCountBetween($values, $min, $max, $message = '') * @method static void allIsList($values, $message = '') + * @method static void allIsNonEmptyList($values, $message = '') * @method static void allIsMap($values, $message = '') + * @method static void allIsNonEmptyMap($values, $message = '') * @method static void allUuid($values, $message = '') * @method static void allThrows($expressions, $class = 'Exception', $message = '') * @@ -1702,6 +1706,20 @@ public static function isList($array, $message = '') } } + /** + * @psalm-assert non-empty-list $array + * + * @param mixed $array + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function isNonEmptyList($array, $message = '') + { + static::isList($array, $message); + static::notEmpty($array, $message); + } + /** * @param mixed $array * @param string $message @@ -1712,7 +1730,7 @@ public static function isMap($array, $message = '') { if ( !\is_array($array) || - \array_keys($array) !== \array_filter(\array_keys($array), 'is_string') + \array_keys($array) !== \array_filter(\array_keys($array), '\is_string') ) { static::reportInvalidArgument( $message ?: 'Expected map - associative array with string keys.' @@ -1720,6 +1738,18 @@ public static function isMap($array, $message = '') } } + /** + * @param mixed $array + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function isNonEmptyMap($array, $message = '') + { + static::isMap($array, $message); + static::notEmpty($array, $message); + } + /** * @param mixed $value * @param string $message diff --git a/tests/AssertTest.php b/tests/AssertTest.php index a5b714c1..d6af7dc4 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -434,10 +434,24 @@ public function getTests() array('isList', array(array(false)), true), array('isList', array(array(array(1), array(2))), true), array('isList', array(array(array('foo' => 'bar'), array('baz' => 'tab'))), true), + array('isNonEmptyList', array(array(1, 2, 3)), true), + array('isNonEmptyList', array(array()), false), + array('isNonEmptyList', array(array(0 => 1, 2 => 3)), false), + array('isNonEmptyList', array(array('key' => 1, 'foo' => 2)), false), + array('isNonEmptyList', array(true), false), + array('isNonEmptyList', array(false), false), + array('isNonEmptyList', array(array(true)), true), + array('isNonEmptyList', array(array(false)), true), + array('isNonEmptyList', array(array(array(1), array(2))), true), + array('isNonEmptyList', array(array(array('foo' => 'bar'), array('baz' => 'tab'))), true), array('isMap', array(array('key' => 1, 'foo' => 2)), true), array('isMap', array(array()), true), array('isMap', array(array(1, 2, 3)), false), array('isMap', array(array(0 => 1, 2 => 3)), false), + array('isNonEmptyMap', array(array('key' => 1, 'foo' => 2)), true), + array('isNonEmptyMap', array(array()), false), + array('isNonEmptyMap', array(array(1, 2, 3)), false), + array('isNonEmptyMap', array(array(0 => 1, 2 => 3)), false), array('uuid', array('00000000-0000-0000-0000-000000000000'), true), array('uuid', array('urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), true), array('uuid', array('uuid:{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'), true), diff --git a/tests/static-analysis/assert-isNonEmptyList.php b/tests/static-analysis/assert-isNonEmptyList.php new file mode 100644 index 00000000..1e8d31b5 --- /dev/null +++ b/tests/static-analysis/assert-isNonEmptyList.php @@ -0,0 +1,17 @@ + + */ +function consume($value): array +{ + Assert::isNonEmptyList($value); + + return $value; +}