Conditions | 40 |
Paths | 510 |
Total Lines | 129 |
Code Lines | 73 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public static function execute(array $datas): bool |
||
70 | { |
||
71 | self::$data = $datas; |
||
72 | |||
73 | self::existData(); |
||
74 | self::jsonData(); |
||
75 | self::hasProvider(); |
||
76 | self::hasRole(); |
||
77 | |||
78 | $data = (array) json_decode($datas['data']); |
||
79 | |||
80 | self::includeValidations(); |
||
81 | |||
82 | $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider'])); |
||
83 | |||
84 | self::existRole($rules); |
||
85 | |||
86 | $validators = self::$validators[get_class($rules)]->getRules($datas['role']); |
||
87 | |||
88 | $tests = 0; |
||
89 | |||
90 | foreach ($validators as $key => $value) { |
||
91 | $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
||
92 | } |
||
93 | |||
94 | $testeds = 0; |
||
95 | |||
96 | foreach ($validators as $key => $value) { |
||
97 | |||
98 | foreach ($data as $keyy => $valuee) { |
||
99 | |||
100 | $v = $valuee; |
||
101 | |||
102 | if(is_array($valuee)){ |
||
103 | $v = null; |
||
104 | foreach ($valuee as $vvv) { |
||
105 | $v .= $vvv; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | $valuee = $v; |
||
110 | |||
111 | if(!array_key_exists($keyy, $validators)){ |
||
112 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
||
113 | } |
||
114 | |||
115 | if($keyy===$key){ |
||
116 | |||
117 | $testeds++; |
||
118 | |||
119 | foreach ($value as $subkey => $subvalue) { |
||
120 | |||
121 | switch ($subkey) { |
||
122 | case 'minlength': |
||
123 | if(array_key_exists('required', $value)){ |
||
124 | if($value['required'] or strlen($valuee)!==0){ |
||
125 | if(strlen($valuee)===0){ |
||
126 | throw new Exception("O campo '{$key}' é obrigatório.",1); |
||
127 | } |
||
128 | |||
129 | if(strlen($valuee) < (int) $subvalue){ |
||
130 | throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.",1); |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | break; |
||
135 | |||
136 | case 'type': |
||
137 | if(array_key_exists('required', $value)){ |
||
138 | if($value['required'] or strlen($valuee)!==0){ |
||
139 | switch ($subvalue) { |
||
140 | case 'date': |
||
141 | $date = explode('/', $valuee); |
||
142 | if(count($date) != 3){ |
||
143 | throw new Exception('Data inválida.',1); |
||
144 | } |
||
145 | if(!@checkdate(intval($date[1]), intval($date[0]), intval($date[2]) )){ |
||
146 | throw new Exception('Data inválida.',1); |
||
147 | } |
||
148 | break; |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | break; |
||
153 | |||
154 | case 'maxlength': |
||
155 | if(array_key_exists('required', $value)){ |
||
156 | if($value['required'] or strlen($valuee)!==0){ |
||
157 | if(strlen($valuee)>(int)$subvalue){ |
||
158 | throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.",1); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | break; |
||
163 | |||
164 | case 'regex': |
||
165 | if(array_key_exists('required', $value)){ |
||
166 | if($value['required'] or strlen($valuee)!==0){ |
||
167 | if(!@preg_match($subvalue,$valuee)){ |
||
168 | throw new Exception("{$key} inválido(a).",1); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | break; |
||
173 | |||
174 | case 'equals': |
||
175 | $equals = false; |
||
176 | foreach ($data as $ke => $sub) { |
||
177 | if($ke===$subvalue){ |
||
178 | $equals=true; |
||
179 | if($valuee !== $sub) |
||
180 | throw new \Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1); |
||
181 | } |
||
182 | } |
||
183 | if(!$equals){ |
||
184 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
||
185 | } |
||
186 | break; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | if($tests > $testeds){ |
||
194 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
||
195 | } |
||
196 | |||
197 | return true; |
||
198 | } |
||
234 |