Passed
Push — master ( d318b7...9b70cb )
by Christopher
01:39
created

LengthTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 111
Duplicated Lines 37.84 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 0
dl 42
loc 111
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setLengthFacet() 0 5 1
A setMinLengthFacet() 0 5 1
A checkValidMinMaxLength() 0 9 3
A setMaxLengthFacet() 0 5 1
A checkMaxLength() 0 12 4
A checkMaxLengthArray() 11 11 2
A checkMaxLengthString() 10 10 2
A checkMinLength() 0 10 3
A checkMinLengthArray() 11 11 2
A checkMinLengthString() 10 10 2

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
2
3
namespace AlgoWeb\xsdTypes\Facets;
4
5
trait LengthTrait
6
{
7
    /**
8
     * @Exclude
9
     * @var integer Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
10
     */
11
    private $maxLength = null;
12
    /**
13
     * @Exclude
14
     * @var integer Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
15
     */
16
    private $minLength = null;
17
18
    protected function setLengthFacet($value)
19
    {
20
        $this->setMinLengthFacet($value);
21
        $this->setMaxLengthFacet($value);
22
    }
23
24
    protected function setMinLengthFacet($value)
25
    {
26
        $this->checkValidMinMaxLength($value);
27
        $this->minLength = $value;
28
    }
29
30
    private function checkValidMinMaxLength($value, $min = 0)
31
    {
32
        if (((int)$value) != $value) {
33
            throw new \InvalidArgumentException("length values MUST be castable to int " . __CLASS__);
34
        }
35
        if ($min >= $value) {
36
            throw new \InvalidArgumentException("length values MUST be greater then 0 " . __CLASS__);
37
        }
38
    }
39
40
    protected function setMaxLengthFacet($value)
41
    {
42
        $this->checkValidMinMaxLength($value);
43
        $this->maxLength = $value;
44
    }
45
46
    private function checkMaxLength($v)
47
    {
48
        if ($this->maxLength != null) {
49
            if ($this->minLength != null) {
50
                if (is_array($v)) {
51
                    $this->checkMaxLengthArray($v);
52
                } else {
53
                    $this->checkMaxLengthString($v);
54
                }
55
            }
56
        }
57
    }
58
59 View Code Duplication
    private function checkMaxLengthArray($v)
60
    {
61
        assert(is_array($v));
62
        $arrayLen = count($v);
63
        if ($arrayLen < $this->maxLength) {
64
            throw new \InvalidArgumentException(
65
                "the provided value for " . __CLASS__ . " is to short MaxLength: "
66
                . $this->maxLength
67
            );
68
        }
69
    }
70
71 View Code Duplication
    private function checkMaxLengthString($v)
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...
72
    {
73
        $stringLen = strlen($v);
74
        if ($stringLen < $this->maxLength) {
75
            throw new \InvalidArgumentException(
76
                "the provided value for " . __CLASS__ . " is to short MaxLength: "
77
                . $this->maxLength
78
            );
79
        }
80
    }
81
82
    private function checkMinLength($v)
83
    {
84
        if ($this->minLength != null) {
85
            if (is_array($v)) {
86
                $this->checkMinLengthArray($v);
87
            } else {
88
                $this->checkMinLengthString($v);
89
            }
90
        }
91
    }
92
93 View Code Duplication
    private function checkMinLengthArray($v)
94
    {
95
        assert(is_array($v));
96
        $arrayLen = count($v);
97
        if ($arrayLen > $this->minLength) {
98
            throw new \InvalidArgumentException(
99
                "the provided value for " . __CLASS__ . " is to long MinLength: "
100
                . $this->minLength
101
            );
102
        }
103
    }
104
105 View Code Duplication
    private function checkMinLengthString($v)
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...
106
    {
107
        $stringLen = strlen($v);
108
        if ($stringLen > $this->minLength) {
109
            throw new \InvalidArgumentException(
110
                "the provided value for " . __CLASS__ . " is to long MinLength: "
111
                . $this->minLength
112
            );
113
        }
114
    }
115
}
116