fc974b2d944d4ec2061d49c964ffefea5c2bf5b0
[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 switch to ksort after we drop support for 5.3:
81         //     ksort($constants, SORT_NATURAL | SORT_FLAG_CASE);
82         uksort($constants, 'strnatcasecmp');
83
84         return $constants;
85     }
86
87     /**
88      * Prepare formatted constant array.
89      *
90      * @param array $constants
91      *
92      * @return array
93      */
94     protected function prepareConstants(array $constants)
95     {
96         // My kingdom for a generator.
97         $ret = array();
98
99         foreach ($constants as $name => $constant) {
100             if ($this->showItem($name)) {
101                 $ret[$name] = array(
102                     'name'  => $name,
103                     'style' => self::IS_CONSTANT,
104                     'value' => $this->presentRef($constant->getValue()),
105                 );
106             }
107         }
108
109         return $ret;
110     }
111
112     /**
113      * Get a label for the particular kind of "class" represented.
114      *
115      * @param \ReflectionClass $reflector
116      *
117      * @return string
118      */
119     protected function getKindLabel(\ReflectionClass $reflector)
120     {
121         if ($reflector->isInterface()) {
122             return 'Interface Constants';
123         } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
124             return 'Trait Constants';
125         } else {
126             return 'Class Constants';
127         }
128     }
129 }