ReflectionMethod::getPrototype()   D
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 4.8196
ccs 0
cts 20
cp 0
cc 10
eloc 17
nc 10
nop 0
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Benoth\StaticReflection\Reflection;
4
5
class ReflectionMethod extends ReflectionFunctionAbstract
6
{
7
    use Parts\VisibilityTrait;
8
    use Parts\StaticTrait;
9
    use Parts\AbstractTrait;
10
    use Parts\FinalTrait;
11
    use Parts\DeclaringClassLikeTrait;
12
13
    /**
14
     * Checks if the method is a constructor.
15
     *
16
     * @return bool
17
     */
18
    public function isConstructor()
19
    {
20
        return $this->getName() === '__construct';
21
    }
22
23
    /**
24
     * Checks if the method is a destructor.
25
     *
26
     * @return bool
27
     */
28
    public function isDestructor()
29
    {
30
        return $this->getName() === '__destruct';
31
    }
32
33
    /**
34
     * Returns the methods prototype.
35
     *
36
     * @throws \ReflectionException If the method does not have a prototype.
37
     *
38
     * @return ReflectionMethod
39
     */
40
    public function getPrototype()
41
    {
42
        $parent     = $this->getDeclaringClass();
43
        $methodName = $this->getName();
44
45
        while (!is_null($parent)) {
46
            if ($parent instanceof ReflectionClass || $parent instanceof ReflectionInterface) {
47
                foreach ($parent->getSelfInterfaces() as $interface) {
48
                    if ($interface->hasMethod($methodName)) {
49
                        return $interface->getMethod($methodName);
50
                    }
51
                }
52
            }
53
54
            if (!$parent instanceof ReflectionClass) {
55
                $parent = null;
56
                continue;
57
            }
58
59
            $parent = $parent->getParentClass();
60
            if (is_null($parent)) {
61
                continue;
62
            }
63
64
            if ($parent->hasMethod($methodName) && $parent->getMethod($methodName)->isAbstract()) {
65
                return $parent->getMethod($methodName);
66
            }
67
        }
68
69
        throw new \ReflectionException('Method '.$this->getDeclaringClass()->getName().'::'.$this->getName().' does not have a prototype');
70
    }
71
72
    public function getParent()
73
    {
74
        $class = $this->getDeclaringClass();
75
76
        $methods = [];
77
        foreach ($class->getSelfMethods() as $methodName => $method) {
78
            if ($methodName === $this->getName()) {
79
                $methods[] = $method;
80
            }
81
        }
82
83
        if ($class instanceof ReflectionClass) {
84
            foreach ($class->getTraitsMethods() as $methodName => $method) {
85
                if ($methodName === $this->getName()) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $methodName (integer) and $this->getName() (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
86
                    $methods[] = $method;
87
                }
88
            }
89
        }
90
91
        if (($class instanceof ReflectionClass || $class instanceof \ReflectionClass) && ($class->getParentClass() instanceof ReflectionClass || $class->getParentClass() instanceof \ReflectionClass)) {
92
            foreach ($class->getParentClass()->getMethods() as $methodName => $method) {
93
                if (is_int($methodName)) {
94
                    $methodName = $method->getName();
95
                }
96
                if ($methodName === $this->getName()) {
97
                    $methods[] = $method;
98
                }
99
            }
100
        }
101
102
        if ($class instanceof ReflectionClass || $class instanceof \ReflectionClass) {
103 View Code Duplication
            foreach ($class->getInterfaces() as $interfaceName => $interface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
                foreach ($interface->getMethods() as $methodName => $method) {
105
                    if ($methodName === $this->getName()) {
106
                        $methods[] = $method;
107
                    }
108
                }
109
            }
110
        }
111
112
        if (count($methods) >= 2) {
113
            // $methods[0] is the current method
114
            return $methods[1];
115
        }
116
117
        return;
118
    }
119
120
    /**
121
     * Sets a method to be accessible.
122
     *
123
     *
124
     * @param bool $accessible
125
     *
126
     * @throws \ReflectionException Always... Can't be implemented.
127
     */
128
    public function setAccessible($accessible)
0 ignored issues
show
Unused Code introduced by
The parameter $accessible is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
    {
130
        throw new \ReflectionException('StaticReflection can\'t change a method accessibility');
131
    }
132
}
133