Yaffs site version 1.1
[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 this should be natcasesort
81         ksort($methods);
82
83         return $methods;
84     }
85
86     /**
87      * Prepare formatted method array.
88      *
89      * @param array $methods
90      *
91      * @return array
92      */
93     protected function prepareMethods(array $methods)
94     {
95         // My kingdom for a generator.
96         $ret = array();
97
98         foreach ($methods as $name => $method) {
99             if ($this->showItem($name)) {
100                 $ret[$name] = array(
101                     'name'  => $name,
102                     'style' => $this->getVisibilityStyle($method),
103                     'value' => $this->presentSignature($method),
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 Methods';
122         } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
123             return 'Trait Methods';
124         } else {
125             return 'Class Methods';
126         }
127     }
128
129     /**
130      * Get output style for the given method's visibility.
131      *
132      * @param \ReflectionMethod $method
133      *
134      * @return string
135      */
136     private function getVisibilityStyle(\ReflectionMethod $method)
137     {
138         if ($method->isPublic()) {
139             return self::IS_PUBLIC;
140         } elseif ($method->isProtected()) {
141             return self::IS_PROTECTED;
142         } else {
143             return self::IS_PRIVATE;
144         }
145     }
146 }