Issues (138)

src/Traits/HasClassTrait.php (4 issues)

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 7
    public function addClass()
15
    {
16 7
        $classes = func_get_args();
17 7
        $classes = array_map('trim', $classes);
18 7
        if (is_array($classes)) {
0 ignored issues
show
The condition is_array($classes) is always true.
Loading history...
19 7
            $classes = implode(' ', $classes) . ' ' . $this->getAttrib('class');
0 ignored issues
show
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 7
            $newClasses = explode(' ', $classes);
21 7
            $newClasses = array_filter($newClasses);
22 7
            $newClasses = array_unique($newClasses);
23 7
            $this->setAttrib('class', trim(implode(' ', $newClasses)));
0 ignored issues
show
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 7
        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
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