ReflectionCaster.php 13 KB

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