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

NumericEnsurance::isBetween()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 3
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Dgame\Ensurance;
4
5
defined('PHP_FLOAT_EPSILON') or define('PHP_FLOAT_EPSILON', 2.2204460492503e-16);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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
}