NumberValidator::setMin()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php declare(strict_types=1);
2
3
namespace indigerd\scenarios\validation\validator;
4
5
class NumberValidator extends AbstractValidator
6
{
7
    protected $pattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
8
9
    protected $message = 'Value must be a number';
10
11
    protected $min = null;
12
13
    protected $max = null;
14
15 View Code Duplication
    public function setMin($min) : self
0 ignored issues
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...
16
    {
17
        if (\intval($min) <= 0) {
18
            throw new \InvalidArgumentException('Invalid min param value');
19
        }
20
        $this->min = (int)$min;
21
        return $this;
22
    }
23
24 View Code Duplication
    public function setMax($max) : self
0 ignored issues
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...
25
    {
26
        if (\intval($max) <= 0) {
27
            throw new \InvalidArgumentException('Invalid max param value');
28
        }
29
        $this->max = (int)$max;
30
        return $this;
31
    }
32
33 3
    protected function normalizeNumber($value) : string
34
    {
35 3
        $value = (string)$value;
36 3
        $localeInfo = \localeconv();
37 3
        $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
38
39 3
        if ($decimalSeparator !== null && $decimalSeparator !== '.') {
40
            $value = \str_replace($decimalSeparator, '.', $value);
41
        }
42 3
        return $value;
43
    }
44
45 3
    public function validate($value, array $context = []): bool
46
    {
47 3 View Code Duplication
        if ($this->skipOnEmpty and ($value === null || \trim((string)$value) === '')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48
            return true;
49
        }
50 3
        if (!\preg_match($this->pattern, $this->normalizeNumber($value))) {
51 1
            return false;
52
        }
53 3 View Code Duplication
        if ($this->min !== null and $this->min > $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            $this->message = 'Value is too small';
55
            return false;
56
        }
57 3 View Code Duplication
        if ($this->max !== null and $this->max < $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
            $this->message = 'Value is too big';
59
            return false;
60
        }
61 3
        return true;
62
    }
63
}
64