Skip to content

Addition of two new assert functions for better validation of differe… #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Changelog
=========

## UNRELEASED
* Added `Assert::nonNegativeInteger()` and `Assert::negativeInteger()`

## 1.11.0

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Method | Description
`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
`negativeInteger($value, $message = '')` | Check that a value is a negative integer
`nonNegativeInteger($value, $message = '')` | Check that a value is a non-negative 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
38 changes: 38 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,44 @@ public static function positiveInteger($value, $message = '')
}
}

/**
* @psalm-pure
* @psalm-assert non-negative-int $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function nonNegativeInteger($value, $message = '')
{
if (!(\is_int($value) && $value >= 0)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a non negative integer. Got: %s',
static::valueToString($value)
));
}
}

/**
* @psalm-pure
* @psalm-assert non-negative-int $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function negativeInteger($value, $message = '')
{
if (!(\is_int($value) && $value < 0)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a negative integer. Got: %s',
static::valueToString($value)
));
}
}

/**
* @psalm-pure
* @psalm-assert float $value
Expand Down
112 changes: 112 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,118 @@ public static function allNullOrPositiveInteger($value, $message = '')
}
}

/**
* @psalm-pure
* @psalm-assert non-negative-int|null $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function nullOrNonNegativeInteger($value, $message = '')
{
null === $value || static::nonNegativeInteger($value, $message);
}

/**
* @psalm-pure
* @psalm-assert iterable<non-negative-int> $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function allNonNegativeInteger($value, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
static::nonNegativeInteger($entry, $message);
}
}

/**
* @psalm-pure
* @psalm-assert iterable<non-negative-int|null> $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function allNullOrNonNegativeInteger($value, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
null === $entry || static::nonNegativeInteger($entry, $message);
}
}

/**
* @psalm-pure
* @psalm-assert non-negative-int|null $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function nullOrNegativeInteger($value, $message = '')
{
null === $value || static::negativeInteger($value, $message);
}

/**
* @psalm-pure
* @psalm-assert iterable<non-negative-int> $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function allNegativeInteger($value, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
static::negativeInteger($entry, $message);
}
}

/**
* @psalm-pure
* @psalm-assert iterable<non-negative-int|null> $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function allNullOrNegativeInteger($value, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
null === $entry || static::negativeInteger($entry, $message);
}
}

/**
* @psalm-pure
* @psalm-assert float|null $value
Expand Down
20 changes: 20 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ public function getTests()
array('positiveInteger', array('0'), false),
array('positiveInteger', array(1.0), false),
array('positiveInteger', array(1.23), false),
array('nonNegativeInteger', array(123), true),
array('nonNegativeInteger', array(1), true),
array('nonNegativeInteger', array(-123), false),
array('nonNegativeInteger', array(0), true),
array('nonNegativeInteger', array(0.0), false),
array('nonNegativeInteger', array('123'), false),
array('nonNegativeInteger', array('-123'), false),
array('nonNegativeInteger', array('0'), false),
array('nonNegativeInteger', array(1.0), false),
array('nonNegativeInteger', array(1.23), false),
array('negativeInteger', array(123), false),
array('negativeInteger', array(1), false),
array('negativeInteger', array(-123), true),
array('negativeInteger', array(0), false),
array('negativeInteger', array(0.0), false),
array('negativeInteger', array('123'), false),
array('negativeInteger', array('-123'), false),
array('negativeInteger', array('0'), false),
array('negativeInteger', array(1.0), false),
array('negativeInteger', 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-negativeInteger.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 negative-int
*/
function negativeInteger($value): int
{
Assert::negativeInteger($value);

return $value;
}

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

return $value;
}

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

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return iterable<negative-int|null>
*/
function allNullOrNegativeInteger($value): iterable
{
Assert::allNullOrNegativeInteger($value);

return $value;
}
63 changes: 63 additions & 0 deletions tests/static-analysis/assert-nonNegativeInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-pure
*
* @param mixed $value
*
* @psalm-return non-negative-int
*/
function nonNegativeInteger($value): int
{
Assert::nonNegativeInteger($value);

$value *= -1;

return $value;
}

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

return $value;
}

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

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return iterable<non-negative-int|null>
*/
function allNullOrNonNegativeInteger($value): iterable
{
Assert::allNullOrPositiveInteger($value);

return $value;
}