GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d0a4cd...ecd986 )
by André Santos
08:51 queued 07:20
created

RuleValidator::executeNotValidationInParameter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Andresmeireles\RespectAnnotation\Annotation;
4
5
use Respect\Validation\Exceptions\NestedValidationException;
6
use Respect\Validation\Validator as v;
7
8
/**
9
 * Class RuleValidator
10
 * @package Andresmeireles\RespectAnnotation\Annotation
11
 */
12
class RuleValidator
13
{
14
    /**
15
     * @var array
16
     */
17
    private $rules;
18
19
    /**
20
     * @var array|null
21
     */
22
    private $errors;
23
24
    /**
25
     * @var array|null
26
     */
27
    private $allErrors;
28
29
//    public function __construct(array $rules)
30
//    {
31
//        $this->rules = $rules;
32
//    }
33
34 69
    public function useRules(array $rules)
35
    {
36 69
        $this->rules = $rules;
37 69
    }
38
39
    /**
40
     * @param array $parameter
41
     */
42 39
    public function executeDefaultValidationInParameter(array $parameter): void
43
    {
44 39
        foreach ($this->rules as $rule) {
45 39
            $nameWithoutParenthesis = $this->clearFunctionName($rule);
46 39
            $ruleToTest = $this->getFunctionParameters($rule) !== '' ?
47
                v::{$nameWithoutParenthesis}($this->getFunctionParameters($rule)) :
48 39
                v::{$nameWithoutParenthesis}();
49 39
            $this->validateParamByRule($parameter, $ruleToTest);
50
        }
51 39
    }
52
53
    /**
54
     * @param array $parameter
55
     */
56 18
    public function executeOptionalValidationInParameter(array $parameter): void
57
    {
58 18
        foreach ($this->rules as $rule) {
59 18
            $nameWithoutParenthesis = $this->clearFunctionName($rule);
60 18
            $ruleToTest = $this->getFunctionParameters($rule) !== '' ?
61
                v::{$nameWithoutParenthesis}($this->getFunctionParameters($rule)) :
62 18
                v::{$nameWithoutParenthesis}();
63 18
            $optionalRuleToTest = v::optional($ruleToTest);
64 18
            $this->validateParamByRule($parameter, $optionalRuleToTest);
65
        }
66 18
    }
67
68
    /**
69
     * @param array $parameter
70
     */
71 21
    public function executeNotValidationInParameter(array $parameter): void
72
    {
73 21
        foreach ($this->rules as $rule) {
74 21
            $nameWithoutParenthesis = $this->clearFunctionName($rule);
75 21
            $ruleToTest = $this->getFunctionParameters($rule) !== '' ?
76
                v::{$nameWithoutParenthesis}($this->getFunctionParameters($rule)) :
77 21
                v::{$nameWithoutParenthesis}();
78 21
            $notRuleToTest = v::not($ruleToTest);
79 21
            $this->validateParamByRule($parameter, $notRuleToTest);
80
        }
81 21
    }
82
83
    /**
84
     * @param $param
85
     * @param v $rule
86
     */
87 69
    private function validateParamByRule(array $param, v $rule): void
88
    {
89 69
        $parameterKey = key($param);
90
91
        try {
92 69
            $rule->setName($parameterKey)->assert($param[$parameterKey]);
93 45
        } catch (NestedValidationException $err) {
94 45
            $err->findMessages([
95 45
                'numeric' => '{{name}} precisa ser um número.',
96
                'positive' => '{{name}} precisa ser positivo.',
97
                'notEmpty' => '{{name}} não pode ser vazio.',
98
                'notBlank' => '{{name}} não pode estar em branco.',
99
                'not' => '{{name}} não'
100
            ]);
101 45
            $this->errors[] = $err->getMessages()[0];
102 45
            $this->allErrors[] = $err->getMessages();
103
        }
104 69
    }
105
106
    /**
107
     * @param $function
108
     * @return string
109
     */
110 69
    private function getFunctionParameters($function): string
111
    {
112 69
        if (!strpos($function, '(')) {
113 69
            return '';
114
        }
115
116
        $openParenthesis = strpos($function, '(');
117
        $closeParenthesis = strrpos($function, ')');
118
        $funcParametersBody = substr($function, $openParenthesis + 1, ($closeParenthesis - $openParenthesis) - 1);
119
120
        return $funcParametersBody;
121
    }
122
123
    /**[
124
     * @param string $unclearFuncName
125
     * @return string
126
     */
127 69
    private function clearFunctionName(string $unclearFuncName): string
128
    {
129 69
        $parameterWithParenthesis = sprintf(
130 69
            '(%s)',
131 69
            $this->getFunctionParameters($unclearFuncName)
132
        );
133
134 69
        return str_replace($parameterWithParenthesis, '', $unclearFuncName);
135
    }
136
137
    /**
138
     * @return null|array
139
     */
140 69
    public function getValidationErrors(): ?array
141
    {
142 69
        return $this->errors;
143
    }
144
145
    /**
146
     * @return array
147
     */
148 69
    public function getAllValidationErrors(): ?array
149
    {
150 69
        return $this->allErrors;
151
    }
152
}
153