ReflectionCaster.php 12 KB

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