|
10 | 10 | trait ReflectsClosures |
11 | 11 | { |
12 | 12 | /** |
13 | | - * Get the class names / types of the parameters of the given Closure. |
| 13 | + * Get the class name of the first parameter of the given Closure. |
14 | 14 | * |
15 | 15 | * @param \Closure $closure |
16 | | - * @return array |
| 16 | + * @return string |
17 | 17 | * |
18 | 18 | * @throws \ReflectionException |
| 19 | + * @throws \RuntimeException |
19 | 20 | */ |
20 | | - protected function closureParameterTypes(Closure $closure) |
| 21 | + protected function firstClosureParameterType(Closure $closure) |
21 | 22 | { |
22 | | - $reflection = new ReflectionFunction($closure); |
| 23 | + $types = array_values($this->closureParameterTypes($closure)); |
23 | 24 |
|
24 | | - return collect($reflection->getParameters())->mapWithKeys(function ($parameter) { |
25 | | - if ($parameter->isVariadic()) { |
26 | | - return [$parameter->getName() => null]; |
27 | | - } |
| 25 | + if (! $types) { |
| 26 | + throw new RuntimeException('The given Closure has no parameters.'); |
| 27 | + } |
28 | 28 |
|
29 | | - return [$parameter->getName() => Reflector::getParameterClassName($parameter)]; |
30 | | - })->all(); |
| 29 | + if ($types[0] === null) { |
| 30 | + throw new RuntimeException('The first parameter of the given Closure is missing a type hint.'); |
| 31 | + } |
| 32 | + |
| 33 | + return $types[0]; |
31 | 34 | } |
32 | 35 |
|
33 | 36 | /** |
34 | | - * Get the class name of the first parameter of the given Closure. |
| 37 | + * Get the class names of the first parameter of the given Closure, including union types. |
35 | 38 | * |
36 | 39 | * @param \Closure $closure |
37 | | - * @return string |
| 40 | + * @return array |
38 | 41 | * |
39 | 42 | * @throws \ReflectionException |
40 | 43 | * @throws \RuntimeException |
41 | 44 | */ |
42 | | - protected function firstClosureParameterType(Closure $closure) |
| 45 | + protected function firstClosureParameterTypes(Closure $closure) |
43 | 46 | { |
44 | | - $types = array_values($this->closureParameterTypes($closure)); |
| 47 | + $reflection = new ReflectionFunction($closure); |
45 | 48 |
|
46 | | - if (! $types) { |
| 49 | + $types = collect($reflection->getParameters())->mapWithKeys(function ($parameter) { |
| 50 | + if ($parameter->isVariadic()) { |
| 51 | + return [$parameter->getName() => null]; |
| 52 | + } |
| 53 | + |
| 54 | + return [$parameter->getName() => Reflector::getParameterClassNames($parameter)]; |
| 55 | + })->filter()->values()->all(); |
| 56 | + |
| 57 | + if (empty($types)) { |
47 | 58 | throw new RuntimeException('The given Closure has no parameters.'); |
48 | 59 | } |
49 | 60 |
|
50 | | - if ($types[0] === null) { |
| 61 | + if (isset($types[0]) && empty($types[0])) { |
51 | 62 | throw new RuntimeException('The first parameter of the given Closure is missing a type hint.'); |
52 | 63 | } |
53 | 64 |
|
54 | 65 | return $types[0]; |
55 | 66 | } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the class names / types of the parameters of the given Closure. |
| 70 | + * |
| 71 | + * @param \Closure $closure |
| 72 | + * @return array |
| 73 | + * |
| 74 | + * @throws \ReflectionException |
| 75 | + */ |
| 76 | + protected function closureParameterTypes(Closure $closure) |
| 77 | + { |
| 78 | + $reflection = new ReflectionFunction($closure); |
| 79 | + |
| 80 | + return collect($reflection->getParameters())->mapWithKeys(function ($parameter) { |
| 81 | + if ($parameter->isVariadic()) { |
| 82 | + return [$parameter->getName() => null]; |
| 83 | + } |
| 84 | + |
| 85 | + return [$parameter->getName() => Reflector::getParameterClassName($parameter)]; |
| 86 | + })->all(); |
| 87 | + } |
56 | 88 | } |
0 commit comments