Completed
Push — 3.x ( 98bb5a...238d5c )
by Hari
10s
created

Filter::addRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura project for PHP.
5
 *
6
 * @package Aura.Input
7
 *
8
 * @license http://opensource.org/licenses/MIT-license.php MIT
9
 *
10
 */
11
namespace Aura\Input;
12
13
use Aura\Filter_Interface\FilterInterface;
14
use Aura\Filter_Interface\FailureCollectionInterface;
15
use Aura\Input\Filter\FailureCollection;
16
17
/**
18
 *
19
 * A filter
20
 *
21
 * @package Aura.Input
22
 *
23
 */
24
class Filter implements FilterInterface
25
{
26
    /**
27
     *
28
     * The array of rules to be applied to fields.
29
     *
30
     * @var array
31
     *
32
     */
33
    protected $rules = [];
34
35
    /**
36
     *
37
     * The array of failures to be used when rules fail.
38
     *
39
     * @var FailureCollection
40
     *
41
     */
42
    protected $failures;
43
44
    /**
45
     *
46
     * A prototype FailureCollection.
47
     *
48
     * @var FailureCollection
49
     *
50
     */
51
    protected $proto_failures;
52
53
    /**
54
     * Initialize filters
55
     */
56
    public function __construct(FailureCollectionInterface $failures = null)
57
    {
58
        if ($failures === null) {
59
            $failures = new FailureCollection();
60
        }
61
        $this->proto_failures = $failures;
0 ignored issues
show
Documentation Bug introduced by
It seems like $failures can also be of type object<Aura\Filter_Inter...ureCollectionInterface>. However, the property $proto_failures is declared as type object<Aura\Input\Filter\FailureCollection>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
62
        $this->init();
63
    }
64
65
    /**
66
     *
67
     * Does nothing
68
     *
69
     */
70
    protected function init()
71
    {
72
        # code...
73
    }
74
75
    /**
76
     *
77
     * Add multiple rules to a field.
78
     *
79
     * @param string $field The field name.
80
     *
81
     * @param string $message The message when the rule fails.
82
     *
83
     * @param Closure $closure A closure that implements the rule. It must
84
     * have the signature `function ($value, &$fields)`; it must return
85
     * boolean true on success, or boolean false on failure.
86
     *
87
     */
88
    public function addRule($field, $message, \Closure $closure)
89
    {
90
        $this->rules[$field][] = [$message, $closure];
91
    }
92
93
    /**
94
     *
95
     * Filter (sanitize and validate) the data.
96
     *
97
     * @param mixed $values The values to be filtered.
98
     *
99
     * @return bool True if all rules passed; false if one or more failed.
100
     *
101
     */
102
    public function apply(&$values)
103
    {
104
        $this->failures = clone $this->proto_failures;
105
106
        // go through each field rules
107
        foreach ($this->rules as $field => $rules) {
108
            foreach ($rules as $rule) {
109
                // get the message and closure
110
                list($message, $closure) = $rule;
111
112
                // apply the closure to the data and get back the result
113
                $passed = $closure($values->$field, $values);
114
115
                if (! $passed) {
116
                    $this->failures->addMessagesForField($field, $message);
117
                }
118
            }
119
        }
120
121
        // Is the failures empty or not
122
        return $this->failures->isEmpty() ? true : false;
123
    }
124
125
    /**
126
     *
127
     * Gets the messages for all fields
128
     *
129
     * @return FailureCollection
130
     *
131
     */
132
    public function getFailures()
133
    {
134
        return $this->failures;
135
    }
136
}
137