StringValidator   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 69
Duplicated Lines 56.52 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 39
loc 69
c 0
b 0
f 0
ccs 0
cts 52
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setMin() 8 8 2
A setMax() 8 8 2
A setLength() 8 8 2
A setEncoding() 0 5 1
B validate() 15 23 11

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 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