ConstantTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 16
c 5
b 2
f 1
lcom 1
cbo 2
dl 0
loc 119
rs 10
ccs 37
cts 37
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
getSelfInterfaces() 0 1 ?
getFileName() 0 1 ?
B getConstants() 0 20 7
A getSelfConstants() 0 9 2
A getConstant() 0 10 3
A hasConstant() 0 10 3
A addConstant() 0 8 1
1
<?php
2
3
namespace Benoth\StaticReflection\Reflection\Parts;
4
5
use Benoth\StaticReflection\Reflection\ReflectionClass;
6
use Benoth\StaticReflection\Reflection\ReflectionConstant;
7
8
trait ConstantTrait
9
{
10
    /**
11
     * @type ReflectionConstant[]
12
     */
13
    protected $constants = [];
14
15
    /**
16
     * Gets the interfaces, without inherited ones.
17
     *
18
     * Must be implemented by classes using this trait
19
     *
20
     * @return ReflectionInterface[]
21
     */
22
    abstract public function getSelfInterfaces();
23
24
    /**
25
     * Gets the filename of the file in which the class has been defined.
26
     *
27
     * Must be implemented by classes using this trait.
28
     *
29
     * @return string
30
     */
31
    abstract public function getFileName();
32
33
    /**
34
     * Gets defined constants from an entity, including inherited ones.
35
     *
36
     * @return ReflectionConstant[]
37
     */
38 36
    public function getConstants()
39
    {
40 36
        $constants = [];
41
42 36
        foreach ($this->getSelfInterfaces() as $interfaceName => $interface) {
43 9
            foreach ($interface->getConstants() as $constantName => $constantValue) {
44 9
                $constants[$constantName] = $constantValue;
45 9
            }
46 36
        }
47
48 36
        if ($this instanceof ReflectionClass && ($this->getParentClass() instanceof ReflectionClass || $this->getParentClass() instanceof \ReflectionClass)) {
49 18
            foreach ($this->getParentClass()->getConstants() as $constantName => $constantValue) {
50 9
                $constants[$constantName] = $constantValue;
51 18
            }
52 18
        }
53
54 36
        $constants = array_merge($constants, $this->getSelfConstants());
55
56 36
        return $constants;
57
    }
58
59
    /**
60
     * Gets defined constants from an entity, without inherited ones.
61
     *
62
     * @return ReflectionConstant[]
63
     */
64 48
    public function getSelfConstants()
65
    {
66 48
        $constants = [];
67 48
        foreach ($this->constants as $constant) {
68 36
            $constants[$constant->getName()] = $constant->getValue();
69 48
        }
70
71 48
        return $constants;
72
    }
73
74
    /**
75
     * Gets defined constant.
76
     *
77
     * @param string $constantSearchedName Name of the constant
78
     *
79
     * @see http://php.net/manual/en/reflectionclass.getconstant.php#113642 Why returning false if the constant does no exists ?
80
     *      It could be the value of the constant... Should throw a \ReflectionException ?
81
     *
82
     * @return mixed Value of the constant, false if constant not found
83
     */
84 12
    public function getConstant($constantSearchedName)
85
    {
86 12
        foreach ($this->getConstants() as $constantName => $constantValue) {
87 12
            if ($constantName === $constantSearchedName) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $constantName (integer) and $constantSearchedName (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
88 12
                return $constantValue;
89
            }
90 9
        }
91
92 9
        return false;
93
    }
94
95
    /**
96
     * Checks if constant is defined.
97
     *
98
     * @param string $constantSearchedName Name of the constant
99
     *
100
     * @return bool
101
     */
102 12
    public function hasConstant($constantSearchedName)
103
    {
104 12
        foreach ($this->getConstants() as $constantName => $constantValue) {
105 12
            if ($constantName === $constantSearchedName) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $constantName (integer) and $constantSearchedName (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
106 12
                return true;
107
            }
108 12
        }
109
110 12
        return false;
111
    }
112
113
    /**
114
     * Add a constant to an entity.
115
     *
116
     * @param ReflectionConstant $constant
117
     */
118 741
    public function addConstant(ReflectionConstant $constant)
119
    {
120 741
        $this->constants[$constant->getShortName()] = $constant;
121 741
        $constant->setDeclaringClassLike($this);
122 741
        $constant->setFilename($this->getFileName());
123
124 741
        return $this;
125
    }
126
}
127