Skip to content

Commit 68b79e9

Browse files
authored
Clean up new isStatic/notStatic (#329)
1 parent 9b07f70 commit 68b79e9

File tree

5 files changed

+90
-79
lines changed

5 files changed

+90
-79
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Changelog
1616
- All assertion methods now return the checked value.
1717
- Added `notInArray` and `notOneOf`.
1818
- Added `isInitialized`, to check if a class property is initialized.
19-
- Added `notNegativeInteger` and `negativeInteger`
19+
- Added `negativeInteger` and `notNegativeInteger`
20+
- Added `isStatic` and `notStatic`
2021

2122
### Fixed
2223

src/Assert.php

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,53 +2045,55 @@ public static function isMap(mixed $array, string $message = ''): array
20452045
}
20462046

20472047
/**
2048-
* @param Closure $closure
2048+
* @psalm-assert callable $callable
20492049
*
2050-
* @psalm-pure
2051-
*
2052-
* @psalm-assert static Closure $closure
2050+
* @param Closure|callable $callable
20532051
*
2054-
* @psalm-return static Closure
2052+
* @return Closure|callable-string
20552053
*
20562054
* @throws InvalidArgumentException
20572055
*/
2058-
public static function isStatic(mixed $closure, string $message = ''): Closure
2056+
public static function isStatic(mixed $callable, string $message = ''): Closure|string
20592057
{
2060-
static::isCallable($closure, $message);
2061-
$reflection = new ReflectionFunction($closure);
2058+
static::isCallable($callable, $message);
2059+
2060+
$callable = static::callableToClosure($callable);
2061+
2062+
$reflection = new ReflectionFunction($callable);
20622063

20632064
if (!$reflection->isStatic()) {
20642065
static::reportInvalidArgument(
20652066
$message ?: 'Closure is not static.'
20662067
);
20672068
}
20682069

2069-
return $closure;
2070+
return $callable;
20702071
}
20712072

20722073
/**
2073-
* @param Closure $closure
2074-
*
2075-
* @psalm-pure
2074+
* @psalm-assert callable $callable
20762075
*
2077-
* @psalm-assert Closure $closure
2076+
* @param Closure|callable $callable
20782077
*
2079-
* @psalm-return Closure
2078+
* @return Closure|callable-string
20802079
*
20812080
* @throws InvalidArgumentException
20822081
*/
2083-
public static function notStatic(mixed $closure, string $message = ''): Closure
2082+
public static function notStatic(mixed $callable, string $message = ''): Closure|string
20842083
{
2085-
static::isCallable($closure, $message);
2086-
$reflection = new ReflectionFunction($closure);
2084+
static::isCallable($callable, $message);
2085+
2086+
$callable = static::callableToClosure($callable);
2087+
2088+
$reflection = new ReflectionFunction($callable);
20872089

20882090
if ($reflection->isStatic()) {
20892091
static::reportInvalidArgument(
20902092
$message ?: 'Closure is not static.'
20912093
);
20922094
}
20932095

2094-
return $closure;
2096+
return $callable;
20952097
}
20962098

20972099
/**
@@ -2172,6 +2174,24 @@ public static function throws(mixed $expression, string $class = Throwable::clas
21722174
));
21732175
}
21742176

2177+
/**
2178+
* @psalm-pure
2179+
*
2180+
* @return Closure|callable-string
2181+
*/
2182+
protected static function callableToClosure(callable $callable): Closure|string
2183+
{
2184+
if (\is_string($callable) && \function_exists($callable)) {
2185+
return $callable;
2186+
}
2187+
2188+
if ($callable instanceof Closure) {
2189+
return $callable;
2190+
}
2191+
2192+
return $callable(...);
2193+
}
2194+
21752195
/**
21762196
* @psalm-pure
21772197
*/

src/Mixin.php

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4956,127 +4956,115 @@ public static function allNullOrIsMap(mixed $array, string $message = ''): mixed
49564956
}
49574957

49584958
/**
4959-
* @param Closure|null $closure
4959+
* @psalm-assert callable|null $callable
49604960
*
4961-
* @psalm-pure
4962-
*
4963-
* @psalm-assert static Closure|null $closure
4961+
* @param Closure|callable|null $callable
49644962
*
4965-
* @return static Closure|null
4963+
* @return callable|null
49664964
*
49674965
* @throws InvalidArgumentException
49684966
*/
4969-
public static function nullOrIsStatic(mixed $closure, string $message = ''): mixed
4967+
public static function nullOrIsStatic(mixed $callable, string $message = ''): mixed
49704968
{
4971-
null === $closure || static::isStatic($closure, $message);
4969+
null === $callable || static::isStatic($callable, $message);
49724970

4973-
return $closure;
4971+
return $callable;
49744972
}
49754973

