InterfaceTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 17.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.38%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 14
c 3
b 0
f 1
lcom 1
cbo 2
dl 18
loc 105
rs 10
ccs 29
cts 37
cp 0.7838

6 Methods

Rating   Name   Duplication   Size   Complexity  
getIndex() 0 1 ?
A getInterfaces() 0 12 3
A getSelfInterfaces() 18 18 4
A getInterfaceNames() 0 12 3
A hasInterface() 0 11 3
A addInterface() 0 6 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\Parts;
4
5
use Benoth\StaticReflection\Reflection\ReflectionInterface;
6
use Benoth\StaticReflection\ReflectionsIndex;
7
8
trait InterfaceTrait
9
{
10
    /**
11
     * @type string[]
12
     */
13
    protected $interfaces = [];
14
15
    /**
16
     * Must be implemented by classes using this trait
17
     */
18
    abstract public function getIndex();
19
20
    /**
21
     * Gets the interfaces, including inherited ones.
22
     *
23
     * @return ReflectionInterface[]
24
     */
25 417
    public function getInterfaces()
26
    {
27 417
        $interfaces = $this->getSelfInterfaces();
28
29 417
        foreach ($interfaces as $interface) {
30 123
            foreach ($interface->getInterfaces() as $parentInterface) {
31 123
                $interfaces[$parentInterface->getName()] = $parentInterface;
32 123
            }
33 417
        }
34
35 417
        return $interfaces;
36
    }
37
38
    /**
39
     * Gets the interfaces, without inherited ones.
40
     *
41
     * @return ReflectionInterface[]
42
     */
43 453 View Code Duplication
    public function getSelfInterfaces()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
44
    {
45 453
        $interfaces = [];
46
47 453
        if (!$this->getIndex() instanceof ReflectionsIndex) {
48
            return $interfaces;
49
        }
50
51 453
        foreach ($this->interfaces as $interface) {
52 303
            $interface = $this->getIndex()->getInterface($interface);
53 303
            if (!$interface instanceof ReflectionInterface) {
54 171
                continue;
55
            }
56 132
            $interfaces[$interface->getName()] = $interface;
57 453
        }
58
59 453
        return $interfaces;
60
    }
61
62
    /**
63
     * Gets the interfaces names, including inherited ones.
64
     *
65
     * @return string[] A numerical array with interface names as the values
66
     */
67 30
    public function getInterfaceNames()
68
    {
69 30
        $interfaces = $this->interfaces;
70
71 30
        foreach ($this->getInterfaces() as $interface) {
72 6
            if (!in_array($interface->getName(), $interfaces)) {
73 6
                $interfaces[] = $interface->getName();
74 6
            }
75 30
        }
76
77 30
        return $interfaces;
78
    }
79
80
    /**
81
     * Check whether current entity implements or extends an interface
82
     *
83
     * @param  string  $interfaceName The interface fully qualified name
84
     *
85
     * @return bool
86
     */
87
    public function hasInterface($interfaceName)
88
    {
89
        $interfaceName = ltrim($interfaceName, '\\');
90
        foreach ($this->getInterfaces() as $interface) {
91
            if ($interfaceName === $interface->getName()) {
92
                return true;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * Add an interface.
101
     *
102
     * @param string $interface Fully Qualified Interface Name
103
     *
104
     * @return self
105
     */
106 708
    public function addInterface($interface)
107
    {
108 708
        $this->interfaces[] = $interface;
109
110 708
        return $this;
111
    }
112
}
113