StringValidator::setLength()   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 8
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 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