Completed
Push — master ( 3348b7...0d8720 )
by Renato
05:41
created

ValidatorResolver::validateRequiredIf()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 3
dl 0
loc 26
ccs 0
cts 17
cp 0
crap 30
rs 8.439
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
            if ($fieldValue != $this->getValue($field)) {
186 6
                $valid = false;
187 6
                break;
188
            }
189 6
        }
190
191 6
        if ($valid) {
192 1
            return $this->validateRequired($attribute, $value);
193
        }
194
195 6
        $this->mergeRules($attribute, 'nullable');
196
197 6
        return true;
198
    }
199
200
    /**
201
     * Validate that an attribute exists when another attribute does not have a given value.
202
     *
203
     * @param  string  $attribute
204
     * @param  mixed  $value
205
     * @param  mixed  $parameters
206
     * @return bool
207
     */
208 6
    public function validateRequiredUnlessAll($attribute, $value, $parameters)
209
    {
210 6
        $this->requireParameterCount(2, $parameters, 'required_unless_all');
211
212 6
        $valid = true;
213 6
        $count = count($parameters);
214
215 6
        for ($i = 0; $i < $count; $i += 2) {
216 6
            $field = $parameters[$i];
217 6
            $fieldValue = $parameters[$i + 1];
218
            
219 6
            if ($fieldValue == $this->getValue($field)) {
220 6
                $valid = false;
221 6
                break;
222
            }
223 1
        }
224
225 6
        if ($valid) {
226 1
            return $this->validateRequired($attribute, $value);
227
        }
228
229 6
        $this->mergeRules($attribute, 'nullable');
230
231 6
        return true;
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237 9
    public function __call($method, $parameters)
238
    {
239 9
        if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) {
240 8
            $className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]);
241
242 8
            if (class_exists($className)) {
243 8
                $reflection = new ReflectionClass($className);
244 8
                if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) {
245 8
                    $arguments = (array) (isset($parameters[2]) ? $parameters[2] : []);
246 8
                    $instance = $reflection->newInstanceArgs($arguments);
247 8
                    return $instance->validate($parameters[1]);
248
                }
249
            }
250
        }
251
252 1
        return parent::__call($method, $parameters);
253
    }
254
}
255