Completed
Push — master ( c3a216...3ff263 )
by Randy
01:56
created

ClassEnsurance   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 186
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 14
loc 186
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getClass() 0 4 1
A getReflection() 0 4 1
A is() 0 6 1
A isNot() 0 6 1
A extends() 0 6 1
A extendsNot() 0 6 1
A implements() 0 8 1
A implementsNot() 0 7 1
A isParentOf() 7 7 1
A isNotParentOf() 7 7 1
A uses() 0 7 1
A hasProperty() 0 7 1
A hasMethod() 0 7 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 Dgame\Ensurance;
4
5
use Dgame\Ensurance\Exception\EnsuranceException;
6
use Dgame\Ensurance\Traits\EnforcementTrait;
7
use ReflectionClass;
8
9
/**
10
 * Class ClassEnsurance
11
 * @package Dgame\Ensurance
12
 */
13
class ClassEnsurance
14
{
15
    /**
16
     * @var string
17
     */
18
    private $class;
19
    /**
20
     * @var ReflectionClass
21
     */
22
    private $reflection;
23
24
    use EnforcementTrait;
25
26
    /**
27
     * ClassEnsurance constructor.
28
     *
29
     * @param string $class
30
     *
31
     * @throws EnsuranceException
32
     */
33
    public function __construct(string $class)
34
    {
35
        if (!class_exists($class)) {
36
            throw new EnsuranceException('"%s" is not a class', $class);
37
        }
38
39
        $this->class      = $class;
40
        $this->reflection = new ReflectionClass($class);
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    final protected function getClass(): string
47
    {
48
        return $this->class;
49
    }
50
51
    /**
52
     * @return ReflectionClass
53
     */
54
    final protected function getReflection(): ReflectionClass
55
    {
56
        return $this->reflection;
57
    }
58
59
    /**
60
     * @param string $class
61
     *
62
     * @return ClassEnsurance
63
     */
64
    public function is(string $class): ClassEnsurance
65
    {
66
        $this->enforce($this->class === $class)->orThrow('"%s" is not "%s"', $this->class, $class);
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $class
73
     *
74
     * @return ClassEnsurance
75
     */
76
    public function isNot(string $class): ClassEnsurance
77
    {
78
        $this->enforce($this->class !== $class)->orThrow('"%s" is "%s"', $this->class, $class);
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param string $class
85
     *
86
     * @return ClassEnsurance
87
     */
88
    public function extends (string $class): ClassEnsurance
89
    {
90
        $this->enforce($this->reflection->isSubclassOf($class))->orThrow('"%s" did not extend "%s"', $this->class, $class);
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $class
97
     *
98
     * @return ClassEnsurance
99
     */
100
    public function extendsNot(string $class): ClassEnsurance
101
    {
102
        $this->enforce(!$this->reflection->isSubclassOf($class))->orThrow('"%s" did extend "%s"', $this->class, $class);
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $interface
109
     *
110
     * @return ClassEnsurance
111
     */
112
    public function implements (string $interface): ClassEnsurance
113
    {
114
115
        $this->enforce($this->reflection->implementsInterface($interface))
116
             ->orThrow('"%s" does not implements interface "%s"', $this->class, $interface);
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param string $interface
123
     *
124
     * @return ClassEnsurance
125
     */
126
    public function implementsNot(string $interface): ClassEnsurance
127
    {
128
        $this->enforce(!$this->reflection->implementsInterface($interface))
129
             ->orThrow('"%s" does implements interface "%s"', $this->class, $interface);
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param string $class
136
     *
137
     * @return ClassEnsurance
138
     */
139 View Code Duplication
    public function isParentOf(string $class): ClassEnsurance
140
    {
141
        $this->enforce(array_key_exists($this->class, class_parents($class, true)))
142
             ->orThrow('"%s" is not a parent of "%s"', $this->class, $class);
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param string $class
149
     *
150
     * @return ClassEnsurance
151
     */
152 View Code Duplication
    public function isNotParentOf(string $class): ClassEnsurance
153
    {
154
        $this->enforce(!array_key_exists($this->class, class_parents($class, true)))
155
             ->orThrow('"%s" is a parent of "%s"', $this->class, $class);
156
157
        return $this;
158
    }
159
160
    /**
161
     * @param string $trait
162
     *
163
     * @return ClassEnsurance
164
     */
165
    public function uses(string $trait): ClassEnsurance
166
    {
167
        $this->enforce(array_key_exists($trait, $this->reflection->getTraitNames()))
168
             ->orThrow('"%s" does not use trait "%s"', $this->class, $trait);
169
170
        return $this;
171
    }
172
173
    /**
174
     * @param string $property
175
     *
176
     * @return ClassEnsurance
177
     */
178
    public function hasProperty(string $property): ClassEnsurance
179
    {
180
        $this->enforce($this->reflection->hasProperty($property))
181
             ->orThrow('"%s" does not have a property "%s"', $this->class, $property);
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param string $method
188
     *
189
     * @return ClassEnsurance
190
     */
191
    public function hasMethod(string $method): ClassEnsurance
192
    {
193
        $this->enforce($this->reflection->hasMethod($method))
194
             ->orThrow('"%s" does not have a method "%s"', $this->class, $method);
195
196
        return $this;
197
    }
198
}