From ef1a9820d6b4344477a500b26e35bbd4ca008ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Fri, 28 Aug 2020 17:56:08 +0200 Subject: [PATCH] Add `keysInteger` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #213 Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- README.md | 1 + src/Assert.php | 17 ++++++++ src/Mixin.php | 26 ++++++++++++ tests/AssertTest.php | 9 +++- tests/static-analysis/assert-keysInteger.php | 43 ++++++++++++++++++++ 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/static-analysis/assert-keysInteger.php diff --git a/README.md b/README.md index 1407a9a1..c247c833 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ Method | Description `isNotA($value, $class, $message = '')` | Check that a value is not of the class or has not one of its parents `isArrayAccessible($value, $message = '')` | Check that a value can be accessed as an array `uniqueValues($values, $message = '')` | Check that the given array contains unique values +`keysInteger($value, $message = '')` | Check that the given iterable contains only integer keys ### Comparison Assertions diff --git a/src/Assert.php b/src/Assert.php index b28e1784..a58fc7e9 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -1707,6 +1707,23 @@ public static function validArrayKey($value, $message = '') } } + /** + * @psalm-pure + * @psalm-template T + * @psalm-param iterable $array + * @psalm-assert iterable $array + * + * @param mixed $value + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function keysInteger($value, $message = '') + { + static::isIterable($value, $message); + static::allInteger(array_keys(is_array($value) ? $value : iterator_to_array($value)), $message); + } + /** * Does not check if $array is countable, this can generate a warning on php versions after 7.2. * diff --git a/src/Mixin.php b/src/Mixin.php index 3ad9b2d0..99d8a059 100644 --- a/src/Mixin.php +++ b/src/Mixin.php @@ -1759,6 +1759,32 @@ public static function nullOrValidArrayKey($value, $message = ''); */ public static function allValidArrayKey($value, $message = ''); + /** + * @psalm-pure + * @psalm-template T + * @psalm-param iterable $array + * @psalm-assert null|iterable $array + * + * @param mixed $value + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function nullOrKeysInteger($value, $message = ''); + + /** + * @psalm-pure + * @psalm-template T + * @psalm-param iterable $array + * @psalm-assert iterable> $array + * + * @param mixed $value + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function allKeysInteger($value, $message = ''); + /** * @param null|Countable|array $array * @param int $number diff --git a/tests/AssertTest.php b/tests/AssertTest.php index c1f708b9..7f78fa2c 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -47,7 +47,7 @@ public function getTests() { $resource = self::getResource(); - return array( + $tests = array( array('string', array('value'), true), array('string', array(''), true), array('string', array(1234), false), @@ -130,6 +130,11 @@ public function getTests() array('isIterable', array(new ArrayIterator(array())), true), array('isIterable', array(123), false), array('isIterable', array(new stdClass()), false), + array('keysInteger', array(array()), true), + array('keysInteger', array(new ArrayIterator(array(1, 2, 3))), true), + array('keysInteger', array(new ArrayIterator(array('foo' => 'bar'))), false), + array('keysInteger', array(array('' => 'bar')), false), + array('keysInteger', array(new ArrayIterator(array('' => 'bar', 2, 3))), false), array('isInstanceOf', array(new stdClass(), 'stdClass'), true), array('isInstanceOf', array(new Exception(), 'stdClass'), false), array('isInstanceOf', array(123, 'stdClass'), false), @@ -567,6 +572,8 @@ public function getTests() array('uniqueValues', array(array('asdfg', 'qwerty')), true), array('uniqueValues', array(array(123, '123')), false), ); + + return $tests; } public function getMethods() diff --git a/tests/static-analysis/assert-keysInteger.php b/tests/static-analysis/assert-keysInteger.php new file mode 100644 index 00000000..436f712c --- /dev/null +++ b/tests/static-analysis/assert-keysInteger.php @@ -0,0 +1,43 @@ + + */ +function allkeysInteger($value): iterable +{ + Assert::allkeysInteger($value); + + return $value; +}