Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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): array
25
    {
26
        // only list methods when a Reflector is present.
27
        if ($reflector === null) {
28
            return [];
29
        }
30
 
31
        // We can only list methods on actual class (or object) reflectors.
32
        if (!$reflector instanceof \ReflectionClass) {
33
            return [];
34
        }
35
 
36
        // only list methods if we are specifically asked
37
        if (!$input->getOption('methods')) {
38
            return [];
39
        }
40
 
41
        $showAll = $input->getOption('all');
42
        $noInherit = $input->getOption('no-inherit');
43
        $methods = $this->prepareMethods($this->getMethods($showAll, $reflector, $noInherit));
44
 
45
        if (empty($methods)) {
46
            return [];
47
        }
48
 
49
        $ret = [];
50
        $ret[$this->getKindLabel($reflector)] = $methods;
51
 
52
        return $ret;
53
    }
54
 
55
    /**
56
     * Get defined methods for the given class or object Reflector.
57
     *
200 lars 58
     * @param bool             $showAll   Include private and protected methods
59
     * @param \ReflectionClass $reflector
60
     * @param bool             $noInherit Exclude inherited methods
148 lars 61
     *
62
     * @return array
63
     */
200 lars 64
    protected function getMethods(bool $showAll, \ReflectionClass $reflector, bool $noInherit = false): array
148 lars 65
    {
66
        $className = $reflector->getName();
67
 
68
        $methods = [];
69
        foreach ($reflector->getMethods() as $name => $method) {
70
            // For some reason PHP reflection shows private methods from the parent class, even
71
            // though they're effectively worthless. Let's suppress them here, like --no-inherit
72
            if (($noInherit || $method->isPrivate()) && $method->getDeclaringClass()->getName() !== $className) {
73
                continue;
74
            }
75
 
76
            if ($showAll || $method->isPublic()) {
77
                $methods[$method->getName()] = $method;
78
            }
79
        }
80
 
81
        \ksort($methods, \SORT_NATURAL | \SORT_FLAG_CASE);
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): array
94
    {
95
        // My kingdom for a generator.
96
        $ret = [];
97
 
98
        foreach ($methods as $name => $method) {
99
            if ($this->showItem($name)) {
100
                $ret[$name] = [
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
    protected function getKindLabel(\ReflectionClass $reflector): string
117
    {
118
        if ($reflector->isInterface()) {
119
            return 'Interface Methods';
120
        } elseif (\method_exists($reflector, 'isTrait') && $reflector->isTrait()) {
121
            return 'Trait Methods';
122
        } else {
123
            return 'Class Methods';
124
        }
125
    }
126
 
127
    /**
128
     * Get output style for the given method's visibility.
129
     *
130
     * @param \ReflectionMethod $method
131
     */
132
    private function getVisibilityStyle(\ReflectionMethod $method): string
133
    {
134
        if ($method->isPublic()) {
135
            return self::IS_PUBLIC;
136
        } elseif ($method->isProtected()) {
137
            return self::IS_PROTECTED;
138
        } else {
139
            return self::IS_PRIVATE;
140
        }
141
    }
142
}