ReflectionCaster.php 12 KB

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