IntBase   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 65
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A max() 0 5 1
B validation() 0 29 7
A between() 0 6 1
A min() 0 5 1
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