Passed
Push — master ( c553d3...935f6a )
by Gabriel
04:17 queued 13s
created

HasAttributesTrait::setDataAttrib()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 2
crap 1
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');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttrib('id') targeting Nip\Form\Utility\HasAttributesTrait::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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 13
    public function getName()
36
    {
37 13
        return $this->getAttrib('name');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttrib('name') targeting Nip\Form\Utility\HasAttributesTrait::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
    }
39
40
41
    /**
42
     * @param $name
43
     * @return $this
44
     */
45 12
    public function setName($name)
46
    {
47 12
        $this->setAttrib('name', $name);
48
49 12
        return $this;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 10
    public function getLabel()
56
    {
57 10
        return $this->getAttrib('label');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttrib('label') targeting Nip\Form\Utility\HasAttributesTrait::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
    }
59
60
    /**
61
     * @param $label
62
     * @return $this
63
     */
64 10
    public function setLabel($label)
65
    {
66 10
        $this->setAttrib('label', $label);
67
68 10
        return $this;
69
    }
70
71
    /**
72
     * @param string $requester
73
     * @return null
74
     */
75 5
    public function getValue($requester = 'abstract')
0 ignored issues
show
Unused Code introduced by
The parameter $requester is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

75
    public function getValue(/** @scrutinizer ignore-unused */ $requester = 'abstract')

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

Loading history...
76
    {
77 5
        return $this->getAttrib('value');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttrib('value') targeting Nip\Form\Utility\HasAttributesTrait::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
78
    }
79
80
    /**
81
     * @return bool
82
     */
83 5
    public function hasValue()
84
    {
85 5
        return !empty($this->getAttrib('value'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttrib('value') targeting Nip\Form\Utility\HasAttributesTrait::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
86
    }
87
88
    /**
89
     * @param $value
90
     * @return static|self
91
     */
92 5
    public function setValue($value)
93
    {
94 5
        $this->setAttrib('value', $value);
95
96 5
        return $this;
97
    }
98
99
    /**
100
     * @return $this
101
     */
102 5
    public function addClass()
103
    {
104 5
        $classes = func_get_args();
105 5
        if (is_array($classes)) {
0 ignored issues
show
introduced by
The condition is_array($classes) is always true.
Loading history...
106 5
            $oldClasses = explode(' ', $this->getAttrib('class'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttrib('class') targeting Nip\Form\Utility\HasAttributesTrait::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
$this->getAttrib('class') of type null is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
            $oldClasses = explode(' ', /** @scrutinizer ignore-type */ $this->getAttrib('class'));
Loading history...
107 5
            $classes = array_merge($classes, $oldClasses);
108 5
            $this->setAttrib('class', implode(' ', $classes));
109
        }
110
111 5
        return $this;
112
    }
113
114
    /**
115
     * @return $this
116
     */
117
    public function removeClass()
118
    {
119
        $removeClasses = func_get_args();
120
        if (is_array($removeClasses)) {
0 ignored issues
show
introduced by
The condition is_array($removeClasses) is always true.
Loading history...
121
            $classes = explode(' ', $this->getAttrib('class'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttrib('class') targeting Nip\Form\Utility\HasAttributesTrait::getAttrib() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
$this->getAttrib('class') of type null is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
            $classes = explode(' ', /** @scrutinizer ignore-type */ $this->getAttrib('class'));
Loading history...
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 14
    public function setDataAttrib($key, $value)
140
    {
141 14
        $key = 'data-' . (string)$key;
142 14
        $this->_attribs[$key] = $value;
143
144 14
        return $this;
145
    }
146
147
    /**
148
     * @param $key
149
     * @param $value
150
     * @return static
151 15
     */
152
    public function setAttrib($key, $value)
153 15
    {
154 15
        $key = (string)$key;
155 9
        $this->_attribs[$key] = $value;
156
157
        return $this;
158 12
    }
159
160
    /**
161
     * @param string $key
162
     * @return null
163
     */
164
    public function getAttrib($key)
165
    {
166
        $key = (string)$key;
167
        if (!isset($this->_attribs[$key])) {
168
            return null;
169
        }
170
171
        return $this->_attribs[$key];
172
    }
173
174
    /**
175
     * @param string $key
176 5
     * @return bool
177
     */
178 5
    public function delAttrib($key)
179
    {
180
        $key = (string)$key;
181
        unset($this->_attribs[$key]);
182
183
        return true;
184
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    public function getAttribs()
190
    {
191
        return $this->_attribs;
192
    }
193
194
    /**
195
     * @param array $attribs
196
     * @return static
197
     */
198
    public function setAttribs(array $attribs)
199
    {
200
        $this->clearAttribs();
201
202
        return $this->addAttribs($attribs);
203
    }
204
205
    /**
206 3
     * @return static
207
     */
208 3
    public function clearAttribs()
209
    {
210
        $this->_attribs = [];
211
212 3
        return $this;
213
    }
214
215
    /**
216
     * @param array $attribs
217
     * @return static
218
     */
219
    public function addAttribs(array $attribs)
220
    {
221
        foreach ($attribs as $key => $value) {
222
            $this->setAttrib($key, $value);
223
        }
224
225
        return $this;
226
    }
227
228
    /**
229
     * @param $key
230
     * @return bool
231
     */
232
    public function removeAttrib($key)
233
    {
234
        if (isset($this->_attribs[$key])) {
235
            unset($this->_attribs[$key]);
236
237
            return true;
238
        }
239
240
        return false;
241
    }
242
}
243