Completed
Pull Request — master (#3)
by Randy
01:20
created

ReflectionEnsurance::uses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Ensurance;
4
5
use ReflectionClass;
6
use stdClass;
7
8
/**
9
 * Class ReflectionEnsurance
10
 * @package Dgame\Ensurance
11
 */
12
final class ReflectionEnsurance implements EnsuranceInterface
13
{
14
    use EnsuranceTrait;
15
16
    /**
17
     * @var ReflectionClass
18
     */
19
    private $reflection;
20
21
    /**
22
     * ReflectionEnsurance constructor.
23
     *
24
     * @param EnsuranceInterface $ensurance
25
     *
26
     * @throws \ReflectionException
27
     */
28
    public function __construct(EnsuranceInterface $ensurance)
29
    {
30
        $this->transferEnsurance($ensurance);
31
        if ($this->isEnsured()) {
32
            $this->reflection = new ReflectionClass($this->value);
33
        } else {
34
            $this->reflection = new ReflectionClass(new stdClass());
35
        }
36
    }
37
38
    /**
39
     * @param string $property
40
     *
41
     * @return ReflectionEnsurance
42
     */
43
    public function hasProperty(string $property): self
44
    {
45
        $this->ensure($this->reflection->hasProperty($property))
46
             ->orThrow('"%s" does not have a property "%s"', $this->value, $property);
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $method
53
     *
54
     * @return ReflectionEnsurance
55
     */
56
    public function hasMethod(string $method): self
57
    {
58
        $this->ensure($this->reflection->hasMethod($method))
59
             ->orThrow('"%s" does not have a method "%s"', $this->value, $method);
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $class
66
     *
67
     * @return ReflectionEnsurance
68
     */
69
    public function is(string $class): self
70
    {
71
        $this->ensure($this->value === $class)->orThrow('"%s" is not "%s"', $this->value, $class);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param string $class
78
     *
79
     * @return ReflectionEnsurance
80
     */
81
    public function isNot(string $class): self
82
    {
83
        $this->ensure($this->value !== $class)->orThrow('"%s" is "%s"', $this->value, $class);
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $class
90
     *
91
     * @return ReflectionEnsurance
92
     */
93
    public function extends(string $class): self
94
    {
95
        $this->ensure($this->reflection->isSubclassOf($class))->orThrow('"%s" did not extend "%s"', $this->value, $class);
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param string $class
102
     *
103
     * @return ReflectionEnsurance
104
     */
105
    public function extendsNot(string $class): self
106
    {
107
        $this->ensure(!$this->reflection->isSubclassOf($class))->orThrow('"%s" did extend "%s"', $this->value, $class);
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $interface
114
     *
115
     * @return ReflectionEnsurance
116
     */
117
    public function implements(string $interface): self
118
    {
119
        $this->ensure(array_key_exists($interface, class_implements($this->value, true)))
120
             ->orThrow('"%s" does not implements interface "%s"', $this->value, $interface);
121
122
        return $this;
123
    }
124
125
    /**
126
     * @param string $interface
127
     *
128
     * @return ReflectionEnsurance
129
     */
130
    public function implementsNot(string $interface): self
131
    {
132
        $this->ensure(!array_key_exists($interface, class_implements($this->value, true)))
133
             ->orThrow('"%s" does implements interface "%s"', $this->value, $interface);
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param string $class
140
     *
141
     * @return ReflectionEnsurance
142
     */
143
    public function isParentOf(string $class): self
144
    {
145
        $this->ensure(array_key_exists($this->value, class_parents($class, true)))
146
             ->orThrow('"%s" is not a parent of "%s"', $this->value, $class);
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param string $class
153
     *
154
     * @return ReflectionEnsurance
155
     */
156
    public function isNotParentOf(string $class): self
157
    {
158
        $this->ensure(!array_key_exists($this->value, class_parents($class, true)))
159
             ->orThrow('"%s" is a parent of "%s"', $this->value, $class);
160
161
        return $this;
162
    }
163
164
    /**
165
     * @param string $trait
166
     *
167
     * @return ReflectionEnsurance
168
     */
169
    public function uses(string $trait): self
170
    {
171
        $this->ensure(array_key_exists($trait, class_uses($this->value, true)))
172
             ->orThrow('"%s" does not use trait "%s"', $this->value, $trait);
173
174
        return $this;
175
    }
176
177
    /**
178
     * @param string $class
179
     *
180
     * @return ReflectionEnsurance
181
     */
182
    public function isSome(string $class): self
183
    {
184
        $this->ensure(is_a($this->value, $class))->orThrow('"%s" is not "%s"', get_class($this->value), $class);
185
186
        return $this;
187
    }
188
189
    /**
190
     * @param string $class
191
     *
192
     * @return ReflectionEnsurance
193
     */
194
    public function isNotSome(string $class): self
195
    {
196
        $this->ensure(!is_a($this->value, $class))->orThrow('"%s" is "%s"', get_class($this->value), $class);
197
198
        return $this;
199
    }
200
201
    /**
202
     * @param $class
203
     *
204
     * @return ReflectionEnsurance
205
     */
206
    public function isInstanceOf($class): self
207
    {
208
        $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...
209
        $this->ensure($this->value instanceof $class)->orThrow('"%s" is not an instance of "%s"', get_class($this->value), $class);
210
211
        return $this;
212
    }
213
214
    /**
215
     * @param $class
216
     *
217
     * @return ReflectionEnsurance
218
     */
219
    public function isNotInstanceOf($class): self
220
    {
221
        $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...
222
        $this->ensure(!($this->value instanceof $class))->orThrow('"%s" is an instance of "%s"', get_class($this->value), $class);
223
224
        return $this;
225
    }
226
}