49764974
/**
4977-
* @param iterable<Closure> $closure
4978-
*
4979-
* @psalm-pure
4975+
* @psalm-assert iterable<callable> $callable
49804976
*
4981-
* @psalm-assert iterable<static Closure> $closure
4977+
* @param iterable<Closure|callable> $callable
49824978
*
4983-
* @return iterable<static Closure>
4979+
* @return iterable<callable>
49844980
*
49854981
* @throws InvalidArgumentException
49864982
*/
4987-
public static function allIsStatic(mixed $closure, string $message = ''): mixed
4983+
public static function allIsStatic(mixed $callable, string $message = ''): mixed
49884984
{
4989-
static::isIterable($closure);
4985+
static::isIterable($callable);
49904986

4991-
foreach ($closure as $entry) {
4987+
foreach ($callable as $entry) {
49924988
static::isStatic($entry, $message);
49934989
}
49944990

4995-
return $closure;
4991+
return $callable;
49964992
}
49974993

49984994
/**
4999-
* @param iterable<Closure|null> $closure
5000-
*
5001-
* @psalm-pure
4995+
* @psalm-assert iterable<callable|null> $callable
50024996
*
5003-
* @psalm-assert iterable<static Closure|null> $closure
4997+
* @param iterable<Closure|callable|null> $callable
50044998
*
5005-
* @return iterable<static Closure|null>
4999+
* @return iterable<callable|null>
50065000
*
50075001
* @throws InvalidArgumentException
50085002
*/
5009-
public static function allNullOrIsStatic(mixed $closure, string $message = ''): mixed
5003+
public static function allNullOrIsStatic(mixed $callable, string $message = ''): mixed
50105004
{
5011-
static::isIterable($closure);
5005+
static::isIterable($callable);
50125006

5013-
foreach ($closure as $entry) {
5007+
foreach ($callable as $entry) {
50145008
null === $entry || static::isStatic($entry, $message);
50155009
}
50165010

5017-
return $closure;
5011+
return $callable;
50185012
}
50195013

50205014
/**
5021-
* @param Closure|null $closure
5015+
* @psalm-assert callable|null $callable
50225016
*
5023-
* @psalm-pure
5024-
*
5025-
* @psalm-assert Closure|null $closure
5017+
* @param Closure|callable|null $callable
50265018
*
5027-
* @return Closure|null
5019+
* @return callable|null
50285020
*
50295021
* @throws InvalidArgumentException
50305022
*/
5031-
public static function nullOrNotStatic(mixed $closure, string $message = ''): mixed
5023+
public static function nullOrNotStatic(mixed $callable, string $message = ''): mixed
50325024
{
5033-
null === $closure || static::notStatic($closure, $message);
5025+
null === $callable || static::notStatic($callable, $message);
50345026

5035-
return $closure;
5027+
return $callable;
50365028
}
50375029

50385030
/**
5039-
* @param iterable<Closure> $closure
5040-
*
5041-
* @psalm-pure
5031+
* @psalm-assert iterable<callable> $callable
50425032
*
5043-
* @psalm-assert iterable<Closure> $closure
5033+
* @param iterable<Closure|callable> $callable
50445034
*
5045-
* @return iterable<Closure>
5035+
* @return iterable<callable>
50465036
*
50475037
* @throws InvalidArgumentException
50485038
*/
5049-
public static function allNotStatic(mixed $closure, string $message = ''): mixed
5039+
public static function allNotStatic(mixed $callable, string $message = ''): mixed
50505040
{
5051-
static::isIterable($closure);
5041+
static::isIterable($callable);
50525042

5053-
foreach ($closure as $entry) {
5043+
foreach ($callable as $entry) {
50545044
static::notStatic($entry, $message);
50555045
}
50565046

5057-
return $closure;
5047+
return $callable;
50585048
}
50595049

50605050
/**
5061-
* @param iterable<Closure|null> $closure
5062-
*
5063-
* @psalm-pure
5051+
* @psalm-assert iterable<callable|null> $callable
50645052
*
5065-
* @psalm-assert iterable<Closure|null> $closure
5053+
* @param iterable<Closure|callable|null> $callable
50665054
*
5067-
* @return iterable<Closure|null>
5055+
* @return iterable<callable|null>
50685056
*
50695057
* @throws InvalidArgumentException
50705058
*/
5071-
public static function allNullOrNotStatic(mixed $closure, string $message = ''): mixed
5059+
public static function allNullOrNotStatic(mixed $callable, string $message = ''): mixed
50725060
{
5073-
static::isIterable($closure);
5061+
static::isIterable($callable);
50745062

5075-
foreach ($closure as $entry) {
5063+
foreach ($callable as $entry) {
50765064
null === $entry || static::notStatic($entry, $message);
50775065
}
50785066

5079-
return $closure;
5067+
return $callable;
50805068
}
50815069

50825070
/**

tests/static-analysis/assert-isStatic.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use Closure;
66
use Webmozart\Assert\Assert;
77

8-
function isStatic(Closure $closure): Closure
8+
/**
9+
* @return Closure|callable-string
10+
*/
11+
function isStatic(mixed $closure): Closure|string
912
{
10-
Assert::isStatic($closure);
11-
12-
return $closure;
13+
return Assert::isStatic($closure);
1314
}

tests/static-analysis/assert-notStatic.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use Closure;
66
use Webmozart\Assert\Assert;
77

8-
function notStatic(Closure $closure): Closure
8+
/**
9+
* @return Closure|callable-string
10+
*/
11+
function notStatic(mixed $closure): Closure|string
912
{
10-
Assert::notStatic($closure);
11-
12-
return $closure;
13+
return Assert::notStatic($closure);
1314
}

0 commit comments

Comments
 (0)