4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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();
69 $properties = array();
70 foreach ($reflector->getProperties() as $property) {
71 if ($noInherit && $property->getDeclaringClass()->getName() !== $className) {
75 if ($showAll || $property->isPublic()) {
76 $properties[$property->getName()] = $property;
80 // @todo this should be natcasesort
87 * Prepare formatted property array.
89 * @param array $properties
93 protected function prepareProperties(array $properties, $target = null)
95 // My kingdom for a generator.
98 foreach ($properties as $name => $property) {
99 if ($this->showItem($name)) {
100 $fname = '$' . $name;
101 $ret[$fname] = array(
103 'style' => $this->getVisibilityStyle($property),
104 'value' => $this->presentValue($property, $target),
113 * Get a label for the particular kind of "class" represented.
115 * @param \ReflectionClass $reflector
119 protected function getKindLabel(\ReflectionClass $reflector)
121 if ($reflector->isInterface()) {
122 return 'Interface Properties';
123 } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
124 return 'Trait Properties';
126 return 'Class Properties';
131 * Get output style for the given property's visibility.
133 * @param \ReflectionProperty $property
137 private function getVisibilityStyle(\ReflectionProperty $property)
139 if ($property->isPublic()) {
140 return self::IS_PUBLIC;
141 } elseif ($property->isProtected()) {
142 return self::IS_PROTECTED;
144 return self::IS_PRIVATE;
149 * Present the $target's current value for a reflection property.
151 * @param \ReflectionProperty $property
152 * @param mixed $target
156 protected function presentValue(\ReflectionProperty $property, $target)
158 // If $target is a class, trait or interface (try to) get the default
159 // value for the property.
160 if (!is_object($target)) {
162 $refl = new \ReflectionClass($target);
163 $props = $refl->getDefaultProperties();
164 if (array_key_exists($property->name, $props)) {
165 $suffix = $property->isStatic() ? '' : ' <aside>(default)</aside>';
167 return $this->presentRef($props[$property->name]) . $suffix;
169 } catch (\Exception $e) {
170 // Well, we gave it a shot.
176 $property->setAccessible(true);
177 $value = $property->getValue($target);
179 return $this->presentRef($value);