NumericEnsurance   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.72%

Importance

Changes 0
Metric Value
dl 0
loc 170
ccs 36
cts 43
cp 0.8372
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isInt() 0 6 1
A isFloat() 0 6 1
A isGreaterThan() 0 6 1
A isGreaterThanOrEqualTo() 0 6 1
A isLessThan() 0 6 1
A isLessThanOrEqualTo() 0 6 1
A isPositive() 0 4 1
A isNegative() 0 4 1
A isEven() 0 6 1
A isOdd() 0 6 1
A isEqualTo() 0 6 1
A isNotEqualTo() 0 6 1
A isBetween() 0 7 2
A isNotBetween() 0 7 2
1
<?php
2
3
namespace Dgame\Ensurance;
4
5
defined('PHP_FLOAT_EPSILON') or define('PHP_FLOAT_EPSILON', 2.2204460492503e-16);
6
7
/**
8
 * Class NumericEnsurance
9
 * @package Dgame\Ensurance
10
 */
11
final class NumericEnsurance implements EnsuranceInterface
12
{
13
    use EnsuranceTrait;
14
15
    /**
16
     * NumericEnsurance constructor.
17
     *
18
     * @param EnsuranceInterface $ensurance
19
     */
20
    public function __construct(EnsuranceInterface $ensurance)
21
    {
22
        $this->transferEnsurance($ensurance);
23
    }
24
25
    /**
26
     * @return NumericEnsurance
27
     */
28
    public function isInt(): self
29 12
    {
30
        $this->ensure(is_int($this->value))->orThrow('"%s" is not an int', $this->value);
31 12
32
        return $this;
33
    }
34
35 12
    /**
36 12
     * @return NumericEnsurance
37
     */
38
    public function isFloat(): self
39
    {
40
        $this->ensure(is_float($this->value))->orThrow('"%s" is not a float', $this->value);
41 1
42
        return $this;
43 1
    }
44
45 1
    /**
46
     * @param float $value
47
     *
48
     * @return NumericEnsurance
49
     */
50
    public function isGreaterThan(float $value): self
51 1
    {
52
        $this->ensure($this->value > $value)->orThrow('"%s" is not greater than "%s"', $this->value, $value);
53 1
54
        return $this;
55 1
    }
56
57
    /**
58
     * @param float $value
59
     *
60
     * @return NumericEnsurance
61
     */
62
    public function isGreaterThanOrEqualTo(float $value): self
63 1
    {
64
        $this->ensure($this->value >= $value)->orThrow('"%s" is not greater or equal than "%s"', $this->value, $value);
65 1
66
        return $this;
67 1
    }
68
69
    /**
70
     * @param float $value
71
     *
72
     * @return NumericEnsurance
73
     */
74
    public function isLessThan(float $value): self
75 1
    {
76
        $this->ensure($this->value < $value)->orThrow('"%s" is greater or equal to "%s"', $this->value, $value);
77 1
78
        return $this;
79 1
    }
80
81
    /**
82
     * @param float $value
83
     *
84
     * @return NumericEnsurance
85
     */
86
    public function isLessThanOrEqualTo(float $value): self
87 2
    {
88
        $this->ensure($this->value <= $value)->orThrow('"%s" is greater than "%s"', $this->value, $value);
89 2
90
        return $this;
91 2
    }
92
93
    /**
94
     * @return NumericEnsurance
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
95
     */
96
    public function isPositive(): self
97
    {
98
        return $this->isGreaterThanOrEqualTo(0);
99 1
    }
100
101 1
    /**
102
     * @return NumericEnsurance
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
103 1
     */
104
    public function isNegative(): self
105
    {
106
        return $this->isLessThan(0);
107
    }
108
109 1
    /**
110
     * @return NumericEnsurance
111 1
     */
112
    public function isEven(): self
113
    {
114
        $this->ensure(($this->value & 1) === 0)->orThrow('"%s is not even"', $this->value);
115
116
        return $this;
117 1
    }
118
119 1
    /**
120
     * @return NumericEnsurance
121
     */
122
    public function isOdd(): self
123
    {
124
        $this->ensure(($this->value & 1) === 1)->orThrow('"%s is not odd"', $this->value);
125 2
126
        return $this;
127 2
    }
128
129 2
    /**
130
     * @param float $value
131
     *
132
     * @return NumericEnsurance
133
     */
134
    public function isEqualTo(float $value): self
135 1
    {
136
        $this->ensure(abs($this->value - $value) < PHP_FLOAT_EPSILON)->orThrow('"%s" is not equal to "%s"', $this->value, $value);
137 1
138
        return $this;
139 1
    }
140
141
    /**
142
     * @param float $value
143
     *
144
     * @return NumericEnsurance
145
     */
146
    public function isNotEqualTo(float $value): self
147
    {
148
        $this->ensure(abs($this->value - $value) > PHP_FLOAT_EPSILON)->orThrow('"%s" is equal to "%s"', $this->value, $value);
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param float $lhs
155
     * @param float $rhs
156
     *
157
     * @return NumericEnsurance
158
     */
159
    public function isBetween(float $lhs, float $rhs): self
160
    {
161
        $this->ensure($lhs <= $this->value && $rhs >= $this->value)
162
             ->orThrow('"%s" is not between "%s" and "%s"', $this->value, $lhs, $rhs);
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param float $lhs
169
     * @param float $rhs
170
     *
171
     * @return NumericEnsurance
172 1
     */
173
    public function isNotBetween(float $lhs, float $rhs): self
174 1
    {
175 1
        $this->ensure($lhs > $this->value || $rhs < $this->value)
176
             ->orThrow('"%s" is between "%s" and "%s"', $this->value, $lhs, $rhs);
177 1
178
        return $this;
179
    }
180
}