db backup prior to drupal security update
[yaffs-website] / vendor / symfony / var-dumper / Caster / ReflectionCaster.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\VarDumper\Caster;
13
14 use Symfony\Component\VarDumper\Cloner\Stub;
15
16 /**
17  * Casts Reflector related classes to array representation.
18  *
19  * @author Nicolas Grekas <p@tchwork.com>
20  */
21 class ReflectionCaster
22 {
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',
32     );
33
34     /**
35      * @deprecated since Symfony 2.7, to be removed in 3.0.
36      */
37     public static function castReflector(\Reflector $c, array $a, Stub $stub, $isNested)
38     {
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();
41
42         return $a;
43     }
44
45     public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
46     {
47         $prefix = Caster::PREFIX_VIRTUAL;
48         $c = new \ReflectionFunction($c);
49
50         $stub->class = 'Closure'; // HHVM generates unique class names for closures
51         $a = static::castFunctionAbstract($c, $a, $stub, $isNested);
52
53         if (isset($a[$prefix.'parameters'])) {
54             foreach ($a[$prefix.'parameters']->value as &$v) {
55                 $param = $v;
56                 $v = new EnumStub(array());
57                 foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
58                     if ("\0" === $k[0]) {
59                         $v->value[substr($k, 3)] = $param;
60                     }
61                 }
62                 unset($v->value['position'], $v->value['isVariadic'], $v->value['byReference'], $v);
63             }
64         }
65
66         if ($f = $c->getFileName()) {
67             $a[$prefix.'file'] = $f;
68             $a[$prefix.'line'] = $c->getStartLine().' to '.$c->getEndLine();
69         }
70
71         $prefix = Caster::PREFIX_DYNAMIC;
72         unset($a['name'], $a[$prefix.'this'], $a[$prefix.'parameter'], $a[Caster::PREFIX_VIRTUAL.'extra']);
73
74         return $a;
75     }
76
77     public static function castGenerator(\Generator $c, array $a, Stub $stub, $isNested)
78     {
79         if (!class_exists('ReflectionGenerator', false)) {
80             return $a;
81         }
82
83         // Cannot create ReflectionGenerator based on a terminated Generator
84         try {
85             $reflectionGenerator = new \ReflectionGenerator($c);
86         } catch (\Exception $e) {
87             $a[Caster::PREFIX_VIRTUAL.'closed'] = true;
88
89             return $a;
90         }
91
92         return self::castReflectionGenerator($reflectionGenerator, $a, $stub, $isNested);
93     }
94
95     public static function castType(\ReflectionType $c, array $a, Stub $stub, $isNested)
96     {
97         $prefix = Caster::PREFIX_VIRTUAL;
98
99         $a += array(
100             $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : $c->__toString(),
101             $prefix.'allowsNull' => $c->allowsNull(),
102             $prefix.'isBuiltin' => $c->isBuiltin(),
103         );
104
105         return $a;
106     }
107
108     public static function castReflectionGenerator(\ReflectionGenerator $c, array $a, Stub $stub, $isNested)
109     {
110         $prefix = Caster::PREFIX_VIRTUAL;
111
112         if ($c->getThis()) {
113             $a[$prefix.'this'] = new CutStub($c->getThis());
114         }
115         $function = $c->getFunction();
116         $frame = array(
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(),
122         );
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,
129             ));
130             $trace[] = $frame;
131             $a[$prefix.'trace'] = new TraceStub($trace, false, 0, -1, -1);
132         } else {
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'],
137             ));
138         }
139
140         $a[Caster::PREFIX_VIRTUAL.'closed'] = false;
141
142         return $a;
143     }
144
145     public static function castClass(\ReflectionClass $c, array $a, Stub $stub, $isNested, $filter = 0)
146     {
147         $prefix = Caster::PREFIX_VIRTUAL;
148
149         if ($n = \Reflection::getModifierNames($c->getModifiers())) {
150             $a[$prefix.'modifiers'] = implode(' ', $n);
151         }
152
153         self::addMap($a, $c, array(
154             'extends' => 'getParentClass',
155             'implements' => 'getInterfaceNames',
156             'constants' => 'getConstants',
157         ));
158
159         foreach ($c->getProperties() as $n) {
160             $a[$prefix.'properties'][$n->name] = $n;
161         }
162
163         foreach ($c->getMethods() as $n) {
164             $a[$prefix.'methods'][$n->name] = $n;
165         }
166
167         if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
168             self::addExtra($a, $c);
169         }
170
171         return $a;
172     }
173
174     public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, array $a, Stub $stub, $isNested, $filter = 0)
175     {
176         $prefix = Caster::PREFIX_VIRTUAL;
177
178         self::addMap($a, $c, array(
179             'returnsReference' => 'returnsReference',
180             'returnType' => 'getReturnType',
181             'class' => 'getClosureScopeClass',
182             'this' => 'getClosureThis',
183         ));
184
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;
189         }
190         if (isset($a[$prefix.'this'])) {
191             $a[$prefix.'this'] = new CutStub($a[$prefix.'this']);
192         }
193
194         foreach ($c->getParameters() as $v) {
195             $k = '$'.$v->name;
196             if (method_exists($v, 'isVariadic') && $v->isVariadic()) {
197                 $k = '...'.$k;
198             }
199             if ($v->isPassedByReference()) {
200                 $k = '&'.$k;
201             }
202             $a[$prefix.'parameters'][$k] = $v;
203         }
204         if (isset($a[$prefix.'parameters'])) {
205             $a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
206         }
207
208         if ($v = $c->getStaticVariables()) {
209             foreach ($v as $k => &$v) {
210                 $a[$prefix.'use']['$'.$k] = &$v;
211             }
212             unset($v);
213             $a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
214         }
215
216         if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
217             self::addExtra($a, $c);
218         }
219
220         // Added by HHVM
221         unset($a[Caster::PREFIX_DYNAMIC.'static']);
222
223         return $a;
224     }
225
226     public static function castMethod(\ReflectionMethod $c, array $a, Stub $stub, $isNested)
227     {
228         $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
229
230         return $a;
231     }
232
233     public static function castParameter(\ReflectionParameter $c, array $a, Stub $stub, $isNested)
234     {
235         $prefix = Caster::PREFIX_VIRTUAL;
236
237         // Added by HHVM
238         unset($a['info']);
239
240         self::addMap($a, $c, array(
241             'position' => 'getPosition',
242             'isVariadic' => 'isVariadic',
243             'byReference' => 'isPassedByReference',
244             'allowsNull' => 'allowsNull',
245         ));
246
247         if (method_exists($c, 'getType')) {
248             if ($v = $c->getType()) {
249                 $a[$prefix.'typeHint'] = $v instanceof \ReflectionNamedType ? $v->getName() : $v->__toString();
250             }
251         } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $c, $v)) {
252             $a[$prefix.'typeHint'] = $v[1];
253         }
254         if (!isset($a[$prefix.'typeHint'])) {
255             unset($a[$prefix.'allowsNull']);
256         }
257
258         try {
259             $a[$prefix.'default'] = $v = $c->getDefaultValue();
260             if (method_exists($c, 'isDefaultValueConstant') && $c->isDefaultValueConstant()) {
261                 $a[$prefix.'default'] = new ConstStub($c->getDefaultValueConstantName(), $v);
262             }
263             if (null === $v) {
264                 unset($a[$prefix.'allowsNull']);
265             }
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']);
270             }
271         }
272
273         return $a;
274     }
275
276     public static function castProperty(\ReflectionProperty $c, array $a, Stub $stub, $isNested)
277     {
278         $a[Caster::PREFIX_VIRTUAL.'modifiers'] = implode(' ', \Reflection::getModifierNames($c->getModifiers()));
279         self::addExtra($a, $c);
280
281         return $a;
282     }
283
284     public static function castExtension(\ReflectionExtension $c, array $a, Stub $stub, $isNested)
285     {
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',
295         ));
296
297         return $a;
298     }
299
300     public static function castZendExtension(\ReflectionZendExtension $c, array $a, Stub $stub, $isNested)
301     {
302         self::addMap($a, $c, array(
303             'version' => 'getVersion',
304             'author' => 'getAuthor',
305             'copyright' => 'getCopyright',
306             'url' => 'getURL',
307         ));
308
309         return $a;
310     }
311
312     private static function addExtra(&$a, \Reflector $c)
313     {
314         $x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();
315
316         if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
317             $x['file'] = $m;
318             $x['line'] = $c->getStartLine().' to '.$c->getEndLine();
319         }
320
321         self::addMap($x, $c, self::$extraMap, '');
322
323         if ($x) {
324             $a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
325         }
326     }
327
328     private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)
329     {
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;
333             }
334         }
335     }
336 }