Test Setup Failed
Push — master ( 4c1de8...3a35d5 )
by Gabriel
02:10
created

HasAttributesTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 21.35 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 34.62%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 19
loc 89
ccs 9
cts 26
cp 0.3462
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttribs() 0 4 1
A setAttribs() 0 6 1
A clearAttribs() 0 6 1
A addAttribs() 0 8 2
A removeAttrib() 10 10 2
A setAttrib() 0 7 1
A getAttrib() 9 9 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 Nip\Form\Traits;
4
5
/**
6
 * Trait HasAttributesTrait
7
 * @package Nip\Form\Traits
8
 */
9
trait HasAttributesTrait
10
{
11
    protected $_attribs = [];
12
13
14
    /**
15
     * @param $key
16
     * @param $value
17
     * @return $this
18
     */
19 5
    public function setAttrib($key, $value)
20
    {
21 5
        $key = (string)$key;
22 5
        $this->_attribs[$key] = $value;
23
24 5
        return $this;
25
    }
26
27
    /**
28
     * @param string $key
29
     * @return string
30
     */
31 6 View Code Duplication
    public function getAttrib($key)
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...
32
    {
33 6
        $key = (string)$key;
34 6
        if (!isset($this->_attribs[$key])) {
35 6
            return null;
36
        }
37
38 5
        return $this->_attribs[$key];
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getAttribs()
45
    {
46
        return $this->_attribs;
47
    }
48
49
    /**
50
     * @param  array $attribs
51
     * @return $this
52
     */
53
    public function setAttribs(array $attribs)
54
    {
55
        $this->clearAttribs();
56
57
        return $this->addAttribs($attribs);
58
    }
59
60
    /**
61
     * @return $this
62
     */
63
    public function clearAttribs()
64
    {
65
        $this->_attribs = [];
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param  array $attribs
72
     * @return $this
73
     */
74
    public function addAttribs(array $attribs)
75
    {
76
        foreach ($attribs as $key => $value) {
77
            $this->setAttrib($key, $value);
78
        }
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param $key
85
     * @return bool
86
     */
87 View Code Duplication
    public function removeAttrib($key)
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...
88
    {
89
        if (isset($this->_attribs[$key])) {
90
            unset($this->_attribs[$key]);
91
92
            return true;
93
        }
94
95
        return false;
96
    }
97
}
98