Skip to content
Merged
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,11 @@ Method | Description

### Function Assertions

Method | Description
------------------------------------------- | -----------------------------------------------------------------------------------------------------
`throws($closure, $class, $message = '')` | Check that a function throws a certain exception. Subclasses of the exception class will be accepted.
Method | Description
------------------------------------------| -----------------------------------------------------------------------------------------------------
`throws($closure, $class, $message = '')` | Check that a function throws a certain exception. Subclasses of the exception class will be accepted.
`isStatic($closure, $message = '')` | Check that a function is static.
`notStatic($closure, $message = '')` | Check that a function is not static.

### Collection Assertions

Expand Down
52 changes: 52 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
namespace Webmozart\Assert;

use ArrayAccess;
use Closure;
use Countable;
use DateTime;
use DateTimeImmutable;
use ReflectionFunction;
use ReflectionProperty;
use Throwable;
use Traversable;
Expand Down Expand Up @@ -2042,6 +2044,56 @@ public static function isMap(mixed $array, string $message = ''): array
return $array;
}

/**
* @param Closure $closure
*
* @psalm-pure
*
* @psalm-assert static Closure $closure
*
* @psalm-return static Closure
*
* @throws InvalidArgumentException
*/
public static function isStatic(mixed $closure, string $message = ''): Closure
{
static::isCallable($closure, $message);
$reflection = new ReflectionFunction($closure);

if (!$reflection->isStatic()) {
static::reportInvalidArgument(
$message ?: 'Closure is not static.'
);
}

return $closure;
}

/**
* @param Closure $closure
*
* @psalm-pure
*
* @psalm-assert Closure $closure
*
* @psalm-return Closure
*
* @throws InvalidArgumentException
*/
public static function notStatic(mixed $closure, string $message = ''): Closure
{
static::isCallable($closure, $message);
$reflection = new ReflectionFunction($closure);

if ($reflection->isStatic()) {
static::reportInvalidArgument(
$message ?: 'Closure is not static.'
);
}

return $closure;
}

/**
* @psalm-pure
*
Expand Down
124 changes: 124 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4955,6 +4955,130 @@ public static function allNullOrIsMap(mixed $array, string $message = ''): mixed
return $array;
}

/**
* @param Closure|null $closure
*
* @psalm-pure
*
* @psalm-assert static Closure|null $closure
*
* @return static Closure|null
*
* @throws InvalidArgumentException
*/
public static function nullOrIsStatic(mixed $closure, string $message = ''): mixed
{
null === $closure || static::isStatic($closure, $message);

return $closure;
}

/**
* @param iterable<Closure> $closure
*
* @psalm-pure
*
* @psalm-assert iterable<static Closure> $closure
*
* @return iterable<static Closure>
*
* @throws InvalidArgumentException
*/
public static function allIsStatic(mixed $closure, string $message = ''): mixed
{
static::isIterable($closure);

foreach ($closure as $entry) {
static::isStatic($entry, $message);
}

return $closure;
}

/**
* @param iterable<Closure|null> $closure
*
* @psalm-pure
*
* @psalm-assert iterable<static Closure|null> $closure
*
* @return iterable<static Closure|null>
*
* @throws InvalidArgumentException
*/
public static function allNullOrIsStatic(mixed $closure, string $message = ''): mixed
{
static::isIterable($closure);

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

return $closure;
}

/**
* @param Closure|null $closure
*
* @psalm-pure
*
* @psalm-assert Closure|null $closure
*
* @return Closure|null
*
* @throws InvalidArgumentException
*/
public static function nullOrNotStatic(mixed $closure, string $message = ''): mixed
{
null === $closure || static::notStatic($closure, $message);

return $closure;
}

/**
* @param iterable<Closure> $closure
*
* @psalm-pure
*
* @psalm-assert iterable<Closure> $closure
*
* @return iterable<Closure>
*
* @throws InvalidArgumentException
*/
public static function allNotStatic(mixed $closure, string $message = ''): mixed
{
static::isIterable($closure);

foreach ($closure as $entry) {
static::notStatic($entry, $message);
}

return $closure;
}

/**
* @param iterable<Closure|null> $closure
*
* @psalm-pure
*
* @psalm-assert iterable<Closure|null> $closure
*
* @return iterable<Closure|null>
*
* @throws InvalidArgumentException
*/
public static function allNullOrNotStatic(mixed $closure, string $message = ''): mixed
{
static::isIterable($closure);

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

return $closure;
}

/**
* @psalm-pure
*
Expand Down
4 changes: 4 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,10 @@ public static function getTests(): array
['uniqueValues', [['qwerty', 'qwerty']], false],
['uniqueValues', [['asdfg', 'qwerty']], true],
['uniqueValues', [[123, '123']], false],
['isStatic', [static function () {}], true],
['isStatic', [function () {}], false],
['notStatic', [static function () {}], false],
['notStatic', [function () {}], true],
];
}

Expand Down
13 changes: 13 additions & 0 deletions tests/static-analysis/assert-isStatic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Closure;
use Webmozart\Assert\Assert;

function isStatic(Closure $closure): Closure
{
Assert::isStatic($closure);

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

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Closure;
use Webmozart\Assert\Assert;

function notStatic(Closure $closure): Closure
{
Assert::notStatic($closure);

return $closure;
}