HasFiltersTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 9
c 5
b 1
f 0
lcom 0
cbo 2
dl 0
loc 52
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 4 2
A setFilters() 0 6 1
B prepareFiltrator() 0 16 6
1
<?php
2
3
namespace Sirius\Input\Traits;
4
5
use Sirius\Input\Specs;
6
use Sirius\Input\InputFilter;
7
8
trait HasFiltersTrait
9
{
10
11
    /**
12
     * Get data filters for the element
13
     *
14
     * @return array
15
     */
16 11
    public function getFilters()
17
    {
18 11
        return isset($this[Specs::FILTERS]) ? $this[Specs::FILTERS] : array();
19
    }
20
21
    /**
22
     * Sets data filter for the element
23
     *
24
     * @param array $filters
25
     *
26
     * @return $this
27
     */
28 3
    public function setFilters($filters = array())
29
    {
30 3
        $this[Specs::FILTERS] = $filters;
31
32 3
        return $this;
33
    }
34
35
    /**
36
     * Attached the element's data filters to the filtrator object
37
     *
38
     * @param InputFilter $input
39
     *
40
     * @throws \InvalidArgumentException
41
     */
42 9
    protected function prepareFiltrator(InputFilter $input)
43
    {
44 9
        $filters = $this->getFilters();
45 9
        if (!$filters || !is_array($filters)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
46 7
            return;
47
        }
48
49 2
        $filtrator = $input->getFiltrator();
50
51 2
        foreach ($filters as $filter) {
52 2
            $params = is_array($filter) ? $filter : array($filter);
53 2
            if (isset($params[0])) {
54 2
                $filtrator->add($this->getName(), $params[0], @$params[1], @$params[2], @$params[3]);
0 ignored issues
show
Bug introduced by
It seems like getName() 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...
55 2
            }
56 2
        }
57 2
    }
58
59
}
60