ValidatedValue   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 3.85 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 3
loc 78
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getValue() 0 4 1
A setValue() 0 4 1
A __toString() 0 4 1
A validateLength() 3 6 2
A validateChoice() 0 6 2
A validatePattern() 0 6 2
validate() 0 1 ?

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 Bpost\BpostApiClient\Common;
4
5
use Bpost\BpostApiClient\Exception\BpostLogicException;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidPatternException;
8
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
9
10
/**
11
 * Class ValidatedValue
12
 */
13
abstract class ValidatedValue
14
{
15
    /** @var mixed */
16
    private $value;
17
18
    /**
19
     * ValidatedValue constructor.
20
     * @param mixed $value
21
     */
22 6
    public function __construct($value)
23
    {
24 6
        $this->setValue($value);
25 6
        $this->validate();
26 6
    }
27
28
    /**
29
     * @return mixed
30
     */
31 6
    public function getValue()
32
    {
33 6
        return $this->value;
34
    }
35
36
    /**
37
     * @param mixed $value
38
     */
39 6
    public function setValue($value)
40
    {
41 6
        $this->value = $value;
42 6
    }
43
44
    /**
45
     * @return string
46
     */
47 1
    public function __toString()
48
    {
49 1
        return (string)$this->getValue();
50
    }
51
52
    /**
53
     * @param int $length
54
     * @throws BpostInvalidLengthException
55
     */
56 1
    public function validateLength($length)
57
    {
58 1 View Code Duplication
        if (mb_strlen($this->getValue()) > $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...
59 1
            throw new BpostInvalidLengthException('', mb_strlen($this->getValue()), $length);
60
        }
61 1
    }
62
63
    /**
64
     * @param array $allowedValues
65
     * @throws BpostInvalidValueException
66
     */
67 3
    public function validateChoice(array $allowedValues)
68
    {
69 3
        if (!in_array($this->getValue(), $allowedValues)) {
70 2
            throw new BpostInvalidValueException('', $this->getValue(), $allowedValues);
71
        }
72 3
    }
73
74
    /**
75
     * @param string $regexPattern
76
     * @throws BpostInvalidPatternException
77
     */
78 1
    public function validatePattern($regexPattern)
79
    {
80 1
        if (!preg_match("/^$regexPattern\$/", $this->getValue())) {
81 1
            throw new BpostInvalidPatternException('', $this->getValue(), $regexPattern);
82
        }
83 1
    }
84
85
    /**
86
     * @throws BpostLogicException
87
     */
88
    public abstract function validate();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
89
90
}
91