4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command\ListCommand;
14 use Symfony\Component\Console\Input\InputInterface;
17 * Property Enumerator class.
19 class PropertyEnumerator extends Enumerator
24 protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
26 // only list properties when a Reflector is present.
28 if ($reflector === null) {
32 // We can only list properties on actual class (or object) reflectors.
33 if (!$reflector instanceof \ReflectionClass) {
37 // only list properties if we are specifically asked
38 if (!$input->getOption('properties')) {
42 $showAll = $input->getOption('all');
43 $noInherit = $input->getOption('no-inherit');
44 $properties = $this->prepareProperties($this->getProperties($showAll, $reflector, $noInherit), $target);
46 if (empty($properties)) {
51 $ret[$this->getKindLabel($reflector)] = $properties;
57 * Get defined properties for the given class or object Reflector.
59 * @param bool $showAll Include private and protected properties
60 * @param \Reflector $reflector
61 * @param bool $noInherit Exclude inherited properties
65 protected function getProperties($showAll, \Reflector $reflector, $noInherit = false)
67 $className = $reflector->getName();
70 foreach ($reflector->getProperties() as $property) {
71 if ($noInherit && $property->getDeclaringClass()->getName() !== $className) {
75 if ($showAll || $property->isPublic()) {
76 $properties[$property->getName()] = $property;
80 ksort($properties, SORT_NATURAL | SORT_FLAG_CASE);
86 * Prepare formatted property array.
88 * @param array $properties
92 protected function prepareProperties(array $properties, $target = null)
94 // My kingdom for a generator.
97 foreach ($properties as $name => $property) {
98 if ($this->showItem($name)) {
102 'style' => $this->getVisibilityStyle($property),
103 'value' => $this->presentValue($property, $target),
112 * Get a label for the particular kind of "class" represented.
114 * @param \ReflectionClass $reflector
118 protected function getKindLabel(\ReflectionClass $reflector)
120 if ($reflector->isInterface()) {
121 return 'Interface Properties';
122 } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
123 return 'Trait Properties';
125 return 'Class Properties';
130 * Get output style for the given property's visibility.
132 * @param \ReflectionProperty $property
136 private function getVisibilityStyle(\ReflectionProperty $property)
138 if ($property->isPublic()) {
139 return self::IS_PUBLIC;
140 } elseif ($property->isProtected()) {
141 return self::IS_PROTECTED;
143 return self::IS_PRIVATE;
148 * Present the $target's current value for a reflection property.
150 * @param \ReflectionProperty $property
151 * @param mixed $target
155 protected function presentValue(\ReflectionProperty $property, $target)
157 // If $target is a class, trait or interface (try to) get the default
158 // value for the property.
159 if (!is_object($target)) {
161 $refl = new \ReflectionClass($target);
162 $props = $refl->getDefaultProperties();
163 if (array_key_exists($property->name, $props)) {
164 $suffix = $property->isStatic() ? '' : ' <aside>(default)</aside>';
166 return $this->presentRef($props[$property->name]) . $suffix;
168 } catch (\Exception $e) {
169 // Well, we gave it a shot.
175 $property->setAccessible(true);
176 $value = $property->getValue($target);
178 return $this->presentRef($value);