Completed
Push — master ( 02819f...5052b0 )
by Gabriel
03:56
created

HasAttributesTrait::removeClass()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 0
cts 10
cp 0
rs 9.7333
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 20
1
<?php
2
3
namespace Nip\Form\Utility;
4
5
/**
6
 * Trait HasAttributesTrait
7
 * @package Nip\Form\Utility
8
 */
9
trait HasAttributesTrait
10
{
11
    protected $_attribs;
12
13
    /**
14
     * @return mixed
15
     */
16
    public function getId()
17
    {
18
        return $this->getAttrib('id');
19
    }
20
21
    /**
22
     * @param $id
23
     * @return $this
24
     */
25
    public function setId($id)
26
    {
27
        $this->setAttrib('id', $id);
28
29
        return $this;
30
    }
31
32
    /**
33
     * @return null
34
     */
35 6
    public function getName()
36
    {
37 6
        return $this->getAttrib('name');
38
    }
39
40
41
    /**
42
     * @param $name
43
     * @return $this
44
     */
45 5
    public function setName($name)
46
    {
47 5
        $this->setAttrib('name', $name);
48
49 5
        return $this;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 5
    public function getLabel()
56
    {
57 5
        return $this->getAttrib('label');
58
    }
59
60
    /**
61
     * @param $label
62
     * @return $this
63
     */
64 5
    public function setLabel($label)
65
    {
66 5
        $this->setAttrib('label', $label);
67
68 5
        return $this;
69
    }
70
71
    /**
72
     * @param string $requester
73
     * @return null
74
     */
75 4
    public function getValue($requester = 'abstract')
0 ignored issues
show
Unused Code introduced by
The parameter $requester is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77 4
        return $this->getAttrib('value');
78
    }
79
80
    /**
81
     * @return bool
82
     */
83 5
    public function hasValue()
84
    {
85 5
        return !empty($this->getAttrib('value'));
86
    }
87
88
    /**
89
     * @param $value
90
     * @return static|self
0 ignored issues
show
Comprehensibility Bug introduced by
The return type HasAttributesTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
91
     */
92 4
    public function setValue($value)
93
    {
94 4
        $this->setAttrib('value', $value);
95
96 4
        return $this;
97
    }
98
99
    /**
100
     * @return $this
101
     */
102 3 View Code Duplication
    public function addClass()
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...
103
    {
104 3
        $classes = func_get_args();
105 3
        if (is_array($classes)) {
106 3
            $oldClasses = explode(' ', $this->getAttrib('class'));
107 3
            $classes = array_merge($classes, $oldClasses);
108 3
            $this->setAttrib('class', implode(' ', $classes));
109
        }
110
111 3
        return $this;
112
    }
113
114
    /**
115
     * @return $this
116
     */
117 View Code Duplication
    public function removeClass()
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...
118
    {
119
        $removeClasses = func_get_args();
120
        if (is_array($removeClasses)) {
121
            $classes = explode(' ', $this->getAttrib('class'));
122
            foreach ($removeClasses as $class) {
123
                $key = array_search($class, $classes);
124
                if ($key !== false) {
125
                    unset($classes[$key]);
126
                }
127
            }
128
            $this->setAttrib('class', implode(' ', $classes));
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param $key
136
     * @param $value
137
     * @return static
138
     */
139 6
    public function setAttrib($key, $value)
140
    {
141 6
        $key = (string)$key;
142 6
        $this->_attribs[$key] = $value;
143
144 6
        return $this;
145
    }
146
147
    /**
148
     * @param string $key
149
     * @return null
150
     */
151 8 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...
152
    {
153 8
        $key = (string)$key;
154 8
        if (!isset($this->_attribs[$key])) {
155 5
            return null;
156
        }
157
158 6
        return $this->_attribs[$key];
159
    }
160
161
    /**
162
     * @param string $key
163
     * @return bool
164
     */
165
    public function delAttrib($key)
166
    {
167
        $key = (string)$key;
168
        unset($this->_attribs[$key]);
169
170
        return true;
171
    }
172
173
    /**
174
     * @return mixed
175
     */
176 3
    public function getAttribs()
177
    {
178 3
        return $this->_attribs;
179
    }
180
181
    /**
182
     * @param  array $attribs
183
     * @return static
184
     */
185
    public function setAttribs(array $attribs)
186
    {
187
        $this->clearAttribs();
188
189
        return $this->addAttribs($attribs);
190
    }
191
192
    /**
193
     * @return static
194
     */
195
    public function clearAttribs()
196
    {
197
        $this->_attribs = [];
198
199
        return $this;
200
    }
201
202
    /**
203
     * @param  array $attribs
204
     * @return static
205
     */
206 3
    public function addAttribs(array $attribs)
207
    {
208 3
        foreach ($attribs as $key => $value) {
209
            $this->setAttrib($key, $value);
210
        }
211
212 3
        return $this;
213
    }
214
215
    /**
216
     * @param $key
217
     * @return bool
218
     */
219 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...
220
    {
221
        if (isset($this->_attribs[$key])) {
222
            unset($this->_attribs[$key]);
223
224
            return true;
225
        }
226
227
        return false;
228
    }
229
}
230