Security update for Core, with self-updated composer
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / MethodEnumerator.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 Symfony\Component\Console\Input\InputInterface;
15
16 /**
17  * Method Enumerator class.
18  */
19 class MethodEnumerator extends Enumerator
20 {
21     /**
22      * {@inheritdoc}
23      */
24     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
25     {
26         // only list methods when a Reflector is present.
27
28         if ($reflector === null) {
29             return;
30         }
31
32         // We can only list methods on actual class (or object) reflectors.
33         if (!$reflector instanceof \ReflectionClass) {
34             return;
35         }
36
37         // only list methods if we are specifically asked
38         if (!$input->getOption('methods')) {
39             return;
40         }
41
42         $showAll   = $input->getOption('all');
43         $noInherit = $input->getOption('no-inherit');
44         $methods   = $this->prepareMethods($this->getMethods($showAll, $reflector, $noInherit));
45
46         if (empty($methods)) {
47             return;
48         }
49
50         $ret = array();
51         $ret[$this->getKindLabel($reflector)] = $methods;
52
53         return $ret;
54     }
55
56     /**
57      * Get defined methods for the given class or object Reflector.
58      *
59      * @param bool       $showAll   Include private and protected methods
60      * @param \Reflector $reflector
61      * @param bool       $noInherit Exclude inherited methods
62      *
63      * @return array
64      */
65     protected function getMethods($showAll, \Reflector $reflector, $noInherit = false)
66     {
67         $className = $reflector->getName();
68
69         $methods = array();
70         foreach ($reflector->getMethods() as $name => $method) {
71             if ($noInherit && $method->getDeclaringClass()->getName() !== $className) {
72                 continue;
73             }
74
75             if ($showAll || $method->isPublic()) {
76                 $methods[$method->getName()] = $method;
77             }
78         }
79
80         // @todo switch to ksort after we drop support for 5.3:
81         //     ksort($methods, SORT_NATURAL | SORT_FLAG_CASE);
82         uksort($methods, 'strnatcasecmp');
83
84         return $methods;
85     }
86
87     /**
88      * Prepare formatted method array.
89      *
90      * @param array $methods
91      *
92      * @return array
93      */
94     protected function prepareMethods(array $methods)
95     {
96         // My kingdom for a generator.
97         $ret = array();
98
99         foreach ($methods as $name => $method) {
100             if ($this->showItem($name)) {
101                 $ret[$name] = array(
102                     'name'  => $name,
103                     'style' => $this->getVisibilityStyle($method),
104                     'value' => $this->presentSignature($method),
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 Methods';
123         } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
124             return 'Trait Methods';
125         } else {
126             return 'Class Methods';
127         }
128     }
129
130     /**
131      * Get output style for the given method's visibility.
132      *
133      * @param \ReflectionMethod $method
134      *
135      * @return string
136      */
137     private function getVisibilityStyle(\ReflectionMethod $method)
138     {
139         if ($method->isPublic()) {
140             return self::IS_PUBLIC;
141         } elseif ($method->isProtected()) {
142             return self::IS_PROTECTED;
143         } else {
144             return self::IS_PRIVATE;
145         }
146     }
147 }