Skip to content
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

adds isStatic and isNotStatic #310

Open
wants to merge 5 commits 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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,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.
`isNotStatic($closure, $message = '')` | Check that a function is not static.

### Collection Assertions

Expand Down
36 changes: 36 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use DateTime;
use DateTimeImmutable;
use Exception;
use ReflectionFunction;
use ResourceBundle;
use SimpleXMLElement;
use Throwable;
Expand Down Expand Up @@ -1940,6 +1941,41 @@ public static function isMap($array, $message = '')
}
}

/**
* @param Closure $closure
* @param string $message
* @return void
* @throws InvalidArgumentException
*/
public static function isStatic(Closure $closure, $message = '')
{
$reflection = new ReflectionFunction($closure);

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

/**
* @param Closure $closure
* @param string $message
* @return void
* @throws InvalidArgumentException
*/
public static function isNotStatic(Closure $closure, $message = '')
{
Assert::isCallable($closure);
$reflection = new ReflectionFunction($closure);

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

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

/**
* @param Closure|null $closure
* @param string $message
*
* @return void
* @return void
*
* @throws InvalidArgumentException
*/
public static function nullOrIsStatic($closure, $message = '')
{
null === $closure || static::isStatic($closure, $message);
}

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

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

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

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

/**
* @param Closure|null $closure
* @param string $message
*
* @return void
* @return void
*
* @throws InvalidArgumentException
*/
public static function nullOrIsNotStatic($closure, $message = '')
{
null === $closure || static::isNotStatic($closure, $message);
}

/**
* @param iterable<Closure> $closure
* @param string $message
*
* @return void
* @return void
*
* @throws InvalidArgumentException
*/
public static function allIsNotStatic($closure, $message = '')
{
static::isIterable($closure);

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

/**
* @param iterable<Closure|null> $closure
* @param string $message
*
* @return void
* @return void
*
* @throws InvalidArgumentException
*/
public static function allNullOrIsNotStatic($closure, $message = '')
{
static::isIterable($closure);

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

/**
* @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 @@ -548,6 +548,10 @@ public function getTests()
array('throws', array(function () { trigger_error('test'); }, 'Throwable'), true, false, 70000),
array('throws', array(function () { trigger_error('test'); }, 'Unthrowable'), false, false, 70000),
array('throws', array(function () { throw new Error(); }, 'Throwable'), true, true, 70000),
array('isStatic', array(static function () {}), true),
array('isStatic', array(function () {}), false),
array('isNotStatic', array(static function () {}), false),
array('isNotStatic', array(function () {}), true),
array('ip', array('192.168.0.1'), true),
array('ip', array(new ToStringClass('192.168.0.1')), true),
array('ip', array('255.255.255.255'), true),
Expand Down
20 changes: 20 additions & 0 deletions tests/static-analysis/assert-isStatic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Closure;
use Webmozart\Assert\Assert;

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

return $closure;
}

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

return $closure;
}