PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/var-dumper/Caster/ReflectionCaster.php

https://gitlab.com/gideonmarked/PLCPortal
PHP | 257 lines | 196 code | 44 blank | 17 comment | 28 complexity | 19653853c1651bc1b359d6c01e2d259b MD5 | raw file
  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 = array(
  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. /**
  30. * @deprecated since Symfony 2.7, to be removed in 3.0.
  31. */
  32. public static function castReflector(\Reflector $c, array $a, Stub $stub, $isNested)
  33. {
  34. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
  35. $a[Caster::PREFIX_VIRTUAL.'reflection'] = $c->__toString();
  36. return $a;
  37. }
  38. public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
  39. {
  40. $prefix = Caster::PREFIX_VIRTUAL;
  41. $c = new \ReflectionFunction($c);
  42. $stub->class = 'Closure'; // HHVM generates unique class names for closures
  43. $a = static::castFunctionAbstract($c, $a, $stub, $isNested);
  44. if (isset($a[$prefix.'parameters'])) {
  45. foreach ($a[$prefix.'parameters'] as &$v) {
  46. $param = $v;
  47. $v = array();
  48. foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
  49. if ("\0" === $k[0]) {
  50. $v[substr($k, 3)] = $param;
  51. }
  52. }
  53. unset($v['position'], $v['isVariadic'], $v['byReference'], $v);
  54. }
  55. }
  56. if ($f = $c->getFileName()) {
  57. $a[$prefix.'file'] = $f;
  58. $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
  59. }
  60. $prefix = Caster::PREFIX_DYNAMIC;
  61. unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
  62. return $a;
  63. }
  64. public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
  65. {
  66. $prefix = Caster::PREFIX_VIRTUAL;
  67. if ($n = \Reflection::getModifierNames($c->getModifiers())) {
  68. $a[$prefix.'modifiers'] = implode(' ', $n);
  69. }
  70. self::addMap($a, $c, array(
  71. 'extends' => 'getParentClass',
  72. 'implements' => 'getInterfaceNames',
  73. 'constants' => 'getConstants',
  74. ));
  75. foreach ($c->getProperties() as $n) {
  76. $a[$prefix.'properties'][$n->name] = $n;
  77. }
  78. foreach ($c->getMethods() as $n) {
  79. $a[$prefix.'methods'][$n->name] = $n;
  80. }
  81. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  82. self::addExtra($a, $c);
  83. }
  84. return $a;
  85. }
  86. public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
  87. {
  88. $prefix = Caster::PREFIX_VIRTUAL;
  89. self::addMap($a, $c, array(
  90. 'returnsReference' => 'returnsReference',
  91. 'returnType' => 'getReturnType',
  92. 'class' => 'getClosureScopeClass',
  93. 'this' => 'getClosureThis',
  94. ));
  95. if (isset($a[$prefix.'returnType'])) {
  96. $v = $a[$prefix.'returnType'];
  97. $v = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  98. $a[$prefix.'returnType'] = $a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v;
  99. }
  100. if (isset($a[$prefix.'this'])) {
  101. $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
  102. }
  103. foreach ($c->getParameters() as $v) {
  104. $k = '$'.$v->name;
  105. if ($v->isPassedByReference()) {
  106. $k = '&'.$k;
  107. }
  108. if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
  109. $k = '...'.$k;
  110. }
  111. $a[$prefix.'parameters'][$k] = $v;
  112. }
  113. if ($v = $c->getStaticVariables()) {
  114. foreach ($v as $k => &$v) {
  115. $a[$prefix.'use']['$'.$k] = &$v;
  116. }
  117. unset($v);
  118. }
  119. if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
  120. self::addExtra($a, $c);
  121. }
  122. return $a;
  123. }
  124. public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
  125. {
  126. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  127. return $a;
  128. }
  129. public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
  130. {
  131. $prefix = Caster::PREFIX_VIRTUAL;
  132. // Added by HHVM
  133. unset($a['info']);
  134. self::addMap($a, $c, array(
  135. 'position' => 'getPosition',
  136. 'isVariadic' => 'isVariadic',
  137. 'byReference' => 'isPassedByReference',
  138. 'allowsNull' => 'allowsNull',
  139. ));
  140. if (method_exists($c, 'getType')) {
  141. if ($v = $c->getType()) {
  142. $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
  143. }
  144. } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
  145. $a[$prefix.'typeHint'] = $v[1];
  146. }
  147. if (!isset($a[$prefix.'typeHint'])) {
  148. unset($a[$prefix.'allowsNull']);
  149. }
  150. try {
  151. $a[$prefix.'default'] = $v = $c->getDefaultValue();
  152. if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
  153. $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
  154. }
  155. if (null === $v) {
  156. unset($a[$prefix.'allowsNull']);
  157. }
  158. } catch (\ReflectionException $e) {
  159. if (isset($a[$prefix.'typeHint']) && $c->allowsNull() && !class_exists('ReflectionNamedType', false)) {
  160. $a[$prefix.'default'] = null;
  161. unset($a[$prefix.'allowsNull']);
  162. }
  163. }
  164. return $a;
  165. }
  166. public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
  167. {
  168. $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
  169. self::addExtra($a, $c);
  170. return $a;
  171. }
  172. public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
  173. {
  174. self::addMap($a, $c, array(
  175. 'version' => 'getVersion',
  176. 'dependencies' => 'getDependencies',
  177. 'iniEntries' => 'getIniEntries',
  178. 'isPersistent' => 'isPersistent',
  179. 'isTemporary' => 'isTemporary',
  180. 'constants' => 'getConstants',
  181. 'functions' => 'getFunctions',
  182. 'classes' => 'getClasses',
  183. ));
  184. return $a;
  185. }
  186. public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
  187. {
  188. self::addMap($a, $c, array(
  189. 'version' => 'getVersion',
  190. 'author' => 'getAuthor',
  191. 'copyright' => 'getCopyright',
  192. 'url' => 'getURL',
  193. ));
  194. return $a;
  195. }
  196. private static function addExtra(&$a, \Reflector $c)
  197. {
  198. $a = &$a[Caster::PREFIX_VIRTUAL.'extra'];
  199. if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
  200. $a['file'] = $m;
  201. $a['line'] = $c->getStartLine().' to '.$c->getEndLine();
  202. }
  203. self::addMap($a, $c, self::$extraMap, '');
  204. }
  205. private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
  206. {
  207. foreach ($map as $k => $m) {
  208. if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
  209. $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;
  210. }
  211. }
  212. }
  213. }