Skip to content

Commit

Permalink
Add non empty assertions for list and map (webmozarts#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
BackEndTea authored Nov 24, 2019
1 parent 7697fe1 commit 1526c4f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ Method | Description
`maxCount($array, $max, $message = '')` | Check that an array contains at most a certain number of elements
`countBetween($array, $min, $max, $message = '')` | Check that an array has a count in the given range
`isList($array, $message = '')` | Check that an array is a non-associative list
`isNonEmptyList($array, $message = '')` | Check that an array is a non-associative list, and not empty
`isMap($array, $message = '')` | Check that an array is associative and has strings as keys
`isNonEmptyMap($array, $message = '')` | Check that an array is associative and has strings as keys, and is not empty

### Function Assertions

Expand Down
32 changes: 31 additions & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@
* @method static void nullOrMinCount($value, $min, $message = '')
* @method static void nullOrMaxCount($value, $max, $message = '')
* @method static void nullOrIsList($value, $message = '')
* @method static void nullOrIsNonEmptyList($value, $message = '')
* @method static void nullOrIsMap($value, $message = '')
* @method static void nullOrIsNonEmptyMap($value, $message = '')
* @method static void nullOrCountBetween($value, $min, $max, $message = '')
* @method static void nullOrUuid($values, $message = '')
* @method static void nullOrThrows($expression, $class = 'Exception', $message = '')
Expand Down Expand Up @@ -186,7 +188,9 @@
* @method static void allMaxCount($values, $max, $message = '')
* @method static void allCountBetween($values, $min, $max, $message = '')
* @method static void allIsList($values, $message = '')
* @method static void allIsNonEmptyList($values, $message = '')
* @method static void allIsMap($values, $message = '')
* @method static void allIsNonEmptyMap($values, $message = '')
* @method static void allUuid($values, $message = '')
* @method static void allThrows($expressions, $class = 'Exception', $message = '')
*
Expand Down Expand Up @@ -1702,6 +1706,20 @@ public static function isList($array, $message = '')
}
}

/**
* @psalm-assert non-empty-list $array
*
* @param mixed $array
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isNonEmptyList($array, $message = '')
{
static::isList($array, $message);
static::notEmpty($array, $message);
}

/**
* @param mixed $array
* @param string $message
Expand All @@ -1712,14 +1730,26 @@ public static function isMap($array, $message = '')
{
if (
!\is_array($array) ||
\array_keys($array) !== \array_filter(\array_keys($array), 'is_string')
\array_keys($array) !== \array_filter(\array_keys($array), '\is_string')
) {
static::reportInvalidArgument(
$message ?: 'Expected map - associative array with string keys.'
);
}
}

/**
* @param mixed $array
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isNonEmptyMap($array, $message = '')
{
static::isMap($array, $message);
static::notEmpty($array, $message);
}

/**
* @param mixed $value
* @param string $message
Expand Down
14 changes: 14 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,24 @@ 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('isNonEmptyList', array(array(1, 2, 3)), true),
array('isNonEmptyList', array(array()), false),
array('isNonEmptyList', array(array(0 => 1, 2 => 3)), false),
array('isNonEmptyList', array(array('key' => 1, 'foo' => 2)), false),
array('isNonEmptyList', array(true), false),
array('isNonEmptyList', array(false), false),
array('isNonEmptyList', array(array(true)), true),
array('isNonEmptyList', array(array(false)), true),
array('isNonEmptyList', array(array(array(1), array(2))), true),
array('isNonEmptyList', array(array(array('foo' => 'bar'), array('baz' => 'tab'))), true),
array('isMap', array(array('key' => 1, 'foo' => 2)), true),
array('isMap', array(array()), true),
array('isMap', array(array(1, 2, 3)), false),
array('isMap', array(array(0 => 1, 2 => 3)), false),
array('isNonEmptyMap', array(array('key' => 1, 'foo' => 2)), true),
array('isNonEmptyMap', array(array()), false),
array('isNonEmptyMap', array(array(1, 2, 3)), false),
array('isNonEmptyMap', array(array(0 => 1, 2 => 3)), false),
array('uuid', array('00000000-0000-0000-0000-000000000000'), true),
array('uuid', array('urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), true),
array('uuid', array('uuid:{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'), true),
Expand Down
17 changes: 17 additions & 0 deletions tests/static-analysis/assert-isNonEmptyList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-param mixed $value
*
* @psalm-return non-empty-list<mixed>
*/
function consume($value): array
{
Assert::isNonEmptyList($value);

return $value;
}

0 comments on commit 1526c4f

Please sign in to comment.