Debian Codegraph @

lib/3rdParty/composer/illuminate/reflection/Reflector.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use ReflectionAttribute;
6 use ReflectionClass;
7 use ReflectionEnum;
8 use ReflectionMethod;
9 use ReflectionNamedType;
10 use ReflectionUnionType;
11
12 class Reflector
13 {
14 /**
15 * This is a PHP 7.4 compatible implementation of is_callable.
16 *
17 * @param mixed $var
18 * @param bool $syntaxOnly
19 * @return bool
20 */
21 public static function isCallable($var, $syntaxOnly = false)
22 {
23 if (! is_array($var)) {
24 return is_callable($var, $syntaxOnly);
25 }
26
27 if (! isset($var[0], $var[1]) || ! is_string($var[1] ?? null)) {
28 return false;
29 }
30
31 if ($syntaxOnly &&
32 (is_string($var[0]) || is_object($var[0])) &&
33 is_string($var[1])) {
34 return true;
35 }
36
37 $class = is_object($var[0]) ? get_class($var[0]) : $var[0];
38
39 $method = $var[1];
40
41 if (! class_exists($class)) {
42 return false;
43 }
44
45 if (method_exists($class, $method)) {
46 return (new ReflectionMethod($class, $method))->isPublic();
47 }
48
49 if (is_object($var[0]) && method_exists($class, '__call')) {
50 return (new ReflectionMethod($class, '__call'))->isPublic();
51 }
52
53 if (! is_object($var[0]) && method_exists($class, '__callStatic')) {
54 return (new ReflectionMethod($class, '__callStatic'))->isPublic();
55 }
56
57 return false;
58 }
59
60 /**
61 * Get the specified class attribute, optionally following an inheritance chain.
62 *
63 * @template TAttribute of object
64 *
65 * @param object|class-string $objectOrClass
66 * @param class-string<TAttribute> $attribute
67 * @param bool $ascend
68 * @return TAttribute|null
69 */
70 public static function getClassAttribute($objectOrClass, $attribute, $ascend = false)
71 {
72 return static::getClassAttributes($objectOrClass, $attribute, $ascend)->flatten()->first();
73 }
74
75 /**
76 * Get the specified class attribute(s), optionally following an inheritance chain.
77 *
78 * @template TTarget of object
79 * @template TAttribute of object
80 *
81 * @param TTarget|class-string<TTarget> $objectOrClass
82 * @param class-string<TAttribute> $attribute
83 * @param bool $includeParents
84 * @return ($includeParents is true ? Collection<class-string<contravariant TTarget>, Collection<int, TAttribute>> : Collection<int, TAttribute>)
85 */
86 public static function getClassAttributes($objectOrClass, $attribute, $includeParents = false)
87 {
88 $reflectionClass = new ReflectionClass($objectOrClass);
89
90 $attributes = [];
91
92 do {
93 $attributes[$reflectionClass->name] = new Collection(array_map(
94 fn (ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance(),
95 $reflectionClass->getAttributes($attribute)
96 ));
97 } while ($includeParents && false !== $reflectionClass = $reflectionClass->getParentClass());
98
99 return $includeParents ? new Collection($attributes) : array_first($attributes);
100 }
101
102 /**
103 * Get the class name of the given parameter's type, if possible.
104 *
105 * @param \ReflectionParameter $parameter
106 * @return string|null
107 */
108 public static function getParameterClassName($parameter)
109 {
110 $type = $parameter->getType();
111
112 if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
113 return;
114 }
115
116 return static::getTypeName($parameter, $type);
117 }
118
119 /**
120 * Get the class names of the given parameter's type, including union types.
121 *
122 * @param \ReflectionParameter $parameter
123 * @return array
124 */
125 public static function getParameterClassNames($parameter)
126 {
127 $type = $parameter->getType();
128
129 if (! $type instanceof ReflectionUnionType) {
130 return array_filter([static::getParameterClassName($parameter)]);
131 }
132
133 $unionTypes = [];
134
135 foreach ($type->getTypes() as $listedType) {
136 if (! $listedType instanceof ReflectionNamedType || $listedType->isBuiltin()) {
137 continue;
138 }
139
140 $unionTypes[] = static::getTypeName($parameter, $listedType);
141 }
142
143 return array_filter($unionTypes);
144 }
145
146 /**
147 * Get the given type's class name.
148 *
149 * @param \ReflectionParameter $parameter
150 * @param \ReflectionNamedType $type
151 * @return string
152 */
153 protected static function getTypeName($parameter, $type)
154 {
155 $name = $type->getName();
156
157 if (! is_null($class = $parameter->getDeclaringClass())) {
158 if ($name === 'self') {
159 return $class->getName();
160 }
161
162 if ($name === 'parent' && $parent = $class->getParentClass()) {
163 return $parent->getName();
164 }
165 }
166
167 return $name;
168 }
169
170 /**
171 * Determine if the parameter's type is a subclass of the given type.
172 *
173 * @param \ReflectionParameter $parameter
174 * @param string $className
175 * @return bool
176 */
177 public static function isParameterSubclassOf($parameter, $className)
178 {
179 $paramClassName = static::getParameterClassName($parameter);
180
181 return $paramClassName
182 && (class_exists($paramClassName) || interface_exists($paramClassName))
183 && (new ReflectionClass($paramClassName))->isSubclassOf($className);
184 }
185
186 /**
187 * Determine if the parameter's type is a Backed Enum with a string backing type.
188 *
189 * @param \ReflectionParameter $parameter
190 * @return bool
191 */
192 public static function isParameterBackedEnumWithStringBackingType($parameter)
193 {
194 if (! $parameter->getType() instanceof ReflectionNamedType) {
195 return false;
196 }
197
198 $backedEnumClass = $parameter->getType()?->getName();
199
200 if (is_null($backedEnumClass)) {
201 return false;
202 }
203
204 if (enum_exists($backedEnumClass)) {
205 $reflectionBackedEnum = new ReflectionEnum($backedEnumClass);
206
207 return $reflectionBackedEnum->isBacked()
208 && $reflectionBackedEnum->getBackingType()->getName() == 'string';
209 }
210
211 return false;
212 }
213 }