-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClosureWrapperTrait.php
52 lines (45 loc) · 1.22 KB
/
ClosureWrapperTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace YourNamespace\App\Tests\Traits;
use Opis\Closure\SerializableClosure;
/**
* Trait ClosureWrapperTrait.
*
* Provides wrapper for closures that allows to use them as arguments in data
* providers.
*
* The methods are deliberately named as short as possible to avoid long lines
* in data providers.
*
* fnw() stands for "function wrap" and fnu() stands for "function unwrap".
*
* @see https://github.com/sebastianbergmann/phpunit/issues/2739
*
* @phpstan-ignore trait.unused
*/
trait ClosureWrapperTrait {
/**
* Wrap closure into serializable object.
*
* @param callable $closure
* The closure to wrap.
*
* @return \Opis\Closure\SerializableClosure
* The serializable closure.
*/
public static function fnw(callable $closure): SerializableClosure {
return new SerializableClosure($closure);
}
/**
* Unwrap closure into serializable object.
*
* @param callable|null $callback
* The closure to unwrap.
*
* @return callable|null
* The unwrapped closure.
*/
public static function fnu(callable|null $callback): mixed {
return $callback && $callback instanceof SerializableClosure ? $callback->getClosure() : $callback;
}
}