Version 1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / ClassConstantEnumerator.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 Psy\Reflection\ReflectionConstant;
15 use Symfony\Component\Console\Input\InputInterface;
16
17 /**
18  * Class Constant Enumerator class.
19  */
20 class ClassConstantEnumerator extends Enumerator
21 {
22     /**
23      * {@inheritdoc}
24      */
25     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
26     {
27         // only list constants when a Reflector is present.
28
29         if ($reflector === null) {
30             return;
31         }
32
33         // We can only list constants on actual class (or object) reflectors.
34         if (!$reflector instanceof \ReflectionClass) {
35             // TODO: handle ReflectionExtension as well
36             return;
37         }
38
39         // only list constants if we are specifically asked
40         if (!$input->getOption('constants')) {
41             return;
42         }
43
44         $noInherit = $input->getOption('no-inherit');
45         $constants = $this->prepareConstants($this->getConstants($reflector, $noInherit));
46
47         if (empty($constants)) {
48             return;
49         }
50
51         $ret = array();
52         $ret[$this->getKindLabel($reflector)] = $constants;
53
54         return $ret;
55     }
56
57     /**
58      * Get defined constants for the given class or object Reflector.
59      *
60      * @param \Reflector $reflector
61      * @param bool       $noInherit Exclude inherited constants
62      *
63      * @return array
64      */
65     protected function getConstants(\Reflector $reflector, $noInherit = false)
66     {
67         $className = $reflector->getName();
68
69         $constants = array();
70         foreach ($reflector->getConstants() as $name => $constant) {
71             $constReflector = new ReflectionConstant($reflector, $name);
72
73             if ($noInherit && $constReflector->getDeclaringClass()->getName() !== $className) {
74                 continue;
75             }
76
77             $constants[$name] = $constReflector;
78         }
79
80         // TODO: this should be natcasesort
81         ksort($constants);
82
83         return $constants;
84     }
85
86     /**
87      * Prepare formatted constant array.
88      *
89      * @param array $constants
90      *
91      * @return array
92      */
93     protected function prepareConstants(array $constants)
94     {
95         // My kingdom for a generator.
96         $ret = array();
97
98         foreach ($constants as $name => $constant) {
99             if ($this->showItem($name)) {
100                 $ret[$name] = array(
101                     'name'  => $name,
102                     'style' => self::IS_CONSTANT,
103                     'value' => $this->presentRef($constant->getValue()),
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 Constants';
122         } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
123             return 'Trait Constants';
124         } else {
125             return 'Class Constants';
126         }
127     }
128 }