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 12namespace Symfony\Component\VarDumper\Caster; 13 14use 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 since Symfony 4.4 22 */ 23class ReflectionCaster 24{ 25 const UNSET_CLOSURE_FILE_INFO = ['Closure' => __CLASS__.'::unsetClosureFileInfo']; 26 27 private static $extraMap = [ 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, $isNested, $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 (false === strpos($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, $isNested) 82 { 83 // Cannot create ReflectionGenerator based on a terminated Generator 84 try { 85 $reflectionGenerator = new \ReflectionGenerator($c); 86 } catch (\Exception $e) { 87 $a[Caster::PREFIX_VIRTUAL.'closed'] = true; 88 89 return $a; 90 } 91 92 return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested); 93 } 94 95 public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested) 96 { 97 $prefix = Caster::PREFIX_VIRTUAL; 98 99 $a += [ 100 $prefix.'name' => $c->getName(), 101 $prefix.'allowsNull' => $c->allowsNull(), 102 $prefix.'isBuiltin' => $c->isBuiltin(), 103 ]; 104 105 return $a; 106 } 107 108 public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested) 109 { 110 $prefix = Caster::PREFIX_VIRTUAL; 111 112 if ($c->getThis()) { 113 $a[$prefix.'this'] = new CutStub($c->getThis()); 114 } 115 $function = $c->getFunction(); 116 $frame = [ 117 'class' => isset($function->class) ? $function->class : null, 118 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null, 119 'function' => $function->name, 120 'file' => $c->getExecutingFile(), 121 'line' => $c->getExecutingLine(), 122 ]; 123 if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) { 124 $function = new \ReflectionGenerator($c->getExecutingGenerator()); 125 array_unshift($trace, [ 126 'function' => 'yield', 127 'file' => $function->getExecutingFile(), 128 'line' => $function->getExecutingLine() - 1, 129 ]); 130 $trace[] = $frame; 131 $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1); 132 } else { 133 $function = new FrameStub($frame, false, true); 134 $function = ExceptionCaster::castFrameStub($function, [], $function, true); 135 $a[$prefix.'executing'] = $function[$prefix.'src']; 136 } 137 138 $a[Caster::PREFIX_VIRTUAL.'closed'] = false; 139 140 return $a; 141 } 142 143 public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0) 144 { 145 $prefix = Caster::PREFIX_VIRTUAL; 146 147 if ($n = \Reflection::getModifierNames($c->getModifiers())) { 148 $a[$prefix.'modifiers'] = implode(' ', $n); 149 } 150 151 self::addMap($a, $c, [ 152 'extends' => 'getParentClass', 153 'implements' => 'getInterfaceNames', 154 'constants' => 'getConstants', 155 ]); 156 157 foreach ($c->getProperties() as $n) { 158 $a[$prefix.'properties'][$n->name] = $n; 159 } 160 161 foreach ($c->getMethods() as $n) { 162 $a[$prefix.'methods'][$n->name] = $n; 163 } 164 165 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { 166 self::addExtra($a, $c); 167 } 168 169 return $a; 170 } 171 172 public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0) 173 { 174 $prefix = Caster::PREFIX_VIRTUAL; 175 176 self::addMap($a, $c, [ 177 'returnsReference' => 'returnsReference', 178 'returnType' => 'getReturnType', 179 'class' => 'getClosureScopeClass', 180 'this' => 'getClosureThis', 181 ]); 182 183 if (isset($a[$prefix.'returnType'])) { 184 $v = $a[$prefix.'returnType']; 185 $v = $v->getName(); 186 $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']); 187 } 188 if (isset($a[$prefix.'class'])) { 189 $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']); 190 } 191 if (isset($a[$prefix.'this'])) { 192 $a[$prefix.'this'] = new CutStub($a[$prefix.'this']); 193 } 194 195 foreach ($c->getParameters() as $v) { 196 $k = '$'.$v->name; 197 if ($v->isVariadic()) { 198 $k = '...'.$k; 199 } 200 if ($v->isPassedByReference()) { 201 $k = '&'.$k; 202 } 203 $a[$prefix.'parameters'][$k] = $v; 204 } 205 if (isset($a[$prefix.'parameters'])) { 206 $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']); 207 } 208 209 if (!($filter & Caster::EXCLUDE_VERBOSE) && $v = $c->getStaticVariables()) { 210 foreach ($v as $k => &$v) { 211 if (\is_object($v)) { 212 $a[$prefix.'use']['$'.$k] = new CutStub($v); 213 } else { 214 $a[$prefix.'use']['$'.$k] = &$v; 215 } 216 } 217 unset($v); 218 $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']); 219 } 220 221 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) { 222 self::addExtra($a, $c); 223 } 224 225 return $a; 226 } 227 228 public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested) 229 { 230 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); 231 232 return $a; 233 } 234 235 public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested) 236 { 237 $prefix = Caster::PREFIX_VIRTUAL; 238 239 self::addMap($a, $c, [ 240 'position' => 'getPosition', 241 'isVariadic' => 'isVariadic', 242 'byReference' => 'isPassedByReference', 243 'allowsNull' => 'allowsNull', 244 ]); 245 246 if ($v = $c->getType()) { 247 $a[$prefix.'typeHint'] = $v->getName(); 248 } 249 250 if (isset($a[$prefix.'typeHint'])) { 251 $v = $a[$prefix.'typeHint']; 252 $a[$prefix.'typeHint'] = new ClassStub($v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']); 253 } else { 254 unset($a[$prefix.'allowsNull']); 255 } 256 257 try { 258 $a[$prefix.'default'] = $v = $c->getDefaultValue(); 259 if ($c->isDefaultValueConstant()) { 260 $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v); 261 } 262 if (null === $v) { 263 unset($a[$prefix.'allowsNull']); 264 } 265 } catch (\ReflectionException $e) { 266 } 267 268 return $a; 269 } 270 271 public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested) 272 { 273 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers())); 274 self::addExtra($a, $c); 275 276 return $a; 277 } 278 279 public static function castReference(\ReflectionReference $c, array $a, Stub $stub, $isNested) 280 { 281 $a[Caster::PREFIX_VIRTUAL.'id'] = $c->getId(); 282 283 return $a; 284 } 285 286 public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested) 287 { 288 self::addMap($a, $c, [ 289 'version' => 'getVersion', 290 'dependencies' => 'getDependencies', 291 'iniEntries' => 'getIniEntries', 292 'isPersistent' => 'isPersistent', 293 'isTemporary' => 'isTemporary', 294 'constants' => 'getConstants', 295 'functions' => 'getFunctions', 296 'classes' => 'getClasses', 297 ]); 298 299 return $a; 300 } 301 302 public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested) 303 { 304 self::addMap($a, $c, [ 305 'version' => 'getVersion', 306 'author' => 'getAuthor', 307 'copyright' => 'getCopyright', 308 'url' => 'getURL', 309 ]); 310 311 return $a; 312 } 313 314 public static function getSignature(array $a) 315 { 316 $prefix = Caster::PREFIX_VIRTUAL; 317 $signature = ''; 318 319 if (isset($a[$prefix.'parameters'])) { 320 foreach ($a[$prefix.'parameters']->value as $k => $param) { 321 $signature .= ', '; 322 if ($type = $param->getType()) { 323 if (!$param->isOptional() && $param->allowsNull()) { 324 $signature .= '?'; 325 } 326 $signature .= substr(strrchr('\\'.$type->getName(), '\\'), 1).' '; 327 } 328 $signature .= $k; 329 330 if (!$param->isDefaultValueAvailable()) { 331 continue; 332 } 333 $v = $param->getDefaultValue(); 334 $signature .= ' = '; 335 336 if ($param->isDefaultValueConstant()) { 337 $signature .= substr(strrchr('\\'.$param->getDefaultValueConstantName(), '\\'), 1); 338 } elseif (null === $v) { 339 $signature .= 'null'; 340 } elseif (\is_array($v)) { 341 $signature .= $v ? '[…'.\count($v).']' : '[]'; 342 } elseif (\is_string($v)) { 343 $signature .= 10 > \strlen($v) && false === strpos($v, '\\') ? "'{$v}'" : "'…".\strlen($v)."'"; 344 } elseif (\is_bool($v)) { 345 $signature .= $v ? 'true' : 'false'; 346 } else { 347 $signature .= $v; 348 } 349 } 350 } 351 $signature = (empty($a[$prefix.'returnsReference']) ? '' : '&').'('.substr($signature, 2).')'; 352 353 if (isset($a[$prefix.'returnType'])) { 354 $signature .= ': '.substr(strrchr('\\'.$a[$prefix.'returnType'], '\\'), 1); 355 } 356 357 return $signature; 358 } 359 360 private static function addExtra(array &$a, \Reflector $c) 361 { 362 $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : []; 363 364 if (method_exists($c, 'getFileName') && $m = $c->getFileName()) { 365 $x['file'] = new LinkStub($m, $c->getStartLine()); 366 $x['line'] = $c->getStartLine().' to '.$c->getEndLine(); 367 } 368 369 self::addMap($x, $c, self::$extraMap, ''); 370 371 if ($x) { 372 $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x); 373 } 374 } 375 376 private static function addMap(array &$a, \Reflector $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL) 377 { 378 foreach ($map as $k => $m) { 379 if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) { 380 $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m; 381 } 382 } 383 } 384} 385