Debian Codegraph @

lib/3rdParty/composer/illuminate/reflection/Traits/ReflectsClosures.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support\Traits;
4
5 use Closure;
6 use Illuminate\Support\Collection;
7 use Illuminate\Support\Reflector;
8 use ReflectionFunction;
9 use ReflectionIntersectionType;
10 use ReflectionUnionType;
11 use RuntimeException;
12
13 trait ReflectsClosures
14 {
15 /**
16 * Get the class name of the first parameter of the given Closure.
17 *
18 * @param \Closure $closure
19 * @return string
20 *
21 * @throws \ReflectionException
22 * @throws \RuntimeException
23 */
24 protected function firstClosureParameterType(Closure $closure)
25 {
26 $types = array_values($this->closureParameterTypes($closure));
27
28 if (! $types) {
29 throw new RuntimeException('The given Closure has no parameters.');
30 }
31
32 if ($types[0] === null) {
33 throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
34 }
35
36 return $types[0];
37 }
38
39 /**
40 * Get the class names of the first parameter of the given Closure, including union types.
41 *
42 * @param \Closure $closure
43 * @return array
44 *
45 * @throws \ReflectionException
46 * @throws \RuntimeException
47 */
48 protected function firstClosureParameterTypes(Closure $closure)
49 {
50 $reflection = new ReflectionFunction($closure);
51
52 $types = (new Collection($reflection->getParameters()))
53 ->mapWithKeys(function ($parameter) {
54 if ($parameter->isVariadic()) {
55 return [$parameter->getName() => null];
56 }
57
58 return [$parameter->getName() => Reflector::getParameterClassNames($parameter)];
59 })
60 ->filter()
61 ->values()
62 ->all();
63
64 if (empty($types)) {
65 throw new RuntimeException('The given Closure has no parameters.');
66 }
67
68 if (isset($types[0]) && empty($types[0])) {
69 throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
70 }
71
72 return $types[0];
73 }
74
75 /**
76 * Get the class names / types of the parameters of the given Closure.
77 *
78 * @param \Closure $closure
79 * @return array
80 *
81 * @throws \ReflectionException
82 */
83 protected function closureParameterTypes(Closure $closure)
84 {
85 $reflection = new ReflectionFunction($closure);
86
87 return (new Collection($reflection->getParameters()))
88 ->mapWithKeys(function ($parameter) {
89 if ($parameter->isVariadic()) {
90 return [$parameter->getName() => null];
91 }
92
93 return [$parameter->getName() => Reflector::getParameterClassName($parameter)];
94 })
95 ->all();
96 }
97
98 /**
99 * Get the class names / types of the return type of the given Closure.
100 *
101 * @param \Closure $closure
102 * @return list<class-string>
103 *
104 * @throws \ReflectionException
105 */
106 protected function closureReturnTypes(Closure $closure)
107 {
108 $reflection = new ReflectionFunction($closure);
109
110 if ($reflection->getReturnType() === null ||
111 $reflection->getReturnType() instanceof ReflectionIntersectionType) {
112 return [];
113 }
114
115 $types = $reflection->getReturnType() instanceof ReflectionUnionType
116 ? $reflection->getReturnType()->getTypes()
117 : [$reflection->getReturnType()];
118
119 return (new Collection($types))
120 ->reject(fn ($type) => $type->isBuiltin())
121 ->reject(fn ($type) => in_array($type->getName(), ['static', 'self']))
122 ->map(fn ($type) => $type->getName())
123 ->values()
124 ->all();
125 }
126 }