Completed
Push — master ( 88b006...c22031 )
by Renato
05:07
created

ValidatorResolver::validateRequiredIfAll()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.004

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 18
nop 3
dl 0
loc 34
ccs 22
cts 23
cp 0.9565
crap 7.004
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace NwLaravel\Validation;
4
5
use ReflectionClass;
6
use Illuminate\Validation\Validator;
7
use Illuminate\Support\Arr;
8
9
/**
10
 * Class ValidatorResolver
11
 *
12
 * @method bool validateCpf(string $attribute, mixed $value, array $parameters)
13
 * @method bool validateCnpj(string $attribute, mixed $value, array $parameters)
14
 */
15
class ValidatorResolver extends Validator
16
{
17
18
    protected $currentRule;
19
    
20
    /**
21
     * The validation rules that imply the field is required.
22
     *
23
     * @var array
24
     */
25
    protected $implicitRules = [
26
        'Required', 'Filled', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
27
        'RequiredIf', 'RequiredUnless', 'Accepted', 'Present', 'RequiredIfAll', 'RequiredUnlessAll',
28
    ];
29
30
    /**
31
     * Validate Pattern Valid
32
     *
33
     * @param string $attribute  String Attribute
34
     * @param string $value      String Value
35
     * @param array  $parameters Array Parameters
36
     *
37
     * @return boolean
38
     */
39 1
    public function validatePattern($attribute, $value, $parameters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41 1
        return (bool) (@preg_match($value, "subject") !== false);
42
    }
43
44
    /**
45
     * Validate Current Password
46
     *
47
     * @param string $attribute  String Attribute
48
     * @param mixed  $value      Mixed Value
49
     * @param array  $parameters Array Parameters
50
     *
51
     * @return bool
52
     */
53 2
    public function validateCurrentPassword($attribute, $value, $parameters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55 2
        $guard = isset($parameters[0]) ? $parameters[0] : null;
56 2
        $field = isset($parameters[1]) ? $parameters[1] : 'password';
57 2
        return password_verify($value, auth($guard)->user()->{$field});
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
58
    }
59
60
    /**
61
     * Validate that an attribute is cpf valid
62
     *
63
     * @param string $attribute String Attribute
64
     * @param mixed  $value     Mixed Value
65
     * @param array  $parameters Array Parameters
66
     *
67
     * @return bool
68
     */
69 1
    public function validateDocument($attribute, $value, $parameters = array())
70
    {
71 1
        $value = preg_replace('/[^0-9]/', '', $value);
72 1
        if (strlen($value) == 11) {
73 1
            return $this->validateCpf($attribute, $value, $parameters);
74
        }
75
76 1
        return $this->validateCnpj($attribute, $value, $parameters);
77
    }
78
79
    /**
80
     * Validate currency
81
     *
82
     * @param string $attribute String Attribute
83
     * @param mixed  $value     Mixed Value
84
     * @param array  $parameters Array Parameters
85
     *
86
     * @return bool
87
     */
88 40
    public function validateCurrency($attribute, $value, $parameters = array())
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90 40
        return !is_null(asCurrency($value));
91
    }
92
93
    /**
94
     * Validate the not existence of an attribute value in a database table.
95
     *
96
     * @param  string  $attribute
97
     * @param  mixed   $value
98
     * @param  array   $parameters
99
     * @return bool
100
     */
101 2
    public function validateNotExists($attribute, $value, $parameters)
102
    {
103 2
        return !$this->validateExists($attribute, $value, $parameters);
104
    }
105
106
   /**
107
     * Validate that an attribute exists when another attribute has a given value.
108
     *
109
     * @param  string  $attribute
110
     * @param  mixed   $value
111
     * @param  mixed   $parameters
112
     * @return bool
113
     */
114
    public function validateRequiredIf($attribute, $value, $parameters)
115
    {
116
        $this->requireParameterCount(2, $parameters, 'required_if');
117
118
        $data = Arr::get($this->data, $parameters[0]);
119
120
        $values = array_slice($parameters, 1);
121
122
        if (is_bool($data)) {
123
            array_walk($values, function (&$value) {
124
                if ($value === 'true') {
125
                    $value = true;
126
                } elseif ($value === 'false') {
127
                    $value = false;
128
                }
129
            });
130
        }
131
132
        if (in_array($data, $values)) {
133
            return $this->validateRequired($attribute, $value);
134
        }
135
136
        $this->mergeRules($attribute, 'nullable');
137
138
        return true;
139
    }
