Skip to content

Commit

Permalink
Implemented webmozarts#213: Assert::positiveInteger() to have psalm…
Browse files Browse the repository at this point in the history
… `positive-int` assertions
  • Loading branch information
Ocramius committed Mar 2, 2021
1 parent 4631e2c commit ee14f24
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ci/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"vimeo/psalm": "dev-master#61f5a06"
"vimeo/psalm": "^3.14.2"
}
}
19 changes: 19 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<positive-int> $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function allPositiveInteger($value, $message = '');

/**
* @psalm-pure
* @psalm-assert float|null $value
Expand Down
10 changes: 10 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
61 changes: 61 additions & 0 deletions tests/static-analysis/assert-positiveInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-pure
*
* @param mixed $value
*
* @psalm-return positive-int
*/
function positiveInteger($value): int
{
Assert::positiveInteger($value);

return $value;
}

/**
* @psalm-pure
*
* @param 0|1|2 $value
*
* @psalm-return 1|2
*/
function positiveIntegerFiltersOutZero($value): int
{
Assert::positiveInteger($value);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @psalm-return positive-int|null
*/
function nullOrPositiveInteger($value): ?int
{
Assert::nullOrPositiveInteger($value);

return $value;
}

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

return $value;
}

0 comments on commit ee14f24

Please sign in to comment.