15ce45db7a06b638f7e72280c6a10edb0b329216
[yaffs-website] / vendor / psy / psysh / src / Command / ListCommand / PropertyEnumerator.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
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 Psy\Command\ListCommand;
13
14 use Symfony\Component\Console\Input\InputInterface;
15
16 /**
17  * Property Enumerator class.
18  */
19 class PropertyEnumerator extends Enumerator
20 {
21     /**
22      * {@inheritdoc}
23      */
24     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
25     {
26         // only list properties when a Reflector is present.
27
28         if ($reflector === null) {
29             return;
30         }
31
32         // We can only list properties on actual class (or object) reflectors.
33         if (!$reflector instanceof \ReflectionClass) {
34             return;
35         }
36
37         // only list properties if we are specifically asked
38         if (!$input->getOption('properties')) {
39             return;
40         }
41
42         $showAll    = $input->getOption('all');
43         $noInherit  = $input->getOption('no-inherit');
44         $properties = $this->prepareProperties($this->getProperties($showAll, $reflector, $noInherit), $target);
45
46         if (empty($properties)) {
47             return;
48         }
49
50         $ret = [];
51         $ret[$this->getKindLabel($reflector)] = $properties;
52
53         return $ret;
54     }
55
56     /**
57      * Get defined properties for the given class or object Reflector.
58      *
59      * @param bool       $showAll   Include private and protected properties
60      * @param \Reflector $reflector
61      * @param bool       $noInherit Exclude inherited properties
62      *
63      * @return array
64      */
65     protected function getProperties($showAll, \Reflector $reflector, $noInherit = false)
66     {
67         $className = $reflector->getName();
68
69         $properties = [];
70         foreach ($reflector->getProperties() as $property) {
71             if ($noInherit && $property->getDeclaringClass()->getName() !== $className) {
72                 continue;
73             }
74
75             if ($showAll || $property->isPublic()) {
76                 $properties[$property->getName()] = $property;
77             }
78         }
79
80         ksort($properties, SORT_NATURAL | SORT_FLAG_CASE);
81
82         return $properties;
83     }
84
85     /**
86      * Prepare formatted property array.
87      *
88      * @param array $properties
89      *
90      * @return array
91      */
92     protected function prepareProperties(array $properties, $target = null)
93     {
94         // My kingdom for a generator.
95         $ret = [];
96
97         foreach ($properties as $name => $property) {
98             if ($this->showItem($name)) {
99                 $fname = '$' . $name;
100                 $ret[$fname] = [
101                     'name'  => $fname,
102                     'style' => $this->getVisibilityStyle($property),
103                     'value' => $this->presentValue($property, $target),
104                 ];
105             }
106         }
107
108         return $ret;
109     }
110
111     /**
112      * Get a label for the particular kind of "class" represented.
113      *
114      * @param \ReflectionClass $reflector
115      *
116      * @return string
117      */
118     protected function getKindLabel(\ReflectionClass $reflector)
119     {
120         if ($reflector->isInterface()) {
121             return 'Interface Properties';
122         } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
123             return 'Trait Properties';
124         } else {
125             return 'Class Properties';
126         }
127     }
128
129     /**
130      * Get output style for the given property's visibility.
131      *
132      * @param \ReflectionProperty $property
133      *
134      * @return string
135      */
136     private function getVisibilityStyle(\ReflectionProperty $property)
137     {
138         if ($property->isPublic()) {
139             return self::IS_PUBLIC;
140         } elseif ($property->isProtected()) {
141             return self::IS_PROTECTED;
142         } else {
143             return self::IS_PRIVATE;
144         }
145     }
146
147     /**
148      * Present the $target's current value for a reflection property.
149      *
150      * @param \ReflectionProperty $property
151      * @param mixed               $target
152      *
153      * @return string
154      */
155     protected function presentValue(\ReflectionProperty $property, $target)
156     {
157         // If $target is a class, trait or interface (try to) get the default
158         // value for the property.
159         if (!is_object($target)) {
160             try {
161                 $refl = new \ReflectionClass($target);
162                 $props = $refl->getDefaultProperties();
163                 if (array_key_exists($property->name, $props)) {
164                     $suffix = $property->isStatic() ? '' : ' <aside>(default)</aside>';
165
166                     return $this->presentRef($props[$property->name]) . $suffix;
167                 }
168             } catch (\Exception $e) {
169                 // Well, we gave it a shot.
170             }
171
172             return '';
173         }
174
175         $property->setAccessible(true);
176         $value = $property->getValue($target);
177
178         return $this->presentRef($value);
179     }
180 }