From e6030ef010a49a8e111605ac702663a84027449d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 28 Aug 2020 13:27:22 +0200 Subject: [PATCH] Implemented #213: `Assert::positiveInteger()` to have psalm `positive-int` assertions --- README.md | 1 + ci/composer.json | 2 +- src/Assert.php | 19 ++++++ src/Mixin.php | 22 +++++++ tests/AssertTest.php | 10 +++ .../assert-positiveInteger.php | 61 +++++++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 tests/static-analysis/assert-positiveInteger.php diff --git a/README.md b/README.md index 9f53f8fa..4288f6e3 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ Method | Description `stringNotEmpty($value, $message = '')` | Check that a value is a non-empty string `integer($value, $message = '')` | Check that a value is an integer `integerish($value, $message = '')` | Check that a value casts to an integer +`positiveInteger($value, $message = '')` | Check that a value is a positive (non-zero) integer `float($value, $message = '')` | Check that a value is a float `numeric($value, $message = '')` | Check that a value is numeric `natural($value, $message= ''')` | Check that a value is a non-negative integer diff --git a/ci/composer.json b/ci/composer.json index 30e6a357..8540facf 100644 --- a/ci/composer.json +++ b/ci/composer.json @@ -1,5 +1,5 @@ { "require": { - "vimeo/psalm": "dev-master#61f5a06" + "vimeo/psalm": "^3.14.2" } } diff --git a/src/Assert.php b/src/Assert.php index b28e1784..d95b588c 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -107,6 +107,25 @@ public static function integerish($value, $message = '') } } + /** + * @psalm-pure + * @psalm-assert positive-int $value + * + * @param mixed $value + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function positiveInteger($value, $message = '') + { + if (! (\is_int($value) && $value > 0)) { + static::reportInvalidArgument(\sprintf( + $message ?: 'Expected a positive integer. Got: %s', + static::typeToString($value) + )); + } + } + /** * @psalm-pure * @psalm-assert float $value diff --git a/src/Mixin.php b/src/Mixin.php index ccc43990..6e5f5f82 100644 --- a/src/Mixin.php +++ b/src/Mixin.php @@ -102,6 +102,28 @@ public static function nullOrIntegerish($value, $message = ''); */ public static function allIntegerish($value, $message = ''); + /** + * @psalm-pure + * @psalm-assert null|positive-int $value + * + * @param mixed $value + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function nullOrPositiveInteger($value, $message = ''); + + /** + * @psalm-pure + * @psalm-assert iterable $value + * + * @param mixed $value + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function allPositiveInteger($value, $message = ''); + /** * @psalm-pure * @psalm-assert float|null $value diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 2b90c67b..9b249452 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -67,6 +67,16 @@ public function getTests() array('integerish', array(1.23), false), array('integerish', array(123), true), array('integerish', array('123'), true), + array('positiveInteger', array(123), true), + array('positiveInteger', array(1), true), + array('positiveInteger', array(-123), false), + array('positiveInteger', array(0), false), + array('positiveInteger', array(0.0), false), + array('positiveInteger', array('123'), false), + array('positiveInteger', array('-123'), false), + array('positiveInteger', array('0'), false), + array('positiveInteger', array(1.0), false), + array('positiveInteger', array(1.23), false), array('float', array(1.0), true), array('float', array(1.23), true), array('float', array(123), false), diff --git a/tests/static-analysis/assert-positiveInteger.php b/tests/static-analysis/assert-positiveInteger.php new file mode 100644 index 00000000..ee1d9c83 --- /dev/null +++ b/tests/static-analysis/assert-positiveInteger.php @@ -0,0 +1,61 @@ + + */ +function allPositiveInteger($value): iterable +{ + Assert::allPositiveInteger($value); + + return $value; +}