19declare(strict_types=1);
25use ReflectionFunction;
32trait CheckClosureTrait
34 private function checkClosureForSignature(Closure
$c,
string $signature): void
36 $error_message =
'first argument and return type of closure must be type-hinted to ' . $signature;
38 $r =
new ReflectionFunction(
$c);
39 if (count($r->getParameters()) !== 1) {
40 throw new LogicException($error_message);
42 $first_param_type = $r->getParameters()[0]->getType();
43 if ($first_param_type instanceof ReflectionType && $first_param_type->getName() !== $signature) {
44 throw new LogicException($error_message);
46 $return_type = $r->getReturnType();
47 if ($return_type ===
null) {
48 throw new LogicException($error_message);
50 if ($return_type->getName() !== $signature) {
51 throw new LogicException($error_message);
54 throw new LogicException($error_message);