ReflectionCaster.php 14 KB

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