InterfaceTrait::getInterfaces()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
ccs 8
cts 8
cp 1
cc 3
eloc 6
nc 3
nop 0
crap 3
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