4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Caster;
14 use Symfony\Component\VarDumper\Cloner\Stub;
17 * Casts Reflector related classes to array representation.
19 * @author Nicolas Grekas <p@tchwork.com>
21 class ReflectionCaster
23 private static $extraMap = array(
24 'docComment' => 'getDocComment',
25 'extension' => 'getExtensionName',
26 'isDisabled' => 'isDisabled',
27 'isDeprecated' => 'isDeprecated',
28 'isInternal' => 'isInternal',
29 'isUserDefined' => 'isUserDefined',
30 'isGenerator' => 'isGenerator',
31 'isVariadic' => 'isVariadic',
34 public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested, $filter = 0)
36 $prefix = Caster::PREFIX_VIRTUAL;
37 $c = new \ReflectionFunction($c);
39 $stub->class = 'Closure'; // HHVM generates unique class names for closures
40 $a = static::castFunctionAbstract($c, $a, $stub, $isNested, $filter);
42 if (isset($a[$prefix.'parameters'])) {
43 foreach ($a[$prefix.'parameters']->value as &$v) {
45 $v = new EnumStub(array());
46 foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
48 $v->value[substr($k, 3)] = $param;
51 unset($v->value['position'], $v->value['isVariadic'], $v->value['byReference'], $v);
55 if (!($filter & Caster::EXCLUDE_VERBOSE) && $f = $c->getFileName()) {
56 $a[$prefix.'file'] = new LinkStub($f, $c->getStartLine());
57 $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
60 $prefix = Caster::PREFIX_DYNAMIC;
61 unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
66 public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
68 if (!class_exists('ReflectionGenerator', false)) {
72 // Cannot create ReflectionGenerator based on a terminated Generator
74 $reflectionGenerator = new \ReflectionGenerator($c);
75 } catch (\Exception $e) {
76 $a[Caster::PREFIX_VIRTUAL.'closed'] = true;
81 return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
84 public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
86 $prefix = Caster::PREFIX_VIRTUAL;
89 $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : $c->__toString(),
90 $prefix.'allowsNull' => $c->allowsNull(),
91 $prefix.'isBuiltin' => $c->isBuiltin(),
97 public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
99 $prefix = Caster::PREFIX_VIRTUAL;
102 $a[$prefix.'this'] = new CutStub($c->getThis());
104 $function = $c->getFunction();
106 'class' => isset($function->class) ? $function->class : null,
107 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
108 'function' => $function->name,
109 'file' => $c->getExecutingFile(),
110 'line' => $c->getExecutingLine(),
112 if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
113 $function = new \ReflectionGenerator($c->getExecutingGenerator());
114 array_unshift($trace, array(
115 'function' => 'yield',
116 'file' => $function->getExecutingFile(),
117 'line' => $function->getExecutingLine() - 1,
120 $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
122 $function = new FrameStub($frame, false, true);
123 $function = ExceptionCaster::castFrameStub($function, array(), $function, true);
124 $a[$prefix.'executing'] = new EnumStub(array(
125 "\0~separator= \0".$frame['class'].$frame['type'].$frame['function'].'()' => $function[$prefix.'src'],
129 $a[Caster::PREFIX_VIRTUAL.'closed'] = false;
134 public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
136 $prefix = Caster::PREFIX_VIRTUAL;
138 if ($n = \Reflection::getModifierNames($c->getModifiers())) {
139 $a[$prefix.'modifiers'] = implode(' ', $n);
142 self::addMap($a, $c, array(
143 'extends' => 'getParentClass',
144 'implements' => 'getInterfaceNames',
145 'constants' => 'getConstants',
148 foreach ($c->getProperties() as $n) {
149 $a[$prefix.'properties'][$n->name] = $n;
152 foreach ($c->getMethods() as $n) {
153 $a[$prefix.'methods'][$n->name] = $n;
156 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
157 self::addExtra($a, $c);
163 public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
165 $prefix = Caster::PREFIX_VIRTUAL;
167 self::addMap($a, $c, array(
168 'returnsReference' => 'returnsReference',
169 'returnType' => 'getReturnType',
170 'class' => 'getClosureScopeClass',
171 'this' => 'getClosureThis',
174 if (isset($a[$prefix.'returnType'])) {
175 $v = $a[$prefix.'returnType'];
176 $v = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
177 $a[$prefix.'returnType'] = new ClassStub($a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v, array(class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', ''));
179 if (isset($a[$prefix.'class'])) {
180 $a[$prefix.'class'] = new ClassStub($a[$prefix.'class']);
182 if (isset($a[$prefix.'this'])) {
183 $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
186 foreach ($c->getParameters() as $v) {
188 if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
191 if ($v->isPassedByReference()) {
194 $a[$prefix.'parameters'][$k] = $v;
196 if (isset($a[$prefix.'parameters'])) {
197 $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
200 if ($v = $c->getStaticVariables()) {
201 foreach ($v as $k => &$v) {
202 if (\is_object($v)) {
203 $a[$prefix.'use']['$'.$k] = new CutStub($v);
205 $a[$prefix.'use']['$'.$k] = &$v;
209 $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
212 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
213 self::addExtra($a, $c);
217 unset($a[Caster::PREFIX_DYNAMIC.'static']);
222 public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
224 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
229 public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
231 $prefix = Caster::PREFIX_VIRTUAL;
236 self::addMap($a, $c, array(
237 'position' => 'getPosition',
238 'isVariadic' => 'isVariadic',
239 'byReference' => 'isPassedByReference',
240 'allowsNull' => 'allowsNull',
243 if (method_exists($c, 'getType')) {
244 if ($v = $c->getType()) {
245 $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
247 } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
248 $a[$prefix.'typeHint'] = $v[1];
251 if (isset($a[$prefix.'typeHint'])) {
252 $v = $a[$prefix.'typeHint'];
253 $a[$prefix.'typeHint'] = new ClassStub($v, array(class_exists($v, false) || interface_exists($v, false) || trait_exists($v, false) ? $v : '', ''));
255 unset($a[$prefix.'allowsNull']);
259 $a[$prefix.'default'] = $v = $c->getDefaultValue();
260 if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
261 $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
264 unset($a[$prefix.'allowsNull']);
266 } catch (\ReflectionException $e) {
267 if (isset($a[$prefix.'typeHint']) && $c->allowsNull() && !class_exists('ReflectionNamedType', false)) {
268 $a[$prefix.'default'] = null;
269 unset($a[$prefix.'allowsNull']);
276 public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
278 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
279 self::addExtra($a, $c);
284 public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
286 self::addMap($a, $c, array(
287 'version' => 'getVersion',
288 'dependencies' => 'getDependencies',
289 'iniEntries' => 'getIniEntries',
290 'isPersistent' => 'isPersistent',
291 'isTemporary' => 'isTemporary',
292 'constants' => 'getConstants',
293 'functions' => 'getFunctions',
294 'classes' => 'getClasses',
300 public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
302 self::addMap($a, $c, array(
303 'version' => 'getVersion',
304 'author' => 'getAuthor',
305 'copyright' => 'getCopyright',
312 private static function addExtra(&$a, \Reflector $c)
314 $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();
316 if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
317 $x['file'] = new LinkStub($m, $c->getStartLine());
318 $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
321 self::addMap($x, $c, self::$extraMap, '');
324 $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
328 private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
330 foreach ($map as $k => $m) {
331 if (method_exists($c, $m) && false !== ($m = $c->$m()) && null !== $m) {
332 $a[$prefix.$k] = $m instanceof \Reflector ? $m->name : $m;