AmqpCaster.php
2 years ago
ArgsStub.php
1 year ago
Caster.php
1 year ago
ClassStub.php
1 year ago
ConstStub.php
2 years ago
CutArrayStub.php
2 years ago
CutStub.php
1 year ago
DOMCaster.php
1 year ago
DateCaster.php
2 years ago
DoctrineCaster.php
2 years ago
DsCaster.php
2 years ago
DsPairStub.php
2 years ago
EnumStub.php
1 year ago
ExceptionCaster.php
1 year ago
FiberCaster.php
2 years ago
FrameStub.php
1 year ago
GmpCaster.php
2 years ago
ImagineCaster.php
2 years ago
ImgStub.php
2 years ago
IntlCaster.php
2 years ago
LinkStub.php
1 year ago
MemcachedCaster.php
2 years ago
MysqliCaster.php
2 years ago
PdoCaster.php
2 years ago
PgSqlCaster.php
1 year ago
ProxyManagerCaster.php
2 years ago
RdKafkaCaster.php
1 year ago
RedisCaster.php
2 years ago
ReflectionCaster.php
1 year ago
ResourceCaster.php
1 year ago
SplCaster.php
1 year ago
StubCaster.php
2 years ago
SymfonyCaster.php
2 years ago
TraceStub.php
1 year ago
UuidCaster.php
2 years ago
XmlReaderCaster.php
1 year ago
XmlResourceCaster.php
2 years ago
ReflectionCaster.php
314 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 | namespace Matomo\Dependencies\Symfony\Component\VarDumper\Caster; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\VarDumper\Cloner\Stub; |
| 14 | /** |
| 15 | * Casts Reflector related classes to array representation. |
| 16 | * |
| 17 | * @author Nicolas Grekas <p@tchwork.com> |
| 18 | * |
| 19 | * @final |
| 20 | */ |
| 21 | class ReflectionCaster |
| 22 | { |
| 23 | public const UNSET_CLOSURE_FILE_INFO = ['Closure' => __CLASS__ . '::unsetClosureFileInfo']; |
| 24 | private const EXTRA_MAP = ['docComment' => 'getDocComment', 'extension' => 'getExtensionName', 'isDisabled' => 'isDisabled', 'isDeprecated' => 'isDeprecated', 'isInternal' => 'isInternal', 'isUserDefined' => 'isUserDefined', 'isGenerator' => 'isGenerator', 'isVariadic' => 'isVariadic']; |
| 25 | public static function castClosure(\Closure $c, array $a, Stub $stub, bool $isNested, int $filter = 0) |
| 26 | { |
| 27 | $prefix = Caster::PREFIX_VIRTUAL; |
| 28 | $c = new \ReflectionFunction($c); |
| 29 | $a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter); |
| 30 | if (!str_contains($c->name, '{closure')) { |
| 31 | $stub->class = isset($a[$prefix . 'class']) ? $a[$prefix . 'class']->value . '::' . $c->name : $c->name; |
| 32 | unset($a[$prefix . 'class']); |
| 33 | } |
| 34 | unset($a[$prefix . 'extra']); |
| 35 | $stub->class .= self::getSignature($a); |
| 36 | if ($f = $c->getFileName()) { |
| 37 | $stub->attr['file'] = $f; |
| 38 | $stub->attr['line'] = $c->getStartLine(); |
| 39 | } |
| 40 | unset($a[$prefix . 'parameters']); |
| 41 | if ($filter & Caster::EXCLUDE_VERBOSE) { |
| 42 | $stub->cut += ($c->getFileName() ? 2 : 0) + \count($a); |
| 43 | return []; |
| 44 | } |
| 45 | if ($f) { |
| 46 | $a[$prefix . 'file'] = new LinkStub($f, $c->getStartLine()); |
| 47 | $a[$prefix . 'line'] = $c->getStartLine() . ' to ' . $c->getEndLine(); |
| 48 | } |
| 49 | return $a; |
| 50 | } |
| 51 | public static function unsetClosureFileInfo(\Closure $c, array $a) |
| 52 | { |
| 53 | unset($a[Caster::PREFIX_VIRTUAL . 'file'], $a[Caster::PREFIX_VIRTUAL . 'line']); |
| 54 | return $a; |
| 55 | } |
| 56 | public static function castGenerator(\Generator $c, array $a, Stub $stub, bool $isNested) |
| 57 | { |
| 58 | // Cannot create ReflectionGenerator based on a terminated Generator |
| 59 | try { |
| 60 | $reflectionGenerator = new \ReflectionGenerator($c); |
| 61 | return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested); |
| 62 | } catch (\Exception $e) { |
| 63 | $a[Caster::PREFIX_VIRTUAL . 'closed'] = \true; |
| 64 | return $a; |
| 65 | } |
| 66 | } |
| 67 | public static function castType(\ReflectionType $c, array $a, Stub $stub, bool $isNested) |
| 68 | { |
| 69 | $prefix = Caster::PREFIX_VIRTUAL; |
| 70 | if ($c instanceof \ReflectionNamedType || \PHP_VERSION_ID < 80000) { |
| 71 | $a += [$prefix . 'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c, $prefix . 'allowsNull' => $c->allowsNull(), $prefix . 'isBuiltin' => $c->isBuiltin()]; |
| 72 | } elseif ($c instanceof \ReflectionUnionType || $c instanceof \ReflectionIntersectionType) { |
| 73 | $a[$prefix . 'allowsNull'] = $c->allowsNull(); |
| 74 | self::addMap($a, $c, ['types' => 'getTypes']); |
| 75 | } else { |
| 76 | $a[$prefix . 'allowsNull'] = $c->allowsNull(); |
| 77 | } |
| 78 | return $a; |
| 79 | } |
| 80 | public static function castAttribute(\ReflectionAttribute $c, array $a, Stub $stub, bool $isNested) |
| 81 | { |
| 82 | $map = ['name' => 'getName', 'arguments' => 'getArguments']; |
| 83 | if (\PHP_VERSION_ID >= 80400) { |
| 84 | unset($map['name']); |
| 85 | } |
| 86 | self::addMap($a, $c, $map); |
| 87 | return $a; |
| 88 | } |
| 89 | public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, bool $isNested) |
| 90 | { |
| 91 | $prefix = Caster::PREFIX_VIRTUAL; |
| 92 | if ($c->getThis()) { |
| 93 | $a[$prefix . 'this'] = new CutStub($c->getThis()); |
| 94 | } |
| 95 | $function = $c->getFunction(); |
| 96 | $frame = ['class' => $function->class ?? null, 'type' => isset($function->class) ? $function->isStatic() ? '::' : '->' : null, 'function' => $function->name, 'file' => $c->getExecutingFile(), 'line' => $c->getExecutingLine()]; |
| 97 | if ($trace = $c->getTrace(\DEBUG_BACKTRACE_IGNORE_ARGS)) { |
| 98 | $function = new \ReflectionGenerator($c->getExecutingGenerator()); |
| 99 | array_unshift($trace, ['function' => 'yield', 'file' => $function->getExecutingFile(), 'line' => $function->getExecutingLine() - (int) (\PHP_VERSION_ID < 80100)]); |
| 100 | $trace[] = $frame; |
| 101 | $a[$prefix . 'trace'] = new TraceStub($trace, \false, 0, -1, -1); |
| 102 | } else { |
| 103 | $function = new FrameStub($frame, \false, \true); |
| 104 | $function = ExceptionCaster::castFrameStub($function, [], $function, \true); |
| 105 | $a[$prefix . 'executing'] = $function[$prefix . 'src']; |
| 106 | } |
| 107 | $a[Caster::PREFIX_VIRTUAL . 'closed'] = \false; |
| 108 | return $a; |
| 109 | } |
| 110 | public static function castClass(\ReflectionClass $c, array $a, Stub $stub, bool $isNested, int $filter = 0) |
| 111 | { |
| 112 | $prefix = Caster::PREFIX_VIRTUAL; |
| 113 | if ($n = \Reflection::getModifierNames($c->getModifiers())) { |
| 114 | $a[$prefix . 'modifiers'] = implode(' ', $n); |
| 115 | } |
| 116 | self::addMap($a, $c, ['extends' => 'getParentClass', 'implements' => 'getInterfaceNames', 'constants' => 'getReflectionConstants']); |
| 117 | foreach ($c->getProperties() as $n) { |
| 118 | $a[$prefix . 'properties'][$n->name] = $n; |
| 119 | } |
| 120 | foreach ($c->getMethods() as $n) { |
| 121 | $a[$prefix . 'methods'][$n->name] = $n; |
| 122 | } |
| 123 | self::addAttributes($a, $c, $prefix); |
| 124 | if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { |
| 125 | self::addExtra($a, $c); |
| 126 | } |
| 127 | return $a; |
| 128 | } |
| 129 | public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, bool $isNested, int $filter = 0) |
| 130 | { |
| 131 | $prefix = Caster::PREFIX_VIRTUAL; |
| 132 | self::addMap($a, $c, ['returnsReference' => 'returnsReference', 'returnType' => 'getReturnType', 'class' => \PHP_VERSION_ID >= 80111 ? 'getClosureCalledClass' : 'getClosureScopeClass', 'this' => 'getClosureThis']); |
| 133 | if (isset($a[$prefix . 'returnType'])) { |
| 134 | $v = $a[$prefix . 'returnType']; |
| 135 | $v = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v; |
| 136 | $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 : '', '']); |
| 137 | } |
| 138 | if (isset($a[$prefix . 'class'])) { |
| 139 | $a[$prefix . 'class'] = new ClassStub($a[$prefix . 'class']); |
| 140 | } |
| 141 | if (isset($a[$prefix . 'this'])) { |
| 142 | $a[$prefix . 'this'] = new CutStub($a[$prefix . 'this']); |
| 143 | } |
| 144 | foreach ($c->getParameters() as $v) { |
| 145 | $k = '$' . $v->name; |
| 146 | if ($v->isVariadic()) { |
| 147 | $k = '...' . $k; |
| 148 | } |
| 149 | if ($v->isPassedByReference()) { |
| 150 | $k = '&' . $k; |
| 151 | } |
| 152 | $a[$prefix . 'parameters'][$k] = $v; |
| 153 | } |
| 154 | if (isset($a[$prefix . 'parameters'])) { |
| 155 | $a[$prefix . 'parameters'] = new EnumStub($a[$prefix . 'parameters']); |
| 156 | } |
| 157 | self::addAttributes($a, $c, $prefix); |
| 158 | if (!($filter & Caster::EXCLUDE_VERBOSE) && ($v = $c->getStaticVariables())) { |
| 159 | foreach ($v as $k => &$v) { |
| 160 | if (\is_object($v)) { |
| 161 | $a[$prefix . 'use']['$' . $k] = new CutStub($v); |
| 162 | } else { |
| 163 | $a[$prefix . 'use']['$' . $k] =& $v; |
| 164 | } |
| 165 | } |
| 166 | unset($v); |
| 167 | $a[$prefix . 'use'] = new EnumStub($a[$prefix . 'use']); |
| 168 | } |
| 169 | if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { |
| 170 | self::addExtra($a, $c); |
| 171 | } |
| 172 | return $a; |
| 173 | } |
| 174 | public static function castClassConstant(\ReflectionClassConstant $c, array $a, Stub $stub, bool $isNested) |
| 175 | { |
| 176 | $a[Caster::PREFIX_VIRTUAL . 'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); |
| 177 | $a[Caster::PREFIX_VIRTUAL . 'value'] = $c->getValue(); |
| 178 | self::addAttributes($a, $c); |
| 179 | return $a; |
| 180 | } |
| 181 | public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, bool $isNested) |
| 182 | { |
| 183 | $a[Caster::PREFIX_VIRTUAL . 'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); |
| 184 | return $a; |
| 185 | } |
| 186 | public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, bool $isNested) |
| 187 | { |
| 188 | $prefix = Caster::PREFIX_VIRTUAL; |
| 189 | self::addMap($a, $c, ['position' => 'getPosition', 'isVariadic' => 'isVariadic', 'byReference' => 'isPassedByReference', 'allowsNull' => 'allowsNull']); |
| 190 | self::addAttributes($a, $c, $prefix); |
| 191 | if ($v = $c->getType()) { |
| 192 | $a[$prefix . 'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v; |
| 193 | } |
| 194 | if (isset($a[$prefix . 'typeHint'])) { |
| 195 | $v = $a[$prefix . 'typeHint']; |
| 196 | $a[$prefix . 'typeHint'] = new ClassStub($v, [class_exists($v, \false) || interface_exists($v, \false) || trait_exists($v, \false) ? $v : '', '']); |
| 197 | } else { |
| 198 | unset($a[$prefix . 'allowsNull']); |
| 199 | } |
| 200 | if ($c->isOptional()) { |
| 201 | try { |
| 202 | $a[$prefix . 'default'] = $v = $c->getDefaultValue(); |
| 203 | if ($c->isDefaultValueConstant() && !\is_object($v)) { |
| 204 | $a[$prefix . 'default'] = new ConstStub($c->getDefaultValueConstantName(), $v); |
| 205 | } |
| 206 | if (null === $v) { |
| 207 | unset($a[$prefix . 'allowsNull']); |
| 208 | } |
| 209 | } catch (\ReflectionException $e) { |
| 210 | } |
| 211 | } |
| 212 | return $a; |
| 213 | } |
| 214 | public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, bool $isNested) |
| 215 | { |
| 216 | $a[Caster::PREFIX_VIRTUAL . 'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); |
| 217 | self::addAttributes($a, $c); |
| 218 | self::addExtra($a, $c); |
| 219 | return $a; |
| 220 | } |
| 221 | public static function castReference(\ReflectionReference $c, array $a, Stub $stub, bool $isNested) |
| 222 | { |
| 223 | $a[Caster::PREFIX_VIRTUAL . 'id'] = $c->getId(); |
| 224 | return $a; |
| 225 | } |
| 226 | public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, bool $isNested) |
| 227 | { |
| 228 | self::addMap($a, $c, ['version' => 'getVersion', 'dependencies' => 'getDependencies', 'iniEntries' => 'getIniEntries', 'isPersistent' => 'isPersistent', 'isTemporary' => 'isTemporary', 'constants' => 'getConstants', 'functions' => 'getFunctions', 'classes' => 'getClasses']); |
| 229 | return $a; |
| 230 | } |
| 231 | public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, bool $isNested) |
| 232 | { |
| 233 | self::addMap($a, $c, ['version' => 'getVersion', 'author' => 'getAuthor', 'copyright' => 'getCopyright', 'url' => 'getURL']); |
| 234 | return $a; |
| 235 | } |
| 236 | public static function getSignature(array $a) |
| 237 | { |
| 238 | $prefix = Caster::PREFIX_VIRTUAL; |
| 239 | $signature = ''; |
| 240 | if (isset($a[$prefix . 'parameters'])) { |
| 241 | foreach ($a[$prefix . 'parameters']->value as $k => $param) { |
| 242 | $signature .= ', '; |
| 243 | if ($type = $param->getType()) { |
| 244 | if (!$type instanceof \ReflectionNamedType) { |
| 245 | $signature .= $type . ' '; |
| 246 | } else { |
| 247 | if ($param->allowsNull() && 'mixed' !== $type->getName()) { |
| 248 | $signature .= '?'; |
| 249 | } |
| 250 | $signature .= substr(strrchr('\\' . $type->getName(), '\\'), 1) . ' '; |
| 251 | } |
| 252 | } |
| 253 | $signature .= $k; |
| 254 | if (!$param->isDefaultValueAvailable()) { |
| 255 | continue; |
| 256 | } |
| 257 | $v = $param->getDefaultValue(); |
| 258 | $signature .= ' = '; |
| 259 | if ($param->isDefaultValueConstant()) { |
| 260 | $signature .= substr(strrchr('\\' . $param->getDefaultValueConstantName(), '\\'), 1); |
| 261 | } elseif (null === $v) { |
| 262 | $signature .= 'null'; |
| 263 | } elseif (\is_array($v)) { |
| 264 | $signature .= $v ? '[…' . \count($v) . ']' : '[]'; |
| 265 | } elseif (\is_string($v)) { |
| 266 | $signature .= 10 > \strlen($v) && !str_contains($v, '\\') ? "'{$v}'" : "'…" . \strlen($v) . "'"; |
| 267 | } elseif (\is_bool($v)) { |
| 268 | $signature .= $v ? 'true' : 'false'; |
| 269 | } elseif (\is_object($v)) { |
| 270 | $signature .= 'new ' . substr(strrchr('\\' . get_debug_type($v), '\\'), 1); |
| 271 | } else { |
| 272 | $signature .= $v; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | $signature = (empty($a[$prefix . 'returnsReference']) ? '' : '&') . '(' . substr($signature, 2) . ')'; |
| 277 | if (isset($a[$prefix . 'returnType'])) { |
| 278 | $signature .= ': ' . substr(strrchr('\\' . $a[$prefix . 'returnType'], '\\'), 1); |
| 279 | } |
| 280 | return $signature; |
| 281 | } |
| 282 | private static function addExtra(array &$a, \Reflector $c) |
| 283 | { |
| 284 | $x = isset($a[Caster::PREFIX_VIRTUAL . 'extra']) ? $a[Caster::PREFIX_VIRTUAL . 'extra']->value : []; |
| 285 | if (method_exists($c, 'getFileName') && ($m = $c->getFileName())) { |
| 286 | $x['file'] = new LinkStub($m, $c->getStartLine()); |
| 287 | $x['line'] = $c->getStartLine() . ' to ' . $c->getEndLine(); |
| 288 | } |
| 289 | self::addMap($x, $c, self::EXTRA_MAP, ''); |
| 290 | if ($x) { |
| 291 | $a[Caster::PREFIX_VIRTUAL . 'extra'] = new EnumStub($x); |
| 292 | } |
| 293 | } |
| 294 | private static function addMap(array &$a, object $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL) |
| 295 | { |
| 296 | foreach ($map as $k => $m) { |
| 297 | if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) { |
| 298 | continue; |
| 299 | } |
| 300 | if (method_exists($c, $m) && \false !== ($m = $c->{$m}()) && null !== $m) { |
| 301 | $a[$prefix . $k] = $m instanceof \Reflector ? $m->name : $m; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | private static function addAttributes(array &$a, \Reflector $c, string $prefix = Caster::PREFIX_VIRTUAL) : void |
| 306 | { |
| 307 | if (\PHP_VERSION_ID >= 80000) { |
| 308 | foreach ($c->getAttributes() as $n) { |
| 309 | $a[$prefix . 'attributes'][] = $n; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 |