4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command\ListCommand;
14 use Symfony\Component\Console\Input\InputInterface;
17 * Method Enumerator class.
19 class MethodEnumerator extends Enumerator
24 protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
26 // only list methods when a Reflector is present.
28 if ($reflector === null) {
32 // We can only list methods on actual class (or object) reflectors.
33 if (!$reflector instanceof \ReflectionClass) {
37 // only list methods if we are specifically asked
38 if (!$input->getOption('methods')) {
42 $showAll = $input->getOption('all');
43 $noInherit = $input->getOption('no-inherit');
44 $methods = $this->prepareMethods($this->getMethods($showAll, $reflector, $noInherit));
46 if (empty($methods)) {
51 $ret[$this->getKindLabel($reflector)] = $methods;
57 * Get defined methods for the given class or object Reflector.
59 * @param bool $showAll Include private and protected methods
60 * @param \Reflector $reflector
61 * @param bool $noInherit Exclude inherited methods
65 protected function getMethods($showAll, \Reflector $reflector, $noInherit = false)
67 $className = $reflector->getName();
70 foreach ($reflector->getMethods() as $name => $method) {
71 if ($noInherit && $method->getDeclaringClass()->getName() !== $className) {
75 if ($showAll || $method->isPublic()) {
76 $methods[$method->getName()] = $method;
80 // @todo this should be natcasesort
87 * Prepare formatted method array.
89 * @param array $methods
93 protected function prepareMethods(array $methods)
95 // My kingdom for a generator.
98 foreach ($methods as $name => $method) {
99 if ($this->showItem($name)) {
102 'style' => $this->getVisibilityStyle($method),
103 'value' => $this->presentSignature($method),
112 * Get a label for the particular kind of "class" represented.
114 * @param \ReflectionClass $reflector
118 protected function getKindLabel(\ReflectionClass $reflector)
120 if ($reflector->isInterface()) {
121 return 'Interface Methods';
122 } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
123 return 'Trait Methods';
125 return 'Class Methods';
130 * Get output style for the given method's visibility.
132 * @param \ReflectionMethod $method
136 private function getVisibilityStyle(\ReflectionMethod $method)
138 if ($method->isPublic()) {
139 return self::IS_PUBLIC;
140 } elseif ($method->isProtected()) {
141 return self::IS_PROTECTED;
143 return self::IS_PRIVATE;