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

HasClassTrait::hasClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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)) {
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24
        }
25
26 5
        return $this;
27
    }
28
29
    /**
30
     * @return $this
31
     */
32 1 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...
33
    {
34 1
        $removeClasses = func_get_args();
35 1
        $removeClasses = array_map('trim', $removeClasses);
36 1
        if (is_array($removeClasses)) {
37 1
            $classes = explode(' ', $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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));
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
        }
46
47 1
        return $this;
48
    }
49
50
    /**
51
     * @param string $class
52
     * @return bool
53
     */
54
    public function hasClass($class)
55
    {
56
        return in_array($class, explode(' ', $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
    }
58
}
59