Skip to content

Commit

Permalink
Add keysInteger
Browse files Browse the repository at this point in the history
Fixes webmozarts#213

Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Aug 28, 2020
1 parent bafc69c commit ef1a982
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,23 @@ public static function validArrayKey($value, $message = '')
}
}

/**
* @psalm-pure
* @psalm-template T
* @psalm-param iterable<T> $array
* @psalm-assert iterable<int, T> $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.
*
Expand Down
26 changes: 26 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,32 @@ public static function nullOrValidArrayKey($value, $message = '');
*/
public static function allValidArrayKey($value, $message = '');

/**
* @psalm-pure
* @psalm-template T
* @psalm-param iterable<T> $array
* @psalm-assert null|iterable<int, T> $array
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function nullOrKeysInteger($value, $message = '');

/**
* @psalm-pure
* @psalm-template T
* @psalm-param iterable<T> $array
* @psalm-assert iterable<iterable<int, T>> $array
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function allKeysInteger($value, $message = '');

/**
* @param null|Countable|array $array
* @param int $number
Expand Down
9 changes: 8 additions & 1 deletion tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions tests/static-analysis/assert-keysInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-pure
*
* @param mixed $value
*/
function keysInteger($value): iterable
{
Assert::keysInteger($value);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*/
function nullOrkeysInteger($value): ?iterable
{
Assert::nullOrkeysInteger($value);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return iterable<iterable>
*/
function allkeysInteger($value): iterable
{
Assert::allkeysInteger($value);

return $value;
}

0 comments on commit ef1a982

Please sign in to comment.