HasValidationRulesTrait::prepareValidator()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
crap 5
1
<?php
2
namespace Sirius\Input\Traits;
3
4
use Sirius\Input\Specs;
5
use Sirius\Input\InputFilter;
6
7
trait HasValidationRulesTrait
8
{
9
10
    /**
11
     * Get the element value validation rules
12
     *
13
     * @return array
14
     */
15 9
    public function getValidationRules()
16
    {
17 9
        return isset($this[Specs::VALIDATION_RULES]) ? $this[Specs::VALIDATION_RULES] : array();
18
    }
19
20
    /**
21
     * Set the element value validation rules
22
     *
23
     * @param array $rules
24
     *
25
     * @return $this
26
     */
27 5
    public function setValidationRules($rules = array())
28
    {
29 5
        $this[Specs::VALIDATION_RULES] = $rules;
30
31 5
        return $this;
32
    }
33
34
    /**
35
     * Adds the element's validation rules to the validator object
36
     *
37
     * @param InputFilter $input
38
     *
39
     * @throws \InvalidArgumentException
40
     */
41 9
    protected function prepareValidator(InputFilter $input)
42
    {
43 9
        $validationRules = $this->getValidationRules();
44 9
        if (!$validationRules || !is_array($validationRules)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $validationRules 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...
45 7
            return;
46
        }
47
48 5
        $validator = $input->getValidator();
49
50 5
        foreach ($validationRules as $rule) {
51 5
            $params = (array) $rule;
52
53 5
            if (isset($params[0])) {
54 5
                $validator->add($this->getName(), $params[0], @$params[1], @$params[2], $this->getLabel());
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...
Bug introduced by
It seems like getLabel() 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 5
            }
56 5
        }
57 5
    }
58
}
59