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 Psy\Reflection\ReflectionConstant;
15 use Symfony\Component\Console\Input\InputInterface;
18 * Class Constant Enumerator class.
20 class ClassConstantEnumerator extends Enumerator
25 protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
27 // only list constants when a Reflector is present.
29 if ($reflector === null) {
33 // We can only list constants on actual class (or object) reflectors.
34 if (!$reflector instanceof \ReflectionClass) {
35 // TODO: handle ReflectionExtension as well
39 // only list constants if we are specifically asked
40 if (!$input->getOption('constants')) {
44 $noInherit = $input->getOption('no-inherit');
45 $constants = $this->prepareConstants($this->getConstants($reflector, $noInherit));
47 if (empty($constants)) {
52 $ret[$this->getKindLabel($reflector)] = $constants;
58 * Get defined constants for the given class or object Reflector.
60 * @param \Reflector $reflector
61 * @param bool $noInherit Exclude inherited constants
65 protected function getConstants(\Reflector $reflector, $noInherit = false)
67 $className = $reflector->getName();
70 foreach ($reflector->getConstants() as $name => $constant) {
71 $constReflector = new ReflectionConstant($reflector, $name);
73 if ($noInherit && $constReflector->getDeclaringClass()->getName() !== $className) {
77 $constants[$name] = $constReflector;
80 // TODO: this should be natcasesort
87 * Prepare formatted constant array.
89 * @param array $constants
93 protected function prepareConstants(array $constants)
95 // My kingdom for a generator.
98 foreach ($constants as $name => $constant) {
99 if ($this->showItem($name)) {
102 'style' => self::IS_CONSTANT,
103 'value' => $this->presentRef($constant->getValue()),
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 Constants';
122 } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
123 return 'Trait Constants';
125 return 'Class Constants';