ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CheckClosureTrait.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Closure;
24use LogicException;
25use ReflectionFunction;
26use ReflectionType;
27use Throwable;
28
32trait CheckClosureTrait
33{
34 private function checkClosureForSignature(Closure $c, string $signature): void
35 {
36 $error_message = 'first argument and return type of closure must be type-hinted to ' . $signature;
37 try {
38 $r = new ReflectionFunction($c);
39 if (count($r->getParameters()) !== 1) {
40 throw new LogicException($error_message);
41 }
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);
45 }
46 $return_type = $r->getReturnType();
47 if ($return_type === null) {
48 throw new LogicException($error_message);
49 }
50 if ($return_type->getName() !== $signature) {
51 throw new LogicException($error_message);
52 }
53 } catch (Throwable) {
54 throw new LogicException($error_message);
55 }
56 }
57}
$c
Definition: deliver.php:25