66d75d0efc8433e0f3700dda1e3b7e4f48115fc7
[yaffs-website] / vendor / psy / psysh / src / Command / ListCommand / ClassConstantEnumerator.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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 = [];
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 = [];
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         ksort($constants, SORT_NATURAL | SORT_FLAG_CASE);
81
82         return $constants;
83     }
84
85     /**
86      * Prepare formatted constant array.
87      *
88      * @param array $constants
89      *
90      * @return array
91      */
92     protected function prepareConstants(array $constants)
93     {
94         // My kingdom for a generator.
95         $ret = [];
96
97         foreach ($constants as $name => $constant) {
98             if ($this->showItem($name)) {
99                 $ret[$name] = [
100                     'name'  => $name,
101                     'style' => self::IS_CONSTANT,
102                     'value' => $this->presentRef($constant->getValue()),
103                 ];
104             }
105         }
106
107         return $ret;
108     }
109
110     /**
111      * Get a label for the particular kind of "class" represented.
112      *
113      * @param \ReflectionClass $reflector
114      *
115      * @return string
116      */
117     protected function getKindLabel(\ReflectionClass $reflector)
118     {
119         if ($reflector->isInterface()) {
120             return 'Interface Constants';
121         } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
122             return 'Trait Constants';
123         } else {
124             return 'Class Constants';
125         }
126     }
127 }