BasicAttribute   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 2.73 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 3
loc 110
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getValue() 0 4 1
A setKey() 0 4 2
A getKey() 0 4 1
A __toString() 0 4 1
A getPrefixedTagName() 0 7 2
A validateLength() 3 6 2
A validateChoice() 0 6 2
A validatePattern() 0 6 2
getDefaultKey() 0 1 ?
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
abstract class BasicAttribute
11
{
12
    /** @var mixed */
13
    private $value;
14
15
    /** @var string */
16
    private $key;
17
18
    /**
19
     * BasicAttribute constructor.
20
     * @param mixed  $value
21
     * @param string $key
22
     */
23 13
    public function __construct($value, $key = '')
24
    {
25 13
        $this->value = $value;
26 13
        $this->setKey($key);
27 13
        $this->validate();
28 13
    }
29
30
    /**
31
     * @return mixed
32
     */
33 13
    public function getValue()
34
    {
35 13
        return $this->value;
36
    }
37
38
    /**
39
     * @param string $key
40
     */
41 13
    private function setKey($key)
42
    {
43 13
        $this->key = (string)($key ?: $this->getDefaultKey());
44 13
    }
45
46
    /**
47
     * @return string
48
     */
49 8
    public function getKey()
50
    {
51 8
        return $this->key;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function __toString()
58
    {
59 1
        return (string)$this->getValue();
60
    }
61
62
    /**
63
     * Prefix $tagName with the $prefix, if needed
64
     * @param string $prefix
0 ignored issues
show
Documentation introduced by
Should the type for parameter $prefix not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     * @param string $tagName
66
     * @return string
67
     */
68
    public function getPrefixedTagName($tagName, $prefix = null)
69
    {
70
        if (empty($prefix)) {
71
            return $tagName;
72
        }
73
        return $prefix . ':' . $tagName;
74
    }
75
76
    /**
77
     * @param int $length
78
     * @throws BpostInvalidLengthException
79
     */
80 8
    public function validateLength($length)
81
    {
82 8 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...
83 4
            throw new BpostInvalidLengthException($this->getKey(), mb_strlen($this->getValue()), $length);
84
        }
85 8
    }
86
87
    /**
88
     * @param array $allowedValues
89
     * @throws BpostInvalidValueException
90
     */
91 5
    public function validateChoice(array $allowedValues)
92
    {
93 5
        if (!in_array($this->getValue(), $allowedValues)) {
94 2
            throw new BpostInvalidValueException($this->getKey(), $this->getValue(), $allowedValues);
95
        }
96 5
    }
97
98
    /**
99
     * @param string $regexPattern
100
     * @throws BpostInvalidPatternException
101
     */
102 5
    public function validatePattern($regexPattern)
103
    {
104 5
        if (!preg_match("/^$regexPattern\$/", $this->getValue())) {
105 2
            throw new BpostInvalidPatternException($this->getKey(), $this->getValue(), $regexPattern);
106
        }
107 5
    }
108
109
    /**
110
     * @return string
111
     */
112
    protected abstract function getDefaultKey();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
113
114
    /**
115
     * @throws BpostLogicException
116
     */
117
    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...
118
119
}
120