NumberValidator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 59
Duplicated Lines 45.76 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 44.83%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 27
loc 59
c 0
b 0
f 0
ccs 13
cts 29
cp 0.4483
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setMin() 8 8 2
A setMax() 8 8 2
A normalizeNumber() 0 11 4
B validate() 11 18 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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