ReflectionCaster.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. private static $extraMap = [
  20. 'docComment' => 'getDocComment',
  21. 'extension' => 'getExtensionName',
  22. 'isDisabled' => 'isDisabled',
  23. 'isDeprecated' => 'isDeprecated',
  24. 'isInternal' => 'isInternal',
  25. 'isUserDefined' => 'isUserDefined',
  26. 'isGenerator' => 'isGenerator',
  27. 'isVariadic' => 'isVariadic',
  28. ];
  29. public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested, $filter = 0)
  30. {
  31. $prefix = Caster::PREFIX_VIRTUAL;
  32. $c = new \ReflectionFunction($c);
  33. $stub->class = 'Closure'; // HHVM generates unique class names for closures
  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. if (isset($a[$prefix.'parameters'])) {
  40. foreach ($a[$prefix.'parameters']->value as &$v) {
  41. $param = $v;
  42. $v = new EnumStub([]);
  43. foreach (static::castParameter($param, [], $stub, true) as $k => $param) {
  44. if ("\0" === $k[0]) {
  45. $v->value[substr($k, 3)] = $param;
  46. }
  47. }
  48. unset($v->value['position'], $v->value['isVariadic'], $v->value['byReference'], $v);
  49. }
  50. }
  51. if (!($filter & Caster::EXCLUDE_VERBOSE) && $f = $c->getFileName()) {
  52. $a[$prefix.'file'] = new LinkStub($f, $c->getStartLine());
  53. $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
  54. }
  55. $prefix = Caster::PREFIX_DYNAMIC;
  56. unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
  57. return $a;
  58. }
  59. public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
  60. {
  61. if (!class_exists('ReflectionGenerator', false)) {
  62. return $a;
  63. }
  64. // Cannot create ReflectionGenerator based on a terminated Generator
  65. try {
  66. $reflectionGenerator = new \ReflectionGenerator($c);
  67. } catch (\Exception $e) {
  68. $a[Caster::PREFIX_VIRTUAL.'closed'] = true;
  69. return $a;
  70. }
  71. return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
  72. }
  73. public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
  74. {
  75. $prefix = Caster::PREFIX_VIRTUAL;
  76. $a += [
  77. $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : $c->__toString(),
  78. $prefix.'allowsNull' => $c->allowsNull(),
  79. $prefix.'isBuiltin' => $c->isBuiltin(),
  80. ];
  81. return $a;
  82. }
  83. public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
  84. {
  85. $prefix = Caster::PREFIX_VIRTUAL;
  86. if ($c->getThis()) {
  87. $a[$prefix.'this'] = new CutStub($c->getThis());
  88. }
  89. $function = $c->getFunction();
  90. $frame = [
  91. 'class' => isset($function->class) ? $function->class : null,
  92. 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
  93. 'function' => $function->name,
  94. 'file' => $c->getExecutingFile(),
  95. 'line' => $c->getExecutingLine(),
  96. ];
  97. if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
  98. $function = new \ReflectionGenerator($c->getExecutingGenerator());
  99. array_unshift($trace, [
  100. 'function' => 'yield',
  101. 'file' => $function->getExecutingFile(),
  102. 'line' => $function->getExecutingLine() - 1,
  103. ]);
  104. $trace[] = $frame;
  105. $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
  106. } else {
  107. $function = new FrameStub($frame, false, true);
  108. $function = ExceptionCaster::castFrameStub($function, [], $function, true);
  109. $a[$prefix.'executing'] = new EnumStub([
  110. "\0~separator= \0".$frame['class'].$frame['type'].$frame['function'].'()' => $function[$prefix.'src'],
  111. ]);
  112. }
  113. $a[Caster::PREFIX_VIRTUAL.'closed'] = false;
  114. return $a;
  115. }
  116. public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
  117. {
  118. $prefix = Caster::PREFIX_VIRTUAL;
  119. if ($n = \Reflection::getModifierNames($c->getModifiers())) {
  120. $a[$prefix.'modifiers'] = implode(' ', $n);
  121. }
  122. self::addMap($a, $c, [
  123. 'extends' => 'getParentClass',
  124. 'implements' => 'getInterfaceNames',
  125. 'constants' => 'getConstants',
  126. ]);
  127. foreach ($c->getProperties() as $n) {
  128. $a[$prefix.'properties'][$n->name] = $n;
  129. }
  130. foreach ($c->getMethods() as $n) {
  131. $a[$prefix.'methods'][$n->name] = $n;
  132. }
  133. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  134. self::addExtra($a, $c);
  135. }
  136. return $a;
  137. }
  138. public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
  139. {
  140. $prefix = Caster::PREFIX_VIRTUAL;
  141. self::addMap($a, $c, [
  142. 'returnsReference' => 'returnsReference',
  143. 'returnType' => 'getReturnType',
  144. 'class' => 'getClosureScopeClass',
  145. 'this' => 'getClosureThis',
  146. ]);
  147. if (isset($a[$prefix.'returnType'])) {
  148. $v = $a[$prefix.'returnType'];
  149. $v = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  150. $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v, [class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', '']);
  151. }
  152. if (isset($a[$prefix.'class'])) {
  153. $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
  154. }
  155. if (isset($a[$prefix.'this'])) {
  156. $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
  157. }
  158. foreach ($c->getParameters() as $v) {
  159. $k = '$'.$v->name;
  160. if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
  161. $k = '...'.$k;
  162. }
  163. if ($v->isPassedByReference()) {
  164. $k = '&'.$k;
  165. }
  166. $a[$prefix.'parameters'][$k] = $v;
  167. }
  168. if (isset($a[$prefix.'parameters'])) {
  169. $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
  170. }
  171. if ($v = $c->getStaticVariables()) {
  172. foreach ($v as $k => &$v) {
  173. if (\is_object($v)) {
  174. $a[$prefix.'use']['$'.$k] = new CutStub($v);
  175. } else {
  176. $a[$prefix.'use']['$'.$k] = &$v;
  177. }
  178. }
  179. unset($v);
  180. $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
  181. }
  182. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  183. self::addExtra($a, $c);
  184. }
  185. // Added by HHVM
  186. unset($a[Caster::PREFIX_DYNAMIC.'static']);
  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. // Added by HHVM
  198. unset($a['info']);
  199. self::addMap($a, $c, [
  200. 'position' => 'getPosition',
  201. 'isVariadic' => 'isVariadic',
  202. 'byReference' => 'isPassedByReference',
  203. 'allowsNull' => 'allowsNull',
  204. ]);
  205. if (method_exists($c, 'getType')) {
  206. if ($v = $c->getType()) {
  207. $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  208. }
  209. } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
  210. $a[$prefix.'typeHint'] = $v[1];
  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. try {
  219. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  220. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  221. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  222. }
  223. if (null === $v) {
  224. unset($a[$prefix.'allowsNull']);
  225. }
  226. } catch (\ReflectionException $e) {
  227. if (isset($a[$prefix.'typeHint']) && $c->allowsNull() && !class_exists('ReflectionNamedType', false)) {
  228. $a[$prefix.'default'] = null;
  229. unset($a[$prefix.'allowsNull']);
  230. }
  231. }
  232. return $a;
  233. }
  234. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  235. {
  236. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  237. self::addExtra($a, $c);
  238. return $a;
  239. }
  240. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  241. {
  242. self::addMap($a, $c, [
  243. 'version' => 'getVersion',
  244. 'dependencies' => 'getDependencies',
  245. 'iniEntries' => 'getIniEntries',
  246. 'isPersistent' => 'isPersistent',
  247. 'isTemporary' => 'isTemporary',
  248. 'constants' => 'getConstants',
  249. 'functions' => 'getFunctions',
  250. 'classes' => 'getClasses',
  251. ]);
  252. return $a;
  253. }
  254. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  255. {
  256. self::addMap($a, $c, [
  257. 'version' => 'getVersion',
  258. 'author' => 'getAuthor',
  259. 'copyright' => 'getCopyright',
  260. 'url' => 'getURL',
  261. ]);
  262. return $a;
  263. }
  264. private static function addExtra(&$a, \Reflector $c)
  265. {
  266. $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : [];
  267. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  268. $x['file'] = new LinkStub($m, $c->getStartLine());
  269. $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
  270. }
  271. self::addMap($x, $c, self::$extraMap, '');
  272. if ($x) {
  273. $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
  274. }
  275. }
  276. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  277. {
  278. foreach ($map as $k => $m) {
  279. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  280. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  281. }
  282. }
  283. }
  284. }