PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.6
Kubio AI Page Builder v2.8.6
2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / vendor / symfony / var-dumper / Caster / ReflectionCaster.php
kubio / vendor / symfony / var-dumper / Caster Last commit date
AmqpCaster.php 1 year ago ArgsStub.php 1 year ago Caster.php 1 year ago ClassStub.php 1 year ago ConstStub.php 4 years ago CutArrayStub.php 4 years ago CutStub.php 4 years ago DOMCaster.php 1 year ago DateCaster.php 1 year ago DoctrineCaster.php 1 year ago DsCaster.php 1 year ago DsPairStub.php 4 years ago EnumStub.php 4 years ago ExceptionCaster.php 1 year ago FiberCaster.php 1 year ago FrameStub.php 4 years ago GmpCaster.php 1 year ago ImagineCaster.php 4 years ago ImgStub.php 1 year ago IntlCaster.php 1 year ago LinkStub.php 1 year ago MemcachedCaster.php 1 year ago MysqliCaster.php 1 year ago PdoCaster.php 1 year ago PgSqlCaster.php 1 year ago ProxyManagerCaster.php 1 year ago RdKafkaCaster.php 1 year ago RedisCaster.php 1 year ago ReflectionCaster.php 1 year ago ResourceCaster.php 1 year ago SplCaster.php 1 year ago StubCaster.php 1 year ago SymfonyCaster.php 1 year ago TraceStub.php 1 year ago UuidCaster.php 4 years ago XmlReaderCaster.php 1 year ago XmlResourceCaster.php 1 year ago
ReflectionCaster.php
449 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\VarDumper\Caster;
13
14 use Symfony\Component\VarDumper\Cloner\Stub;
15
16 /**
17 * Casts Reflector related classes to array representation.
18 *
19 * @author Nicolas Grekas <p@tchwork.com>
20 *
21 * @final
22 */
23 class ReflectionCaster
24 {
25 public const UNSET_CLOSURE_FILE_INFO = ['Closure' => __CLASS__.'::unsetClosureFileInfo'];
26
27 private const EXTRA_MAP = [
28 'docComment' => 'getDocComment',
29 'extension' => 'getExtensionName',
30 'isDisabled' => 'isDisabled',
31 'isDeprecated' => 'isDeprecated',
32 'isInternal' => 'isInternal',
33 'isUserDefined' => 'isUserDefined',
34 'isGenerator' => 'isGenerator',
35 'isVariadic' => 'isVariadic',
36 ];
37
38 public static function castClosure(\Closure $c, array $a, Stub $stub, bool $isNested, int $filter = 0)
39 {
40 $prefix = Caster::PREFIX_VIRTUAL;
41 $c = new \ReflectionFunction($c);
42
43 $a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter);
44
45 if (!str_contains($c->name, '{closure')) {
46 $stub->class = isset($a[$prefix.'class']) ? $a[$prefix.'class']->value.'::'.$c->name : $c->name;
47 unset($a[$prefix.'class']);
48 }
49 unset($a[$prefix.'extra']);
50
51 $stub->class .= self::getSignature($a);
52
53 if ($f = $c->getFileName()) {
54 $stub->attr['file'] = $f;
55 $stub->attr['line'] = $c->getStartLine();
56 }
57
58 unset($a[$prefix.'parameters']);
59
60 if ($filter & Caster::EXCLUDE_VERBOSE) {
61 $stub->cut += ($c->getFileName() ? 2 : 0) + \count($a);
62
63 return [];
64 }
65
66 if ($f) {
67 $a[$prefix.'file'] = new LinkStub($f, $c->getStartLine());
68 $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
69 }
70
71 return $a;
72 }
73
74 public static function unsetClosureFileInfo(\Closure $c, array $a)
75 {
76 unset($a[Caster::PREFIX_VIRTUAL.'file'], $a[Caster::PREFIX_VIRTUAL.'line']);
77
78 return $a;
79 }
80
81 public static function castGenerator(\Generator $c, array $a, Stub $stub, bool $isNested)
82 {
83 // Cannot create ReflectionGenerator based on a terminated Generator
84 try {
85 $reflectionGenerator = new \ReflectionGenerator($c);
86
87 return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
88 } catch (\Exception $e) {
89 $a[Caster::PREFIX_VIRTUAL.'closed'] = true;
90
91 return $a;
92 }
93 }
94
95 public static function castType(\ReflectionType $c, array $a, Stub $stub, bool $isNested)
96 {
97 $prefix = Caster::PREFIX_VIRTUAL;
98
99 if ($c instanceof \ReflectionNamedType || \PHP_VERSION_ID < 80000) {
100 $a += [
101 $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c,
102 $prefix.'allowsNull' => $c->allowsNull(),
103 $prefix.'isBuiltin' => $c->isBuiltin(),
104 ];
105 } elseif ($c instanceof \ReflectionUnionType || $c instanceof \ReflectionIntersectionType) {
106 $a[$prefix.'allowsNull'] = $c->allowsNull();
107 self::addMap($a, $c, [
108 'types' => 'getTypes',
109 ]);
110 } else {
111 $a[$prefix.'allowsNull'] = $c->allowsNull();
112 }
113
114 return $a;
115 }
116
117 public static function castAttribute(\ReflectionAttribute $c, array $a, Stub $stub, bool $isNested)
118 {
119 $map = [
120 'name' => 'getName',
121 'arguments' => 'getArguments',
122 ];
123
124 if (\PHP_VERSION_ID >= 80400) {
125 unset($map['name']);
126 }
127
128 self::addMap($a, $c, $map);
129
130 return $a;
131 }
132
133 public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, bool $isNested)
134 {
135 $prefix = Caster::PREFIX_VIRTUAL;
136
137 if ($c->getThis()) {
138 $a[$prefix.'this'] = new CutStub($c->getThis());
139 }
140 $function = $c->getFunction();
141 $frame = [
142 'class' => $function->class ?? null,
143 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
144 'function' => $function->name,
145 'file' => $c->getExecutingFile(),
146 'line' => $c->getExecutingLine(),
147 ];
148 if ($trace = $c->getTrace(\DEBUG_BACKTRACE_IGNORE_ARGS)) {
149 $function = new \ReflectionGenerator($c->getExecutingGenerator());
150 array_unshift($trace, [
151 'function' => 'yield',
152 'file' => $function->getExecutingFile(),
153 'line' => $function->getExecutingLine() - (int) (\PHP_VERSION_ID < 80100),
154 ]);
155 $trace[] = $frame;
156 $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
157 } else {
158 $function = new FrameStub($frame, false, true);
159 $function = ExceptionCaster::castFrameStub($function, [], $function, true);
160 $a[$prefix.'executing'] = $function[$prefix.'src'];
161 }
162
163 $a[Caster::PREFIX_VIRTUAL.'closed'] = false;
164
165 return $a;
166 }
167
168 public static function castClass(\ReflectionClass $c, array $a, Stub $stub, bool $isNested, int $filter = 0)
169 {
170 $prefix = Caster::PREFIX_VIRTUAL;
171
172 if ($n = \Reflection::getModifierNames($c->getModifiers())) {
173 $a[$prefix.'modifiers'] = implode(' ', $n);
174 }
175
176 self::addMap($a, $c, [
177 'extends' => 'getParentClass',
178 'implements' => 'getInterfaceNames',
179 'constants' => 'getReflectionConstants',
180 ]);
181
182 foreach ($c->getProperties() as $n) {
183 $a[$prefix.'properties'][$n->name] = $n;
184 }
185
186 foreach ($c->getMethods() as $n) {
187 $a[$prefix.'methods'][$n->name] = $n;
188 }
189
190 self::addAttributes($a, $c, $prefix);
191
192 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
193 self::addExtra($a, $c);
194 }
195
196 return $a;
197 }
198
199 public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, bool $isNested, int $filter = 0)
200 {
201 $prefix = Caster::PREFIX_VIRTUAL;
202
203 self::addMap($a, $c, [
204 'returnsReference' => 'returnsReference',
205 'returnType' => 'getReturnType',
206 'class' => \PHP_VERSION_ID >= 80111 ? 'getClosureCalledClass' : 'getClosureScopeClass',
207 'this' => 'getClosureThis',
208 ]);
209
210 if (isset($a[$prefix.'returnType'])) {
211 $v = $a[$prefix.'returnType'];
212 $v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
213 $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
214 }
215 if (isset($a[$prefix.'class'])) {
216 $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
217 }
218 if (isset($a[$prefix.'this'])) {
219 $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
220 }
221
222 foreach ($c->getParameters() as $v) {
223 $k = '$'.$v->name;
224 if ($v->isVariadic()) {
225 $k = '...'.$k;
226 }
227 if ($v->isPassedByReference()) {
228 $k = '&'.$k;
229 }
230 $a[$prefix.'parameters'][$k] = $v;
231 }
232 if (isset($a[$prefix.'parameters'])) {
233 $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
234 }
235
236 self::addAttributes($a, $c, $prefix);
237
238 if (!($filter & Caster::EXCLUDE_VERBOSE) && $v = $c->getStaticVariables()) {
239 foreach ($v as $k => &$v) {
240 if (\is_object($v)) {
241 $a[$prefix.'use']['$'.$k] = new CutStub($v);
242 } else {
243 $a[$prefix.'use']['$'.$k] = &$v;
244 }
245 }
246 unset($v);
247 $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
248 }
249
250 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
251 self::addExtra($a, $c);
252 }
253
254 return $a;
255 }
256
257 public static function castClassConstant(\ReflectionClassConstant $c, array $a, Stub $stub, bool $isNested)
258 {
259 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
260 $a[Caster::PREFIX_VIRTUAL.'value'] = $c->getValue();
261
262 self::addAttributes($a, $c);
263
264 return $a;
265 }
266
267 public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, bool $isNested)
268 {
269 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
270
271 return $a;
272 }
273
274 public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, bool $isNested)
275 {
276 $prefix = Caster::PREFIX_VIRTUAL;
277
278 self::addMap($a, $c, [
279 'position' => 'getPosition',
280 'isVariadic' => 'isVariadic',
281 'byReference' => 'isPassedByReference',
282 'allowsNull' => 'allowsNull',
283 ]);
284
285 self::addAttributes($a, $c, $prefix);
286
287 if ($v = $c->getType()) {
288 $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
289 }
290
291 if (isset($a[$prefix.'typeHint'])) {
292 $v = $a[$prefix.'typeHint'];
293 $a[$prefix.'typeHint'] = new ClassStub($v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
294 } else {
295 unset($a[$prefix.'allowsNull']);
296 }
297
298 if ($c->isOptional()) {
299 try {
300 $a[$prefix.'default'] = $v = $c->getDefaultValue();
301 if ($c->isDefaultValueConstant() && !\is_object($v)) {
302 $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
303 }
304 if (null === $v) {
305 unset($a[$prefix.'allowsNull']);
306 }
307 } catch (\ReflectionException $e) {
308 }
309 }
310
311 return $a;
312 }
313
314 public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, bool $isNested)
315 {
316 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
317
318 self::addAttributes($a, $c);
319 self::addExtra($a, $c);
320
321 return $a;
322 }
323
324 public static function castReference(\ReflectionReference $c, array $a, Stub $stub, bool $isNested)
325 {
326 $a[Caster::PREFIX_VIRTUAL.'id'] = $c->getId();
327
328 return $a;
329 }
330
331 public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, bool $isNested)
332 {
333 self::addMap($a, $c, [
334 'version' => 'getVersion',
335 'dependencies' => 'getDependencies',
336 'iniEntries' => 'getIniEntries',
337 'isPersistent' => 'isPersistent',
338 'isTemporary' => 'isTemporary',
339 'constants' => 'getConstants',
340 'functions' => 'getFunctions',
341 'classes' => 'getClasses',
342 ]);
343
344 return $a;
345 }
346
347 public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, bool $isNested)
348 {
349 self::addMap($a, $c, [
350 'version' => 'getVersion',
351 'author' => 'getAuthor',
352 'copyright' => 'getCopyright',
353 'url' => 'getURL',
354 ]);
355
356 return $a;
357 }
358
359 public static function getSignature(array $a)
360 {
361 $prefix = Caster::PREFIX_VIRTUAL;
362 $signature = '';
363
364 if (isset($a[$prefix.'parameters'])) {
365 foreach ($a[$prefix.'parameters']->value as $k => $param) {
366 $signature .= ', ';
367 if ($type = $param->getType()) {
368 if (!$type instanceof \ReflectionNamedType) {
369 $signature .= $type.' ';
370 } else {
371 if ($param->allowsNull() && 'mixed' !== $type->getName()) {
372 $signature .= '?';
373 }
374 $signature .= substr(strrchr('\\'.$type->getName(), '\\'), 1).' ';
375 }
376 }
377 $signature .= $k;
378
379 if (!$param->isDefaultValueAvailable()) {
380 continue;
381 }
382 $v = $param->getDefaultValue();
383 $signature .= ' = ';
384
385 if ($param->isDefaultValueConstant()) {
386 $signature .= substr(strrchr('\\'.$param->getDefaultValueConstantName(), '\\'), 1);
387 } elseif (null === $v) {
388 $signature .= 'null';
389 } elseif (\is_array($v)) {
390 $signature .= $v ? '[…'.\count($v).']' : '[]';
391 } elseif (\is_string($v)) {
392 $signature .= 10 > \strlen($v) && !str_contains($v, '\\') ? "'{$v}'" : "'…".\strlen($v)."'";
393 } elseif (\is_bool($v)) {
394 $signature .= $v ? 'true' : 'false';
395 } elseif (\is_object($v)) {
396 $signature .= 'new '.substr(strrchr('\\'.get_debug_type($v), '\\'), 1);
397 } else {
398 $signature .= $v;
399 }
400 }
401 }
402 $signature = (empty($a[$prefix.'returnsReference']) ? '' : '&').'('.substr($signature, 2).')';
403
404 if (isset($a[$prefix.'returnType'])) {
405 $signature .= ': '.substr(strrchr('\\'.$a[$prefix.'returnType'], '\\'), 1);
406 }
407
408 return $signature;
409 }
410
411 private static function addExtra(array &$a, \Reflector $c)
412 {
413 $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : [];
414
415 if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
416 $x['file'] = new LinkStub($m, $c->getStartLine());
417 $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
418 }
419
420 self::addMap($x, $c, self::EXTRA_MAP, '');
421
422 if ($x) {
423 $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
424 }
425 }
426
427 private static function addMap(array &$a, object $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
428 {
429 foreach ($map as $k => $m) {
430 if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) {
431 continue;
432 }
433
434 if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
435 $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
436 }
437 }
438 }
439
440 private static function addAttributes(array &$a, \Reflector $c, string $prefix = Caster::PREFIX_VIRTUAL): void
441 {
442 if (\PHP_VERSION_ID >= 80000) {
443 foreach ($c->getAttributes() as $n) {
444 $a[$prefix.'attributes'][] = $n;
445 }
446 }
447 }
448 }
449