From 7e202b151ac27e6720037e022f9edc98d077c8cf Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 23 Nov 2019 10:11:04 +0100 Subject: [PATCH] Fix: allow empty maps and lists (#152) * Expecting `isList` to allow empty arrays and lists composed of non-list items * Corrected `Assert::isList()` to accept empty lists as valid lists * Expecting empty maps to be valid according to `Assert::isMap()` * Corrected `Assert::isMap()` to accept empty maps * Simplified `Assert::isMap()` internal mapping over keys --- src/Assert.php | 7 ++----- tests/AssertTest.php | 8 ++++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index 09a06a54..d32366d9 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -1699,7 +1699,7 @@ public static function countBetween($array, $min, $max, $message = '') */ public static function isList($array, $message = '') { - if (!\is_array($array) || !$array || \array_keys($array) !== \range(0, \count($array) - 1)) { + if (!\is_array($array) || $array !== \array_values($array)) { static::reportInvalidArgument( $message ?: 'Expected list - non-associative array.' ); @@ -1716,10 +1716,7 @@ public static function isMap($array, $message = '') { if ( !\is_array($array) || - !$array || - \array_keys($array) !== \array_filter(\array_keys($array), function ($key) { - return \is_string($key); - }) + \array_keys($array) !== \array_filter(\array_keys($array), 'is_string') ) { static::reportInvalidArgument( $message ?: 'Expected map - associative array with string keys.' diff --git a/tests/AssertTest.php b/tests/AssertTest.php index dea9893f..4b22723b 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -425,13 +425,17 @@ public function getTests() array('countBetween', array(array(0, 1, 2), 2, 5), true), array('countBetween', array(array(0, 1, 2), 2, 3), true), array('isList', array(array(1, 2, 3)), true), - array('isList', array(array()), false), + array('isList', array(array()), true), array('isList', array(array(0 => 1, 2 => 3)), false), array('isList', array(array('key' => 1, 'foo' => 2)), false), array('isList', array(true), false), array('isList', array(false), false), + array('isList', array(array(true)), true), + 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('isMap', array(array('key' => 1, 'foo' => 2)), true), - array('isMap', array(array()), false), + array('isMap', array(array()), true), array('isMap', array(array(1, 2, 3)), false), array('isMap', array(array(0 => 1, 2 => 3)), false), array('uuid', array('00000000-0000-0000-0000-000000000000'), true),