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.

RuleValidator::executeNotValidationInParameter()   A
last analyzed

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 111
    public function useRules(array $rules): void
30
    {
31 111
        $this->rules = $rules;
32 111
    }
33
34
    /**
35
     * @param array $parameter
36
     */
37 75
    public function executeDefaultValidationInParameter(array $parameter): void
38
    {
39 75
        foreach ($this->rules as $rule) {
40 75
            $nameWithoutParenthesis = $this->clearFunctionName($rule);
41 75
            $ruleToTest = $this->getFunctionParameters($rule) !== '' ?
42 12
                v::{$nameWithoutParenthesis}($this->getFunctionParameters($rule)) :
43 75
                v::{$nameWithoutParenthesis}();
44 75
            $this->validateParamByRule($parameter, $ruleToTest);
45
        }
46 75
    }
47
48
    /**
49
     * @param array $parameter
50
     */
51 18
    public function executeOptionalValidationInParameter(array $parameter): void
52
    {
53 18
        foreach ($this->rules as $rule) {
54 18
            $nameWithoutParenthesis = $this->clearFunctionName($rule);
55 18
            $ruleToTest = $this->getFunctionParameters($rule) !== '' ?
56
                v::{$nameWithoutParenthesis}($this->getFunctionParameters($rule)) :
57 18
                v::{$nameWithoutParenthesis}();
58 18
            $optionalRuleToTest = v::optional($ruleToTest);
59 18
            $this->validateParamByRule($parameter, $optionalRuleToTest);
60
        }
61 18
    }
62
63
    /**
64
     * @param array $parameter
65
     */
66 27
    public function executeNotValidationInParameter(array $parameter): void
67
    {
68 27
        foreach ($this->rules as $rule) {
69 27
            $nameWithoutParenthesis = $this->clearFunctionName($rule);
70 27
            $ruleToTest = $this->getFunctionParameters($rule) !== '' ?
71
                v::{$nameWithoutParenthesis}($this->getFunctionParameters($rule)) :
72 27
                v::{$nameWithoutParenthesis}();
73 27
            $notRuleToTest = v::not($ruleToTest);
74 27
            $this->validateParamByRule($parameter, $notRuleToTest);
75
        }
76 27
    }
77
78
    /**
79
     * @param $param
80
     * @param v $rule
81
     */
82 111
    private function validateParamByRule(array $param, v $rule): void
83
    {
84 111
        $parameterKey = key($param);
85
86
        try {
87 111
            $rule->setName($parameterKey)->assert($param[$parameterKey]);
88 66
        } catch (NestedValidationException $err) {
89 66
            $err->findMessages([
90 66
                'numeric' => '{{name}} precisa ser um número.',
91
                'positive' => '{{name}} precisa ser positivo.',
92
                'notEmpty' => '{{name}} não pode ser vazio.',
93
                'notBlank' => '{{name}} não pode estar em branco.',
94
                'not' => '{{name}} não'
95
            ]);
96 66
            $this->errors[] = $err->getMessages()[0];
97 66
            $this->allErrors[] = $err->getMessages();
98
        }
99 111
    }
100
101
    /**
102
     * @param $function
103
     * @return string
104
     */
105 111
    private function getFunctionParameters($function): string
106
    {
107 111
        if (!strpos($function, '(')) {
108 111
            return '';
109
        }
110
111 12
        $openParenthesis = strpos($function, '(');
112 12
        $closeParenthesis = strrpos($function, ')');
113 12
        $funcParametersBody = substr($function, $openParenthesis + 1, ($closeParenthesis - $openParenthesis) - 1);
114
115 12
        return $funcParametersBody;
116
    }
117
118
    /**[
119
     * @param string $unclearFuncName
120
     * @return string
121
     */
122 111
    private function clearFunctionName(string $unclearFuncName): string
123
    {
124 111
        $parameterWithParenthesis = sprintf(
125 111
            '(%s)',
126 111
            $this->getFunctionParameters($unclearFuncName)
127
        );
128
129 111
        return str_replace($parameterWithParenthesis, '', $unclearFuncName);
130
    }
131
132
    /**
133
     * @return null|array
134
     */
135 90
    public function getValidationErrors(): ?array
136
    {
137 90
        return $this->errors;
138
    }
139
140
    /**
141
     * @return array
142
     */
143 81
    public function getAllValidationErrors(): ?array
144
    {
145 81
        return $this->allErrors;
146
    }
147
}
148