Completed
Push — master ( 3423e4...b569e5 )
by Renato
08:36
created

ValidatorResolver::validateDocument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
namespace NwLaravel\Validation;
4
5
use ReflectionClass;
6
use Illuminate\Validation\Validator;
7
8
/**
9
 * Class ValidatorResolver
10
 *
11
 * @method bool validateCpf(string $attribute, mixed $value, array $parameters)
12
 * @method bool validateCnpj(string $attribute, mixed $value, array $parameters)
13
 */
14
class ValidatorResolver extends Validator
15
{
16
17
    protected $currentRule;
18
    
19
    /**
20
     * Validate Pattern Valid
21
     *
22
     * @param string $attribute  String Attribute
23
     * @param string $value      String Value
24
     * @param array  $parameters Array Parameters
25
     *
26
     * @return boolean
27
     */
28
    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...
29
    {
30
        return (bool) (@preg_match($value, "subject") !== false);
31
    }
32
33
    /**
34
     * Validate Current Password
35
     *
36
     * @param string $attribute  String Attribute
37
     * @param mixed  $value      Mixed Value
38
     * @param array  $parameters Array Parameters
39
     *
40
     * @return bool
41
     */
42
    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...
43
    {
44
        $guard = isset($parameters[0]) ? $parameters[0] : null;
45
        $field = isset($parameters[1]) ? $parameters[1] : 'password';
46
        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...
47
    }
48
49
    /**
50
     * Validate that an attribute is cpf valid
51
     *
52
     * @param string $attribute String Attribute
53
     * @param mixed  $value     Mixed Value
54
     * @param array  $parameters Array Parameters
55
     *
56
     * @return bool
57
     */
58
    public function validateDocument($attribute, $value, $parameters = array())
59
    {
60
        $value = preg_replace('/[^0-9]/', '', $value);
61
        if (strlen($value) == 11) {
62
            return $this->validateCpf($attribute, $value, $parameters);
63
        }
64
65
        return $this->validateCnpj($attribute, $value, $parameters);
66
    }
67
68
    /**
69
     * Validate currency
70
     *
71
     * @param string $attribute String Attribute
72
     * @param mixed  $value     Mixed Value
73
     * @param array  $parameters Array Parameters
74
     *
75
     * @return bool
76
     */
77
    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...
78
    {
79
        return !is_null(asCurrency($value));
80
    }
81
82
    /**
83
     * Validate the not existence of an attribute value in a database table.
84
     *
85
     * @param  string  $attribute
86
     * @param  mixed   $value
87
     * @param  array   $parameters
88
     * @return bool
89
     */
90
    public function validateNotExists($attribute, $value, $parameters)
91
    {
92
        return !$this->validateExists($attribute, $value, $parameters);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function __call($method, $parameters)
99
    {
100
        if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) {
101
            $className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]);
102
103
            if (class_exists($className)) {
104
                $reflection = new ReflectionClass($className);
105
                if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) {
106
                    $arguments = (array) (isset($parameters[2]) ? $parameters[2] : []);
107
                    $valided = app($className, $arguments)->validate($parameters[1]);
0 ignored issues
show
Unused Code introduced by
The call to app() has too many arguments starting with $arguments.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
                }
109
            }
110
        }
111
112
        if (isset($valided)) {
113
            return $valided;
114
        }
115
116
        return parent::__call($method, $parameters);
117
    }
118
}
119