Skip to content

Commit 9b07f70

Browse files
authored
adds isStatic and isNotStatic (#310)
1 parent 911bf98 commit 9b07f70

File tree

6 files changed

+211
-3
lines changed

6 files changed

+211
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ Method | Description
212212

213213
### Function Assertions
214214

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

219221
### Collection Assertions
220222

src/Assert.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
namespace Webmozart\Assert;
1515

1616
use ArrayAccess;
17+
use Closure;
1718
use Countable;
1819
use DateTime;
1920
use DateTimeImmutable;
21+
use ReflectionFunction;
2022
use ReflectionProperty;
2123
use Throwable;
2224
use Traversable;
@@ -2042,6 +2044,56 @@ public static function isMap(mixed $array, string $message = ''): array
20422044
return $array;
20432045
}
20442046

2047+
/**
2048+
* @param Closure $closure
2049+
*
2050+
* @psalm-pure
2051+
*
2052+
* @psalm-assert static Closure $closure
2053+
*
2054+
* @psalm-return static Closure
2055+
*
2056+
* @throws InvalidArgumentException
2057+
*/
2058+
public static function isStatic(mixed $closure, string $message = ''): Closure
2059+
{
2060+
static::isCallable($closure, $message);
2061+
$reflection = new ReflectionFunction($closure);
2062+
2063+
if (!$reflection->isStatic()) {
2064+
static::reportInvalidArgument(
2065+
$message ?: 'Closure is not static.'
2066+
);
2067+
}
2068+
2069+
return $closure;
2070+
}
2071+
2072+
/**
2073+
* @param Closure $closure
2074+
*
2075+
* @psalm-pure
2076+
*
2077+
* @psalm-assert Closure $closure
2078+
*
2079+
* @psalm-return Closure
2080+
*
2081+
* @throws InvalidArgumentException
2082+
*/
2083+
public static function notStatic(mixed $closure, string $message = ''): Closure
2084+
{
2085+
static::isCallable($closure, $message);
2086+
$reflection = new ReflectionFunction($closure);
2087+
2088+
if ($reflection->isStatic()) {
2089+
static::reportInvalidArgument(
2090+
$message ?: 'Closure is not static.'
2091+
);
2092+
}
2093+
2094+
return $closure;
2095+
}
2096+
20452097
/**
20462098
* @psalm-pure
20472099
*

src/Mixin.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4955,6 +4955,130 @@ public static function allNullOrIsMap(mixed $array, string $message = ''): mixed
49554955
return $array;
49564956
}
49574957

4958+
/**
4959+
* @param Closure|null $closure
4960+
*
4961+
* @psalm-pure
4962+
*
4963+
* @psalm-assert static Closure|null $closure
4964+
*
4965+
* @return static Closure|null
4966+
*
4967+
* @throws InvalidArgumentException
4968+
*/
4969+
public static function nullOrIsStatic(mixed $closure, string $message = ''): mixed
4970+
{
4971+
null === $closure || static::isStatic($closure, $message);
4972+
4973+
return $closure;
4974+
}
4975+
4976+
/**
4977+
* @param iterable<Closure> $closure
4978+
*
4979+
* @psalm-pure
4980+
*
4981+
* @psalm-assert iterable<static Closure> $closure
4982+
*
4983+
* @return iterable<static Closure>
4984+
*
4985+
* @throws InvalidArgumentException
4986+
*/
4987+
public static function allIsStatic(mixed $closure, string $message = ''): mixed
4988+
{
4989+
static::isIterable($closure);
4990+
4991+
foreach ($closure as $entry) {
4992+
static::isStatic($entry, $message);
4993+
}
4994+
4995+
return $closure;
4996+
}
4997+
4998+
/**
4999+
* @param iterable<Closure|null> $closure
5000+
*
5001+
* @psalm-pure
5002+
*
5003+
* @psalm-assert iterable<static Closure|null> $closure
5004+
*
5005+
* @return iterable<static Closure|null>
5006+
*
5007+
* @throws InvalidArgumentException
5008+
*/
5009+
public static function allNullOrIsStatic(mixed $closure, string $message = ''): mixed
5010+
{
5011+
static::isIterable($closure);
5012+
5013+
foreach ($closure as $entry) {
5014+
null === $entry || static::isStatic($entry, $message);
5015+
}
5016+
5017+
return $closure;
5018+
}
5019+
5020+
/**
5021+
* @param Closure|null $closure
5022+
*
5023+
* @psalm-pure
5024+
*
5025+
* @psalm-assert Closure|null $closure
5026+
*
5027+
* @return Closure|null
5028+
*
5029+
* @throws InvalidArgumentException
5030+
*/
5031+
public static function nullOrNotStatic(mixed $closure, string $message = ''): mixed
5032+
{
5033+
null === $closure || static::notStatic($closure, $message);
5034+
5035+
return $closure;
5036+
}
5037+
5038+
/**
5039+
* @param iterable<Closure> $closure
5040+
*
5041+
* @psalm-pure
5042+
*
5043+
* @psalm-assert iterable<Closure> $closure
5044+
*
5045+
* @return iterable<Closure>
5046+
*
5047+
* @throws InvalidArgumentException
5048+
*/
5049+
public static function allNotStatic(mixed $closure, string $message = ''): mixed
5050+
{
5051+
static::isIterable($closure);
5052+
5053+
foreach ($closure as $entry) {
5054+
static::notStatic($entry, $message);
5055+
}
5056+
5057+
return $closure;
5058+
}
5059+
5060+
/**
5061+
* @param iterable<Closure|null> $closure
5062+
*
5063+
* @psalm-pure
5064+
*
5065+
* @psalm-assert iterable<Closure|null> $closure
5066+
*
5067+
* @return iterable<Closure|null>
5068+
*
5069+
* @throws InvalidArgumentException
5070+
*/
5071+
public static function allNullOrNotStatic(mixed $closure, string $message = ''): mixed
5072+
{
5073+
static::isIterable($closure);
5074+
5075+
foreach ($closure as $entry) {
5076+
null === $entry || static::notStatic($entry, $message);
5077+
}
5078+
5079+
return $closure;
5080+
}
5081+
49585082
/**
49595083
* @psalm-pure
49605084
*

tests/AssertTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,10 @@ public static function getTests(): array
621621
['uniqueValues', [['qwerty', 'qwerty']], false],
622622
['uniqueValues', [['asdfg', 'qwerty']], true],
623623
['uniqueValues', [[123, '123']], false],
624+
['isStatic', [static function () {}], true],
625+
['isStatic', [function () {}], false],
626+
['notStatic', [static function () {}], false],
627+
['notStatic', [function () {}], true],
624628
];
625629
}
626630

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Webmozart\Assert\Tests\StaticAnalysis;
4+
5+
use Closure;
6+
use Webmozart\Assert\Assert;
7+
8+
function isStatic(Closure $closure): Closure
9+
{
10+
Assert::isStatic($closure);
11+
12+
return $closure;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Webmozart\Assert\Tests\StaticAnalysis;
4+
5+
use Closure;
6+
use Webmozart\Assert\Assert;
7+
8+
function notStatic(Closure $closure): Closure
9+
{
10+
Assert::notStatic($closure);
11+
12+
return $closure;
13+
}

0 commit comments

Comments
 (0)