Completed
Push — master ( 0214e8...4fdbd2 )
by Renato
10:18
created

ValidatorResolver::validateCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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