Completed
Push — master ( 4be8f9...c3a216 )
by Randy
01:42
created

ClassEnsurance::isNot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Ensurance;
4
5
use Dgame\Ensurance\Exception\EnsuranceException;
6
use Dgame\Ensurance\Traits\EnforcementTrait;
7
8
/**
9
 * Class ClassEnsurance
10
 * @package Dgame\Ensurance
11
 */
12
class ClassEnsurance
13
{
14
    /**
15
     * @var string
16
     */
17
    private $class;
18
19
    use EnforcementTrait;
20
21
    /**
22
     * ClassEnsurance constructor.
23
     *
24
     * @param string $class
25
     *
26
     * @throws EnsuranceException
27
     */
28
    public function __construct(string $class)
29
    {
30
        if (!class_exists($class)) {
31
            throw new EnsuranceException('"%s" is not a class', $class);
32
        }
33
34
        $this->class = $class;
35
    }
36
37
    /**
38
     * @param string $class
39
     *
40
     * @return ClassEnsurance
41
     */
42
    public function is(string $class): ClassEnsurance
43
    {
44
        $this->enforce($this->class === $class)->orThrow('"%s" is not "%s"', $this->class, $class);
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param string $class
51
     *
52
     * @return ClassEnsurance
53
     */
54
    public function isNot(string $class): ClassEnsurance
55
    {
56
        $this->enforce($this->class !== $class)->orThrow('"%s" is "%s"', $this->class, $class);
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $class
63
     *
64
     * @return ClassEnsurance
65
     */
66
    public function extends (string $class): ClassEnsurance
67
    {
68
        $this->enforce(is_subclass_of($this->class, $class))->orThrow('"%s" did not extend "%s"', $this->class, $class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
69
70
        return $this;
71
    }
72
73
    /**
74
     * @param string $class
75
     *
76
     * @return ClassEnsurance
77
     */
78
    public function extendsNot(string $class): ClassEnsurance
79
    {
80
        $this->enforce(!is_subclass_of($this->class, $class))->orThrow('"%s" did extend "%s"', $this->class, $class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $interface
87
     *
88
     * @return ClassEnsurance
89
     */
90 View Code Duplication
    public function implements (string $interface): ClassEnsurance
1 ignored issue
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...
91
    {
92
        $this->enforce(array_key_exists($interface, class_implements($this->class)))
93
             ->orThrow('"%s" does not implements interface "%s"', $this->class, $interface);
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param string $interface
100
     *
101
     * @return ClassEnsurance
102
     */
103 View Code Duplication
    public function implementsNot(string $interface): ClassEnsurance
1 ignored issue
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...
104
    {
105
        $this->enforce(!array_key_exists($interface, class_implements($this->class)))
106
             ->orThrow('"%s" does implements interface "%s"', $this->class, $interface);
107
108
        return $this;
109
    }
110
111
    /**
112
     * @param string $class
113
     *
114
     * @return ClassEnsurance
115
     */
116 View Code Duplication
    public function isParentOf(string $class): ClassEnsurance
1 ignored issue
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...
117
    {
118
        $this->enforce(array_key_exists($this->class, class_parents($class, true)))
119
             ->orThrow('"%s" is not a parent of "%s"', $this->class, $class);
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param string $class
126
     *
127
     * @return ClassEnsurance
128
     */
129 View Code Duplication
    public function isNotParentOf(string $class): ClassEnsurance
1 ignored issue
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...
130
    {
131
        $this->enforce(!array_key_exists($this->class, class_parents($class, true)))
132
             ->orThrow('"%s" is a parent of "%s"', $this->class, $class);
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param string $trait
139
     *
140
     * @return ClassEnsurance
141
     */
142 View Code Duplication
    public function uses(string $trait): ClassEnsurance
1 ignored issue
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...
143
    {
144
        $this->enforce(array_key_exists($trait, class_uses($this->class)))
145
             ->orThrow('"%s" does not use trait "%s"', $this->class, $trait);
146
147
        return $this;
148
    }
149
150
    /**
151
     * @param string $property
152
     *
153
     * @return ClassEnsurance
154
     */
155
    public function hasProperty(string $property): ClassEnsurance
156
    {
157
        $this->enforce(property_exists($this->class, $property))
158
             ->orThrow('"%s" does not have a property "%s"', $this->class, $property);
159
160
        return $this;
161
    }
162
163
    /**
164
     * @param string $method
165
     *
166
     * @return ClassEnsurance
167
     */
168
    public function hasMethod(string $method): ClassEnsurance
169
    {
170
        $this->enforce(method_exists($this->class, $method))
171
             ->orThrow('"%s" does not have a method "%s"', $this->class, $method);
172
173
        return $this;
174
    }
175
}