Skip to content

Commit

Permalink
Removed Assert::naturalNumber(), since we already have `Assert::nat…
Browse files Browse the repository at this point in the history
…ural()` doing the same thing

Note: we refined the assertion on `Assert::natural()` to correctly restrict to `positive-int|0`,
but we had to remove some minor test on `Assert::allNaturalNumber()` due to `@psalm-assert iterable<positive-int|0>`
not yet working in upstream.

See vimeo/psalm#5052
  • Loading branch information
Ocramius committed Jan 19, 2021
1 parent 140fc69 commit bd91e2f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 100 deletions.
21 changes: 1 addition & 20 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,6 @@ public static function positiveInteger($value, $message = '')
}
}

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

/**
* @psalm-pure
* @psalm-assert float $value
Expand Down Expand Up @@ -185,7 +166,7 @@ public static function numeric($value, $message = '')

/**
* @psalm-pure
* @psalm-assert int $value
* @psalm-assert positive-int|0 $value
*
* @param mixed $value
* @param string $message
Expand Down
34 changes: 2 additions & 32 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,36 +168,6 @@ public static function allPositiveInteger($value, $message = '')
static::__callStatic('allPositiveInteger', [$value, $message]);
}

/**
* @psalm-pure
* @psalm-assert positive-int|0|null $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
* @return void
*/
public static function nullOrNaturalNumber($value, $message = '')
{
static::__callStatic('nullOrNaturalNumber', [$value, $message]);
}

/**
* @psalm-pure
* @psalm-assert iterable<positive-int|0> $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
* @return void
*/
public static function allNaturalNumber($value, $message = '')
{
static::__callStatic('allNaturalNumber', [$value, $message]);
}

/**
* @psalm-pure
* @psalm-assert float|null $value
Expand Down Expand Up @@ -260,7 +230,7 @@ public static function allNumeric($value, $message = '')

/**
* @psalm-pure
* @psalm-assert int|null $value
* @psalm-assert positive-int|0|null $value
*
* @param mixed $value
* @param string $message
Expand All @@ -275,7 +245,7 @@ public static function nullOrNatural($value, $message = '')

/**
* @psalm-pure
* @psalm-assert iterable<int> $value
* @psalm-assert iterable<positive-int|0> $value
*
* @param mixed $value
* @param string $message
Expand Down
9 changes: 8 additions & 1 deletion tests/static-analysis/assert-natural.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @psalm-pure
*
* @param mixed $value
*
* @psalm-return positive-int|0
*/
function natural($value): int
{
Expand All @@ -20,6 +22,8 @@ function natural($value): int
* @psalm-pure
*
* @param mixed $value
*
* @psalm-return positive-int|0|null
*/
function nullOrNatural($value): ?int
{
Expand All @@ -33,7 +37,10 @@ function nullOrNatural($value): ?int
*
* @param mixed $value
*
* @return iterable<int>
* @return iterable<positive-int|0>
*
* @psalm-suppress MixedInferredReturnType https://github.com/vimeo/psalm/issues/5052
* @psalm-suppress MixedReturnStatement https://github.com/vimeo/psalm/issues/5052
*/
function allNatural($value): iterable
{
Expand Down
47 changes: 0 additions & 47 deletions tests/static-analysis/assert-naturalNumber.php

This file was deleted.

0 comments on commit bd91e2f

Please sign in to comment.