Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / PropertyEnumerator.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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 = array();
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 = array();
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         // @todo this should be natcasesort
81         ksort($properties);
82
83         return $properties;
84     }
85
86     /**
87      * Prepare formatted property array.
88      *
89      * @param array $properties
90      *
91      * @return array
92      */
93     protected function prepareProperties(array $properties, $target = null)
94     {
95         // My kingdom for a generator.
96         $ret = array();
97
98         foreach ($properties as $name => $property) {
99             if ($this->showItem($name)) {
100                 $fname = '$' . $name;
101                 $ret[$fname] = array(
102                     'name'  => $fname,
103                     'style' => $this->getVisibilityStyle($property),
104                     'value' => $this->presentValue($property, $target),
105                 );
106             }
107         }
108
109         return $ret;
110     }
111
112     /**
113      * Get a label for the particular kind of "class" represented.
114      *
115      * @param \ReflectionClass $reflector
116      *
117      * @return string
118      */
119     protected function getKindLabel(\ReflectionClass $reflector)
120     {
121         if ($reflector->isInterface()) {
122             return 'Interface Properties';
123         } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
124             return 'Trait Properties';
125         } else {
126             return 'Class Properties';
127         }
128     }
129
130     /**
131      * Get output style for the given property's visibility.
132      *
133      * @param \ReflectionProperty $property
134      *
135      * @return string
136      */
137     private function getVisibilityStyle(\ReflectionProperty $property)
138     {
139         if ($property->isPublic()) {
140             return self::IS_PUBLIC;
141         } elseif ($property->isProtected()) {
142             return self::IS_PROTECTED;
143         } else {
144             return self::IS_PRIVATE;
145         }
146     }
147
148     /**
149      * Present the $target's current value for a reflection property.
150      *
151      * @param \ReflectionProperty $property
152      * @param mixed               $target
153      *
154      * @return string
155      */
156     protected function presentValue(\ReflectionProperty $property, $target)
157     {
158         // If $target is a class, trait or interface (try to) get the default
159         // value for the property.
160         if (!is_object($target)) {
161             try {
162                 $refl = new \ReflectionClass($target);
163                 $props = $refl->getDefaultProperties();
164                 if (array_key_exists($property->name, $props)) {
165                     $suffix = $property->isStatic() ? '' : ' <aside>(default)</aside>';
166
167                     return $this->presentRef($props[$property->name]) . $suffix;
168                 }
169             } catch (\Exception $e) {
170                 // Well, we gave it a shot.
171             }
172
173             return '';
174         }
175
176         $property->setAccessible(true);
177         $value = $property->getValue($target);
178
179         return $this->presentRef($value);
180     }
181 }