IntBase::max()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace InputGuard\Guards\Bases;
5
6
trait IntBase
7
{
8
    use Strict;
9
10
    /**
11
     * @var int
12
     */
13
    private $min = PHP_INT_MIN;
14
15
    /**
16
     * @var int
17
     */
18
    private $max = PHP_INT_MAX;
19
20 5
    public function between(int $min, int $max): self
21
    {
22 5
        $this->min = $min;
23 5
        $this->max = $max;
24
25 5
        return $this;
26
    }
27
28 12
    public function min(int $min): self
29
    {
30 12
        $this->min = $min;
31
32 12
        return $this;
33
    }
34
35 11
    public function max(int $max): self
36
    {
37 11
        $this->max = $max;
38
39 11
        return $this;
40
    }
41
42 28
    protected function validation($input, &$value): bool
43
    {
44 28
        if (\is_bool($input)) {
45 1
            return false;
46
        }
47
48 27
        if ($this->strict && \is_int($input) === false) {
49 1
            return false;
50
        }
51
52
        $options = [
53
            'options' => [
54 26
                'min_range' => PHP_INT_MIN,
55 26
                'max_range' => PHP_INT_MAX,
56
            ],
57
        ];
58
59 26
        $returned = filter_var($input, FILTER_VALIDATE_INT, $options);
60 26
        if ($returned === false) {
61 8
            return false;
62
        }
63
64 18
        if ($returned < $this->min || $returned > $this->max) {
65 4
            return false;
66
        }
67
68 15
        $value = $returned;
69
70 15
        return true;
71
    }
72
}
73