140
141
    /**
142
     * Validate that an attribute exists when another attribute does not have a given value.
143
     *
144
     * @param  string  $attribute
145
     * @param  mixed  $value
146
     * @param  mixed  $parameters
147
     * @return bool
148
     */
149
    public function validateRequiredUnless($attribute, $value, $parameters)
150
    {
151
        $this->requireParameterCount(2, $parameters, 'required_unless');
152
153
        $data = Arr::get($this->data, $parameters[0]);
154
155
        $values = array_slice($parameters, 1);
156
157
        if (! in_array($data, $values)) {
158
            return $this->validateRequired($attribute, $value);
159
        }
160
161
        $this->mergeRules($attribute, 'nullable');
162
163
        return true;
164
    }
165
166
    /**
167
     * Validate that an attribute exists when another attribute has a given value.
168
     *
169
     * @param  string  $attribute
170
     * @param  mixed   $value
171
     * @param  mixed   $parameters
172
     * @return bool
173
     */
174 7
    public function validateRequiredIfAll($attribute, $value, $parameters)
175
    {
176 6
        $this->requireParameterCount(2, $parameters, 'required_if_all');
177
178 6
        $valid = true;
179 6
        $count = count($parameters);
180
181 7
        for ($i = 0; $i < $count; $i += 2) {
182 6
            $field = $parameters[$i];
183 6
            $fieldValue = $parameters[$i + 1];
184
185 6
            $data = $this->getValue($field);
186 6
            if (is_bool($data)) {
187 1
                if (strtolower($fieldValue) === 'true') {
188
                    $fieldValue = true;
189 1
                } elseif (strtolower($fieldValue) === 'false') {
190 1
                    $fieldValue = false;
191 1
                }
192 1
            }
193
            
194 6
            if ($fieldValue != $data) {
195 6
                $valid = false;
196 6
                break;
197
            }
198 6
        }
199
200 6
        if ($valid) {
201 1
            return $this->validateRequired($attribute, $value);
202
        }
203
204 6
        $this->mergeRules($attribute, 'nullable');
205
206 6
        return true;
207
    }
208
209
    /**
210
     * Validate that an attribute exists when another attribute does not have a given value.
211
     *
212
     * @param  string  $attribute
213
     * @param  mixed  $value
214
     * @param  mixed  $parameters
215
     * @return bool
216
     */
217 6
    public function validateRequiredUnlessAll($attribute, $value, $parameters)
218
    {
219 6
        $this->requireParameterCount(2, $parameters, 'required_unless_all');
220
221 6
        $valid = true;
222 6
        $count = count($parameters);
223
224 6
        for ($i = 0; $i < $count; $i += 2) {
225 6
            $field = $parameters[$i];
226 6
            $fieldValue = $parameters[$i + 1];
227
            
228 6
            if ($fieldValue == $this->getValue($field)) {
229 6
                $valid = false;
230 6
                break;
231
            }
232 1
        }
233
234 6
        if ($valid) {
235 1
            return $this->validateRequired($attribute, $value);
236
        }
237
238 6
        $this->mergeRules($attribute, 'nullable');
239
240 6
        return true;
241
    }
242
243
    /**
244
     * Replace all place-holders for the after rule.
245
     *
246
     * @param  string  $message
247
     * @param  string  $attribute
248
     * @param  string  $rule
249
     * @param  array   $parameters
250
     * @return string
251
     */
252 1
    public function replaceBeforeOrEqual($message, $attribute, $rule, $parameters)
253
    {
254 1
        return $this->replaceBefore($message, $attribute, $rule, $parameters);
255
    }
256
257
    /**
258
     * Replace all place-holders for the after rule.
259
     *
260
     * @param  string  $message
261
     * @param  string  $attribute
262
     * @param  string  $rule
263
     * @param  array   $parameters
264
     * @return string
265
     */
266 1
    public function replaceAfterOrEqual($message, $attribute, $rule, $parameters)
267
    {
268 1
        return $this->replaceAfter($message, $attribute, $rule, $parameters);
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274 9
    public function __call($method, $parameters)
275
    {
276 9
        if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) {
277 8
            $className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]);
278
279 8
            if (class_exists($className)) {
280 8
                $reflection = new ReflectionClass($className);
281 8
                if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) {
282 8
                    $arguments = (array) (isset($parameters[2]) ? $parameters[2] : []);
283 8
                    $instance = $reflection->newInstanceArgs($arguments);
284 8
                    return $instance->validate($parameters[1]);
285
                }
286
            }
287
        }
288
289 1
        return parent::__call($method, $parameters);
290
    }
291
}
292