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 switch to ksort after we drop support for 5.3:
81 // ksort($methods, SORT_NATURAL | SORT_FLAG_CASE);
82 uksort($methods, 'strnatcasecmp');
88 * Prepare formatted method array.
90 * @param array $methods
94 protected function prepareMethods(array $methods)
96 // My kingdom for a generator.
99 foreach ($methods as $name => $method) {
100 if ($this->showItem($name)) {
103 'style' => $this->getVisibilityStyle($method),
104 'value' => $this->presentSignature($method),
113 * Get a label for the particular kind of "class" represented.
115 * @param \ReflectionClass $reflector
119 protected function getKindLabel(\ReflectionClass $reflector)
121 if ($reflector->isInterface()) {
122 return 'Interface Methods';
123 } elseif (method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
124 return 'Trait Methods';
126 return 'Class Methods';
131 * Get output style for the given method's visibility.
133 * @param \ReflectionMethod $method
137 private function getVisibilityStyle(\ReflectionMethod $method)
139 if ($method->isPublic()) {
140 return self::IS_PUBLIC;
141 } elseif ($method->isProtected()) {
142 return self::IS_PROTECTED;
144 return self::IS_PRIVATE;