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',
35 * @deprecated since Symfony 2.7, to be removed in 3.0.
37 public static function castReflector(\Reflector $c, array $a, Stub $stub, $isNested)
39 @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
40 $a[Caster::PREFIX_VIRTUAL.'reflection'] = $c->__toString();
45 public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
47 $prefix = Caster::PREFIX_VIRTUAL;
48 $c = new \ReflectionFunction($c);
50 $stub->class = 'Closure'; // HHVM generates unique class names for closures
51 $a = static::castFunctionAbstract($c, $a, $stub, $isNested);
53 if (isset($a[$prefix.'parameters'])) {
54 foreach ($a[$prefix.'parameters']->value as &$v) {
56 $v = new EnumStub(array());
57 foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
59 $v->value[substr($k, 3)] = $param;
62 unset($v->value['position'], $v->value['isVariadic'], $v->value['byReference'], $v);
66 if ($f = $c->getFileName()) {
67 $a[$prefix.'file'] = $f;
68 $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
71 $prefix = Caster::PREFIX_DYNAMIC;
72 unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
77 public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
79 if (!class_exists('ReflectionGenerator', false)) {
83 // Cannot create ReflectionGenerator based on a terminated Generator
85 $reflectionGenerator = new \ReflectionGenerator($c);
86 } catch (\Exception $e) {
87 $a[Caster::PREFIX_VIRTUAL.'closed'] = true;
92 return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
95 public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
97 $prefix = Caster::PREFIX_VIRTUAL;
100 $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : $c->__toString(),
101 $prefix.'allowsNull' => $c->allowsNull(),
102 $prefix.'isBuiltin' => $c->isBuiltin(),
108 public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
110 $prefix = Caster::PREFIX_VIRTUAL;
113 $a[$prefix.'this'] = new CutStub($c->getThis());
115 $function = $c->getFunction();
117 'class' => isset($function->class) ? $function->class : null,
118 'type' => isset($function->class) ? ($function->isStatic() ? '::' : '->') : null,
119 'function' => $function->name,
120 'file' => $c->getExecutingFile(),
121 'line' => $c->getExecutingLine(),
123 if ($trace = $c->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS)) {
124 $function = new \ReflectionGenerator($c->getExecutingGenerator());
125 array_unshift($trace, array(
126 'function' => 'yield',
127 'file' => $function->getExecutingFile(),
128 'line' => $function->getExecutingLine() - 1,
131 $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
133 $function = new FrameStub($frame, false, true);
134 $function = ExceptionCaster::castFrameStub($function, array(), $function, true);
135 $a[$prefix.'executing'] = new EnumStub(array(
136 $frame['class'].$frame['type'].$frame['function'].'()' => $function[$prefix.'src'],
140 $a[Caster::PREFIX_VIRTUAL.'closed'] = false;
145 public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
147 $prefix = Caster::PREFIX_VIRTUAL;
149 if ($n = \Reflection::getModifierNames($c->getModifiers())) {
150 $a[$prefix.'modifiers'] = implode(' ', $n);
153 self::addMap($a, $c, array(
154 'extends' => 'getParentClass',
155 'implements' => 'getInterfaceNames',
156 'constants' => 'getConstants',
159 foreach ($c->getProperties() as $n) {
160 $a[$prefix.'properties'][$n->name] = $n;
163 foreach ($c->getMethods() as $n) {
164 $a[$prefix.'methods'][$n->name] = $n;
167 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
168 self::addExtra($a, $c);
174 public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
176 $prefix = Caster::PREFIX_VIRTUAL;
178 self::addMap($a, $c, array(
179 'returnsReference' => 'returnsReference',
180 'returnType' => 'getReturnType',
181 'class' => 'getClosureScopeClass',
182 'this' => 'getClosureThis',
185 if (isset($a[$prefix.'returnType'])) {
186 $v = $a[$prefix.'returnType'];
187 $v = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
188 $a[$prefix.'returnType'] = $a[$prefix.'returnType']->allowsNull() ? '?'.$v : $v;
190 if (isset($a[$prefix.'this'])) {
191 $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
194 foreach ($c->getParameters() as $v) {
196 if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
199 if ($v->isPassedByReference()) {
202 $a[$prefix.'parameters'][$k] = $v;
204 if (isset($a[$prefix.'parameters'])) {
205 $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
208 if ($v = $c->getStaticVariables()) {
209 foreach ($v as $k => &$v) {
210 $a[$prefix.'use']['$'.$k] = &$v;
213 $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
216 if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
217 self::addExtra($a, $c);
221 unset($a[Caster::PREFIX_DYNAMIC.'static']);
226 public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
228 $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
233 public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
235 $prefix = Caster::PREFIX_VIRTUAL;
240 self::addMap($a, $c, array(
241 'position' => 'getPosition',
242 'isVariadic' => 'isVariadic',
243 'byReference' => 'isPassedByReference',
244 'allowsNull' => 'allowsNull',
247 if (method_exists($c, 'getType')) {
248 if ($v = $c->getType()) {
249 $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
251 } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
252 $a[$prefix.'typeHint'] = $v[1];
254 if (!isset($a[$prefix.'typeHint'])) {
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()) {
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;