Passed
Push — master ( eb0fcb...47bdab )
by Gabriel
04:01 queued 10s
created

HasClassTrait::addClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 3
Metric Value
eloc 9
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.9666
c 3
b 0
f 3
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
/**
6
 * Trait HasClassTrait
7
 * @package Nip\Form\Traits
8
 */
9
trait HasClassTrait
10
{
11
    /**
12
     * @return $this
13
     */
14 5
    public function addClass()
15
    {
16 5
        $classes = func_get_args();
17 5
        $classes = array_map('trim', $classes);
18 5
        if (is_array($classes)) {
0 ignored issues
show
introduced by
The condition is_array($classes) is always true.
Loading history...
19 5
            $classes = implode(' ', $classes).' '.$this->getAttrib('class');
0 ignored issues
show
Bug introduced by
It seems like getAttrib() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

19
            $classes = implode(' ', $classes).' '.$this->/** @scrutinizer ignore-call */ getAttrib('class');
Loading history...
20 5
            $newClasses = explode(' ', $classes);
21 5
            $newClasses = array_filter($newClasses);
22 5
            $newClasses = array_unique($newClasses);
23 5
            $this->setAttrib('class', trim(implode(' ', $newClasses)));
0 ignored issues
show
Bug introduced by
It seems like setAttrib() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
            $this->/** @scrutinizer ignore-call */ 
24
                   setAttrib('class', trim(implode(' ', $newClasses)));
Loading history...
24
        }
25
26 5
        return $this;
27
    }
28
29
    /**
30
     * @return $this
31
     */
32 1
    public function removeClass()
33
    {
34 1
        $removeClasses = func_get_args();
35 1
        $removeClasses = array_map('trim', $removeClasses);
36 1
        if (is_array($removeClasses)) {
0 ignored issues
show
introduced by
The condition is_array($removeClasses) is always true.
Loading history...
37 1
            $classes = explode(' ', $this->getAttrib('class'));
38 1
            foreach ($removeClasses as $class) {
39 1
                $key = array_search($class, $classes);
40 1
                if ($key !== false) {
41 1
                    unset($classes[$key]);
42
                }
43
            }
44 1
            $this->setAttrib('class', implode(' ', $classes));
45
        }
46
47 1
        return $this;
48
    }
49
50
    /**
51
     * @param string $class
52
     * @return bool
53
     */
54 1
    public function hasClass($class)
55
    {
56 1
        return in_array($class, explode(' ', $this->getAttrib('class')));
57
    }
58
}
59