ReflectionMethod   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 128
Duplicated Lines 5.47 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 32
c 2
b 1
f 0
lcom 1
cbo 7
dl 7
loc 128
rs 9.6
ccs 0
cts 63
cp 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isConstructor() 0 4 1
A isDestructor() 0 4 1
D getPrototype() 0 31 10
D getParent() 7 47 19
A setAccessible() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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