Keine Beschreibung

ReflectionCaster.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 since Symfony 4.4
  18. */
  19. class ReflectionCaster
  20. {
  21. public const UNSET_CLOSURE_FILE_INFO = ['Closure' => __CLASS__.'::unsetClosureFileInfo'];
  22. private const EXTRA_MAP = [
  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, $isNested, $filter = 0)
  33. {
  34. $prefix = Caster::PREFIX_VIRTUAL;
  35. $c = new \ReflectionFunction($c);
  36. $a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter);
  37. if (!str_contains($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, $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, $isNested)
  75. {
  76. $prefix = Caster::PREFIX_VIRTUAL;
  77. if ($c instanceof \ReflectionNamedType || \PHP_VERSION_ID < 80000) {
  78. $a += [
  79. $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c,
  80. $prefix.'allowsNull' => $c->allowsNull(),
  81. $prefix.'isBuiltin' => $c->isBuiltin(),
  82. ];
  83. } elseif ($c instanceof \ReflectionUnionType || $c instanceof \ReflectionIntersectionType) {
  84. $a[$prefix.'allowsNull'] = $c->allowsNull();
  85. self::addMap($a, $c, [
  86. 'types' => 'getTypes',
  87. ]);
  88. } else {
  89. $a[$prefix.'allowsNull'] = $c->allowsNull();
  90. }
  91. return $a;
  92. }
  93. public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
  94. {
  95. $prefix = Caster::PREFIX_VIRTUAL;
  96. if ($c->getThis()) {
  97. $a[$prefix.'this'] = new CutStub($c->getThis());
  98. }
  99. $function = $c->getFunction();
  100. $frame = [
  101. 'class' => $function->class ?? null,
  102. 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
  103. 'function' => $function->name,
  104. 'file' => $c->getExecutingFile(),
  105. 'line' => $c->getExecutingLine(),
  106. ];
  107. if ($trace = $c->getTrace(\DEBUG_BACKTRACE_IGNORE_ARGS)) {
  108. $function = new \ReflectionGenerator($c->getExecutingGenerator());
  109. array_unshift($trace, [
  110. 'function' => 'yield',
  111. 'file' => $function->getExecutingFile(),
  112. 'line' => $function->getExecutingLine() - (int) (\PHP_VERSION_ID < 80100),
  113. ]);
  114. $trace[] = $frame;
  115. $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
  116. } else {
  117. $function = new FrameStub($frame, false, true);
  118. $function = ExceptionCaster::castFrameStub($function, [], $function, true);
  119. $a[$prefix.'executing'] = $function[$prefix.'src'];
  120. }
  121. $a[Caster::PREFIX_VIRTUAL.'closed'] = false;
  122. return $a;
  123. }
  124. public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
  125. {
  126. $prefix = Caster::PREFIX_VIRTUAL;
  127. if ($n = \Reflection::getModifierNames($c->getModifiers())) {
  128. $a[$prefix.'modifiers'] = implode(' ', $n);
  129. }
  130. self::addMap($a, $c, [
  131. 'extends' => 'getParentClass',
  132. 'implements' => 'getInterfaceNames',
  133. 'constants' => 'getConstants',
  134. ]);
  135. foreach ($c->getProperties() as $n) {
  136. $a[$prefix.'properties'][$n->name] = $n;
  137. }
  138. foreach ($c->getMethods() as $n) {
  139. $a[$prefix.'methods'][$n->name] = $n;
  140. }
  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, $isNested, $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'] instanceof \ReflectionNamedType && $a[$prefix.'returnType']->allowsNull() && 'mixed' !== $v ? '?'.$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. if (!($filter & Caster::EXCLUDE_VERBOSE) && $v = $c->getStaticVariables()) {
  180. foreach ($v as $k => &$v) {
  181. if (\is_object($v)) {
  182. $a[$prefix.'use']['$'.$k] = new CutStub($v);
  183. } else {
  184. $a[$prefix.'use']['$'.$k] = &$v;
  185. }
  186. }
  187. unset($v);
  188. $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
  189. }
  190. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  191. self::addExtra($a, $c);
  192. }
  193. return $a;
  194. }
  195. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  196. {
  197. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  198. return $a;
  199. }
  200. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  201. {
  202. $prefix = Caster::PREFIX_VIRTUAL;
  203. self::addMap($a, $c, [
  204. 'position' => 'getPosition',
  205. 'isVariadic' => 'isVariadic',
  206. 'byReference' => 'isPassedByReference',
  207. 'allowsNull' => 'allowsNull',
  208. ]);
  209. if ($v = $c->getType()) {
  210. $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : (string) $v;
  211. }
  212. if (isset($a[$prefix.'typeHint'])) {
  213. $v = $a[$prefix.'typeHint'];
  214. $a[$prefix.'typeHint'] = new ClassStub($v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
  215. } else {
  216. unset($a[$prefix.'allowsNull']);
  217. }
  218. if ($c->isOptional()) {
  219. try {
  220. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  221. if ($c->isDefaultValueConstant()) {
  222. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  223. }
  224. if (null === $v) {
  225. unset($a[$prefix.'allowsNull']);
  226. }
  227. } catch (\ReflectionException $e) {
  228. }
  229. }
  230. return $a;
  231. }
  232. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  233. {
  234. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  235. self::addExtra($a, $c);
  236. return $a;
  237. }
  238. public static function castReference(\ReflectionReference $c, array $a, Stub $stub, $isNested)
  239. {
  240. $a[Caster::PREFIX_VIRTUAL.'id'] = $c->getId();
  241. return $a;
  242. }
  243. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  244. {
  245. self::addMap($a, $c, [
  246. 'version' => 'getVersion',
  247. 'dependencies' => 'getDependencies',
  248. 'iniEntries' => 'getIniEntries',
  249. 'isPersistent' => 'isPersistent',
  250. 'isTemporary' => 'isTemporary',
  251. 'constants' => 'getConstants',
  252. 'functions' => 'getFunctions',
  253. 'classes' => 'getClasses',
  254. ]);
  255. return $a;
  256. }
  257. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  258. {
  259. self::addMap($a, $c, [
  260. 'version' => 'getVersion',
  261. 'author' => 'getAuthor',
  262. 'copyright' => 'getCopyright',
  263. 'url' => 'getURL',
  264. ]);
  265. return $a;
  266. }
  267. public static function getSignature(array $a)
  268. {
  269. $prefix = Caster::PREFIX_VIRTUAL;
  270. $signature = '';
  271. if (isset($a[$prefix.'parameters'])) {
  272. foreach ($a[$prefix.'parameters']->value as $k => $param) {
  273. $signature .= ', ';
  274. if ($type = $param->getType()) {
  275. if (!$type instanceof \ReflectionNamedType) {
  276. $signature .= $type.' ';
  277. } else {
  278. if (!$param->isOptional() && $param->allowsNull() && 'mixed' !== $type->getName()) {
  279. $signature .= '?';
  280. }
  281. $signature .= substr(strrchr('\\'.$type->getName(), '\\'), 1).' ';
  282. }
  283. }
  284. $signature .= $k;
  285. if (!$param->isDefaultValueAvailable()) {
  286. continue;
  287. }
  288. $v = $param->getDefaultValue();
  289. $signature .= ' = ';
  290. if ($param->isDefaultValueConstant()) {
  291. $signature .= substr(strrchr('\\'.$param->getDefaultValueConstantName(), '\\'), 1);
  292. } elseif (null === $v) {
  293. $signature .= 'null';
  294. } elseif (\is_array($v)) {
  295. $signature .= $v ? '[…'.\count($v).']' : '[]';
  296. } elseif (\is_string($v)) {
  297. $signature .= 10 > \strlen($v) && !str_contains($v, '\\') ? "'{$v}'" : "'…".\strlen($v)."'";
  298. } elseif (\is_bool($v)) {
  299. $signature .= $v ? 'true' : 'false';
  300. } elseif (\is_object($v)) {
  301. $signature .= 'new '.substr(strrchr('\\'.get_debug_type($v), '\\'), 1);
  302. } else {
  303. $signature .= $v;
  304. }
  305. }
  306. }
  307. $signature = (empty($a[$prefix.'returnsReference']) ? '' : '&').'('.substr($signature, 2).')';
  308. if (isset($a[$prefix.'returnType'])) {
  309. $signature .= ': '.substr(strrchr('\\'.$a[$prefix.'returnType'], '\\'), 1);
  310. }
  311. return $signature;
  312. }
  313. private static function addExtra(array &$a, \Reflector $c)
  314. {
  315. $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : [];
  316. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  317. $x['file'] = new LinkStub($m, $c->getStartLine());
  318. $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
  319. }
  320. self::addMap($x, $c, self::EXTRA_MAP, '');
  321. if ($x) {
  322. $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
  323. }
  324. }
  325. private static function addMap(array &$a, $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL)
  326. {
  327. foreach ($map as $k => $m) {
  328. if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) {
  329. continue;
  330. }
  331. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  332. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  333. }
  334. }
  335. }
  336. }