Skip to content

Commit

Permalink
Deal with lists that have been modified that have (#247)
Browse files Browse the repository at this point in the history
NAN as a value
  • Loading branch information
BackEndTea authored Apr 18, 2022
1 parent 7b05571 commit dc96b67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -1822,11 +1822,24 @@ public static function countBetween($array, $min, $max, $message = '')
*/
public static function isList($array, $message = '')
{
if (!\is_array($array) || $array !== \array_values($array)) {
if (!\is_array($array)) {
static::reportInvalidArgument(
$message ?: 'Expected list - non-associative array.'
);
}

if ($array === \array_values($array)) {
return;
}

$nextKey = -1;
foreach ($array as $k => $v) {
if ($k !== ++$nextKey) {
static::reportInvalidArgument(
$message ?: 'Expected list - non-associative array.'
);
}
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public function getTests()
{
$resource = self::getResource();

$nanList = array('key' => 2, NAN);
unset($nanList['key']);

$normalList = array('foo' => 'b', 3);
unset($normalList['foo']);

return array(
array('string', array('value'), true),
array('string', array(''), true),
Expand Down Expand Up @@ -495,6 +501,8 @@ 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('isList', array($nanList), true),
array('isList', array($normalList), true),
array('isNonEmptyList', array(array(1, 2, 3)), true),
array('isNonEmptyList', array(array()), false),
array('isNonEmptyList', array(array(0 => 1, 2 => 3)), false),
Expand Down

0 comments on commit dc96b67

Please sign in to comment.