Completed
Push — master ( 9bf1c7...3749d0 )
by Renato
07:44
created

BaseValidator::getValidator()   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 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace NwLaravel\Validation;
3
4
use Prettus\Validator\LaravelValidator;
5
6
/**
7
 * Class BaseValidator
8
 * @abstract
9
 */
10
abstract class BaseValidator extends LaravelValidator
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $keyName;
16
17
    /**
18
     * Get Validator
19
     *
20
     * @return \Illuminate\Validation\Factory
21
     */
22 1
    public function getValidator()
23
    {
24 1
        return $this->validator;
25
    }
26
27
    /**
28
     * Set Key Name
29
     *
30
     * @param Key $keyName Key Name
31
     *
32
     * @return $this
33
     */
34 1
    public function setKeyName($keyName)
35
    {
36 1
        $this->keyName = $keyName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $keyName of type object<NwLaravel\Validation\Key> is incompatible with the declared type string of property $keyName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37 1
    }
38
39
    /**
40
     * Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE
41
     *
42
     * Default rule: ValidatorInterface::RULE_CREATE
43
     *
44
     * @param null $action
45
     * @return array
46
     */
47 1
    public function getRules($action = null)
48
    {
49 1
        $rules = [];
50
51 1
        if (isset($this->rules[$action])) {
52 1
            $rules = $this->rules[$action];
53 1
        }
54
55 1
        return $this->parserValidationRules($rules, $this->id);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->parserValidationRules($rules, $this->id); of type NwLaravel\Validation\Unknown|array adds the type NwLaravel\Validation\Unknown to the return on line 55 which is incompatible with the return type declared by the interface Prettus\Validator\Contra...atorInterface::getRules of type array.
Loading history...
56
    }
57
58
    /**
59
     * Parser Validation Rules
60
     *
61
     * @param Unknown $rules Rules
62
     * @param null    $id    Null Id
63
     *
64
     * @return array
65
     */
66 1
    protected function parserValidationRules($rules, $id = null)
67
    {
68
69 1
        if ($id === null) {
70 1
            return $rules;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $rules; (NwLaravel\Validation\Unknown) is incompatible with the return type of the parent method Prettus\Validator\Abstra...::parserValidationRules of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
71
        }
72
73 1
        array_walk($rules, function (&$rules, $field) use ($id) {
74 1
            if (!is_array($rules)) {
75 1
                $rules = explode("|", $rules);
76 1
            }
77
78 1
            foreach ($rules as $ruleIdx => $rule) {
79
                // get name and parameters
80 1
                @list($name, $params) = array_pad(explode(":", $rule), 2, null);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
81
82
                // only do someting for the unique rule
83 1
                if (strtolower($name) != "unique") {
84 1
                    continue; // continue in foreach loop, nothing left to do here
85
                }
86
87 1
                $p = array_map("trim", explode(",", $params));
88
89
                // set field name to rules key ($field) (laravel convention)
90 1
                if (!isset($p[1])) {
91 1
                    $p[1] = $field;
92 1
                }
93
94
                // set 3rd parameter to id given to getValidationRules()
95 1
                $p[2] = $id;
96 1
                if ($this->keyName) {
97 1
                    $p[3] = $this->keyName;
98 1
                }
99
100 1
                $params = implode(",", $p);
101 1
                $rules[$ruleIdx] = $name.":".$params;
102 1
            }
103 1
        });
104
105 1
        return $rules;
106
    }
107
}
108