From d7bffa27335a4b84cec430092f383a4aa1adcaee 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:57:39 +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 | 5 +++ tests/static-analysis/assert-keysInteger.php | 43 ++++++++++++++++++++ 5 files changed, 92 insertions(+) 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..8a99c3e0 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -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), 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; +}