Completed
Push — master ( 25a367...4e273e )
by Adrian
03:50
created

HasValidationRulesTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 0
cbo 2
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidationRules() 0 4 2
A setValidationRules() 0 6 1
B prepareValidator() 0 16 6
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 5
        $validator = $input->getValidator();
48 5
        foreach ($validationRules as $rule) {
49 5
            $params = is_array($rule) ? $rule : array(
50 5
                $rule
51
            );
52 5
            if (isset($params[0])) {
53 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...
54
            }
55
        }
56 5
    }
57
}
58