ReflectionEnsurance   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 243
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 1

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getReflection() 0 12 3
A getEmptyReflection() 0 4 1
A hasProperty() 0 7 1
A hasMethod() 0 7 1
A is() 0 6 1
A isNot() 0 6 1
A extends() 0 6 1
A extendsNot() 0 6 1
A implements() 0 7 1
A implementsNot() 0 7 1
A isParentOf() 0 7 1
A isNotParentOf() 0 7 1
A uses() 0 7 1
A isSome() 0 6 1
A isNotSome() 0 6 1
A isInstanceOf() 0 7 2
A isNotInstanceOf() 0 7 2
1
<?php
2
3
namespace Dgame\Ensurance;
4
5
use ReflectionClass;
6
use ReflectionException;
7
use ReflectionObject;
8
use stdClass;
9
10
/**
11
 * Class ReflectionEnsurance
12
 * @package Dgame\Ensurance
13
 */
14
final class ReflectionEnsurance implements EnsuranceInterface
15
{
16
    use EnsuranceTrait;
17
18
    /**
19
     * @var ReflectionClass
20
     */
21
    private $reflection;
22
23
    /**
24
     * ReflectionEnsurance constructor.
25
     *
26
     * @param EnsuranceInterface $ensurance
27
     *
28
     * @throws ReflectionException
29
     */
30
    public function __construct(EnsuranceInterface $ensurance)
31
    {
32
        $this->transferEnsurance($ensurance);
33
        if ($this->isEnsured()) {
34
            $this->reflection = self::getReflection($this->value);
35
        } else {
36
            $this->reflection = self::getEmptyReflection();
37
        }
38
    }
39
40
    /**
41
     * @param mixed $value
42
     *
43
     * @return ReflectionClass
44
     * @throws ReflectionException
45
     */
46
    private static function getReflection($value): ReflectionClass
47
    {
48
        if (is_object($value)) {
49
            return new ReflectionObject($value);
50
        }
51
52
        if (is_string($value)) {
53
            return new ReflectionClass($value);
54
        }
55
56
        return self::getEmptyReflection();
57
    }
58
59
    /**
60
     * @return ReflectionClass
61
     * @throws ReflectionException
62
     */
63
    private static function getEmptyReflection(): ReflectionClass
64
    {
65
        return new ReflectionClass(new stdClass());
66
    }
67
68
    /**
69
     * @param string $property
70
     *
71
     * @return ReflectionEnsurance
72
     */
73
    public function hasProperty(string $property): self
74
    {
75
        $this->ensure($this->reflection->hasProperty($property))
76
             ->orThrow('"%s" does not have a property "%s"', $this->value, $property);
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $method
83
     *
84
     * @return ReflectionEnsurance
85
     */
86
    public function hasMethod(string $method): self
87
    {
88
        $this->ensure($this->reflection->hasMethod($method))
89
             ->orThrow('"%s" does not have a method "%s"', $this->value, $method);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $class
96
     *
97
     * @return ReflectionEnsurance
98
     */
99
    public function is(string $class): self
100
    {
101
        $this->ensure($this->value === $class)->orThrow('"%s" is not "%s"', $this->value, $class);
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param string $class
108
     *
109
     * @return ReflectionEnsurance
110
     */
111
    public function isNot(string $class): self
112
    {
113
        $this->ensure($this->value !== $class)->orThrow('"%s" is "%s"', $this->value, $class);
114
115
        return $this;
116
    }
117
118
    /**
119
     * @param string $class
120
     *
121
     * @return ReflectionEnsurance
122
     */
123
    public function extends(string $class): self
124
    {
125
        $this->ensure($this->reflection->isSubclassOf($class))->orThrow('"%s" did not extend "%s"', $this->value, $class);
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $class
132
     *
133
     * @return ReflectionEnsurance
134
     */
135
    public function extendsNot(string $class): self
136
    {
137
        $this->ensure(!$this->reflection->isSubclassOf($class))->orThrow('"%s" did extend "%s"', $this->value, $class);
138
139
        return $this;
140
    }
141
142
    /**
143
     * @param string $interface
144
     *
145
     * @return ReflectionEnsurance
146
     */
147
    public function implements(string $interface): self
148
    {
149
        $this->ensure(array_key_exists($interface, class_implements($this->value, true)))
150
             ->orThrow('"%s" does not implements interface "%s"', $this->value, $interface);
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param string $interface
157
     *
158
     * @return ReflectionEnsurance
159
     */
160
    public function implementsNot(string $interface): self
161
    {
162
        $this->ensure(!array_key_exists($interface, class_implements($this->value, true)))
163
             ->orThrow('"%s" does implements interface "%s"', $this->value, $interface);
164
165
        return $this;
166
    }
167
168
    /**
169
     * @param string $class
170
     *
171
     * @return ReflectionEnsurance
172
     */
173
    public function isParentOf(string $class): self
174
    {
175
        $this->ensure(array_key_exists($this->value, class_parents($class, true)))
176
             ->orThrow('"%s" is not a parent of "%s"', $this->value, $class);
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param string $class
183
     *
184
     * @return ReflectionEnsurance
185
     */
186
    public function isNotParentOf(string $class): self
187
    {
188
        $this->ensure(!array_key_exists($this->value, class_parents($class, true)))
189
             ->orThrow('"%s" is a parent of "%s"', $this->value, $class);
190
191
        return $this;
192
    }
193
194
    /**
195
     * @param string $trait
196
     *
197
     * @return ReflectionEnsurance
198
     */
199
    public function uses(string $trait): self
200
    {
201
        $this->ensure(array_key_exists($trait, class_uses($this->value, true)))
202
             ->orThrow('"%s" does not use trait "%s"', $this->value, $trait);
203
204
        return $this;
205
    }
206
207
    /**
208
     * @param string $class
209
     *
210
     * @return ReflectionEnsurance
211
     */
212
    public function isSome(string $class): self
213
    {
214
        $this->ensure(is_a($this->value, $class))->orThrow('"%s" is not "%s"', get_class($this->value), $class);
215
216
        return $this;
217
    }
218
219
    /**
220
     * @param string $class
221
     *
222
     * @return ReflectionEnsurance
223
     */
224
    public function isNotSome(string $class): self
225
    {
226
        $this->ensure(!is_a($this->value, $class))->orThrow('"%s" is "%s"', get_class($this->value), $class);
227
228
        return $this;
229
    }
230
231
    /**
232
     * @param mixed $class
233
     *
234
     * @return ReflectionEnsurance
235
     */
236
    public function isInstanceOf($class): self
237
    {
238
        $class = is_object($class) ? get_class($class) : $class;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $class. This often makes code more readable.
Loading history...
239
        $this->ensure($this->value instanceof $class)->orThrow('"%s" is not an instance of "%s"', get_class($this->value), $class);
240
241
        return $this;
242
    }
243
244
    /**
245
     * @param mixed $class
246
     *
247
     * @return ReflectionEnsurance
248
     */
249
    public function isNotInstanceOf($class): self
250
    {
251
        $class = is_object($class) ? get_class($class) : $class;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $class. This often makes code more readable.
Loading history...
252
        $this->ensure(!($this->value instanceof $class))->orThrow('"%s" is an instance of "%s"', get_class($this->value), $class);
253
254
        return $this;
255
    }
256
}
257