StringValidator::validate()   B
last analyzed

Complexity

Conditions 11
Paths 6

Size

Total Lines 23

Duplication

Lines 15
Ratio 65.22 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 15
loc 23
c 0
b 0
f 0
ccs 0
cts 23
cp 0
rs 7.3166
cc 11
nc 6
nop 2
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace indigerd\scenarios\validation\validator;
4
5
class StringValidator extends AbstractValidator
6
{
7
    protected $min = null;
8
9
    protected $max = null;
10
11
    protected $length = null;
12
13
    protected $encoding = 'UTF-8';
14
15
    protected $message = 'Value must be a string';
16
17 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...
18
    {
19
        if (\intval($min) <= 0) {
20
            throw new \InvalidArgumentException('Invalid min param value');
21
        }
22
        $this->min = (int)$min;
23
        return $this;
24
    }
25
26 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...
27
    {
28
        if (\intval($max) <= 0) {
29
            throw new \InvalidArgumentException('Invalid max param value');
30
        }
31
        $this->max = (int)$max;
32
        return $this;
33
    }
34
35 View Code Duplication
    public function setLength($length) : 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...
36
    {
37
        if (\intval($length) <= 0) {
38
            throw new \InvalidArgumentException('Invalid length param value');
39
        }
40
        $this->length = (int)$length;
41
        return $this;
42
    }
43
44
    public function setEncoding($encoding)
45
    {
46
        $this->encoding = $encoding;
47
        return $this;
48
    }
49
50
    public function validate($value, array $context = []): bool
51
    {
52 View Code Duplication
        if ($this->skipOnEmpty and ($value === null || \trim($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...
53
            return true;
54
        }
55
        if (!\is_string($value)) {
56
            return false;
57
        }
58
        $length = \mb_strlen($value, $this->encoding);
59 View Code Duplication
        if ($this->length !== null and $this->length != $length) {
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...
60
            $this->message = 'Invalid string length';
61
            return false;
62
        }
63 View Code Duplication
        if ($this->min !== null and $this->min > $length) {
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...
64
            $this->message = 'String length is too short';
65
            return false;
66
        }
67 View Code Duplication
        if ($this->max !== null and $this->max < $length) {
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...
68
            $this->message = 'String length is too long';
69
            return false;
70
        }
71
        return true;
72
    }
73
}
74