Completed
Push — master ( 326602...e591d7 )
by Renato
07:39
created

ValidatorResolver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.59%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 102
ccs 25
cts 27
cp 0.9259
rs 10
c 3
b 0
f 0
wmc 15
lcom 0
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validatePattern() 0 4 1
A validateCurrentPassword() 0 6 3
A validateDocument() 0 9 2
A validateCurrency() 0 4 1
A validateNotExists() 0 4 1
B __call() 0 17 7
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 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...
29
    {
30 1
        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 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...
43
    {
44 2
        $guard = isset($parameters[0]) ? $parameters[0] : null;
45 2
        $field = isset($parameters[1]) ? $parameters[1] : 'password';
46 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...
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 1
    public function validateDocument($attribute, $value, $parameters = array())
59
    {
60 1
        $value = preg_replace('/[^0-9]/', '', $value);
61 1
        if (strlen($value) == 11) {
62 1
            return $this->validateCpf($attribute, $value, $parameters);
63
        }
64
65 1
        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 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...
78
    {
79 40
        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 2
    public function validateNotExists($attribute, $value, $parameters)
91
    {
92 2
        return !$this->validateExists($attribute, $value, $parameters);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 9
    public function __call($method, $parameters)
99
    {
100 9
        if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) {
101 8
            $className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]);
102
103 8
            if (class_exists($className)) {
104 8
                $reflection = new ReflectionClass($className);
105 8
                if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) {
106 8
                    $arguments = (array) (isset($parameters[2]) ? $parameters[2] : []);
107 8
                    $instance = $reflection->newInstanceArgs($arguments);
108 8
                    return $instance->validate($parameters[1]);
109
                }
110
            }
111
        }
112
113 1
        return parent::__call($method, $parameters);
114
    }
115
}
116