Passed
Pull Request — master (#21)
by Christopher
01:58
created

LengthTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 21.51 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 15
c 5
b 1
f 0
lcom 1
cbo 0
dl 20
loc 93
rs 10

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