Complex classes like Validator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Validator |
||
20 | { |
||
21 | |||
22 | /** @var array */ |
||
23 | private $errors = []; |
||
24 | /** @var array */ |
||
25 | private $namings = []; |
||
26 | /** @var array */ |
||
27 | private $customErrorsWithInputName = []; |
||
28 | /** @var array */ |
||
29 | private $customErrors = []; |
||
30 | |||
31 | /** |
||
32 | * Constructor is not allowed because Validoo uses its own |
||
33 | * static method to instantiate the validation |
||
34 | */ |
||
35 | 46 | private function __construct($errors, $namings) |
|
40 | |||
41 | /** |
||
42 | * @return bool |
||
43 | */ |
||
44 | 44 | public function isSuccess() |
|
48 | |||
49 | /** |
||
50 | * @param array $errors_array |
||
51 | */ |
||
52 | 1 | public function customErrors(array $errors_array) |
|
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | 3 | protected function getDefaultLang(): string |
|
72 | |||
73 | /** |
||
74 | * @param string $lang |
||
75 | * @return null |
||
76 | */ |
||
77 | 3 | protected function getErrorFilePath(string $lang) |
|
81 | |||
82 | /** |
||
83 | * @param string|null $lang |
||
84 | * @return array|mixed |
||
85 | */ |
||
86 | 3 | protected function getDefaultErrorTexts(string $lang = null) |
|
95 | |||
96 | /** |
||
97 | * @param string|null $lang |
||
98 | * @return array|mixed |
||
99 | */ |
||
100 | 3 | protected function getCustomErrorTexts(string $lang = null) |
|
109 | |||
110 | /** |
||
111 | * @param string $input_name |
||
112 | * @return mixed|string |
||
113 | */ |
||
114 | 3 | protected function handleNaming(string $input_name) |
|
123 | |||
124 | /** |
||
125 | * @param array $params |
||
126 | * @return array |
||
127 | */ |
||
128 | 3 | protected function handleParameterNaming(array $params) |
|
141 | |||
142 | /** |
||
143 | * @param string|null $lang |
||
144 | * @return array |
||
145 | * @throws ValidooException |
||
146 | */ |
||
147 | 3 | public function getErrors(string $lang = null): array |
|
190 | |||
191 | /** |
||
192 | * @param string $input_name |
||
193 | * @param string|null $rule_name |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function has(string $input_name, string $rule_name = null): bool |
||
202 | |||
203 | /** |
||
204 | * @return array |
||
205 | */ |
||
206 | final public function getResults(): array |
||
210 | |||
211 | /** |
||
212 | * Gets the parameter names of a rule |
||
213 | * @param $rule |
||
214 | * @return mixed |
||
215 | */ |
||
216 | 46 | private static function getParams($rule) |
|
229 | |||
230 | /** |
||
231 | * Handle parameter with input name |
||
232 | * eg: equals(:name) |
||
233 | * @param mixed $params |
||
234 | * @return mixed |
||
235 | */ |
||
236 | 46 | private static function getParamValues($params, $inputs) |
|
245 | |||
246 | /** |
||
247 | * @param mixed $inputs |
||
248 | * @param array $rules |
||
249 | * @param array|null $naming |
||
250 | * @return Validator |
||
251 | * @throws ValidooException |
||
252 | */ |
||
253 | 46 | public static function validate($inputs, array $rules, array $naming = null): self |
|
305 | |||
306 | /** |
||
307 | * @param null $input |
||
308 | * @return bool |
||
309 | */ |
||
310 | 9 | protected static function required($input = null): bool |
|
314 | |||
315 | /** |
||
316 | * @param $input |
||
317 | * @return bool |
||
318 | */ |
||
319 | protected static function numeric($input): bool |
||
323 | |||
324 | /** |
||
325 | * @param $input |
||
326 | * @return bool |
||
327 | */ |
||
328 | 4 | protected static function email($input): bool |
|
332 | |||
333 | /** |
||
334 | * @param $input |
||
335 | * @return bool |
||
336 | */ |
||
337 | 11 | protected static function integer($input): bool |
|
341 | |||
342 | /** |
||
343 | * @param $input |
||
344 | * @return bool |
||
345 | */ |
||
346 | protected static function float($input): bool |
||
350 | |||
351 | /** |
||
352 | * @param $input |
||
353 | * @return bool |
||
354 | */ |
||
355 | 3 | protected static function alpha($input): bool |
|
359 | |||
360 | /** |
||
361 | * @param $input |
||
362 | * @return bool |
||
363 | */ |
||
364 | protected static function alpha_numeric($input): bool |
||
368 | |||
369 | /** |
||
370 | * @param $input |
||
371 | * @return bool |
||
372 | */ |
||
373 | 4 | protected static function ip($input): bool |
|
377 | |||
378 | /* |
||
379 | * TODO: need improvements for tel and urn urls. |
||
380 | * check out url.test.php for the test result |
||
381 | * urn syntax: http://www.faqs.org/rfcs/rfc2141.html |
||
382 | * |
||
383 | */ |
||
384 | /** |
||
385 | * @param $input |
||
386 | * @return bool |
||
387 | */ |
||
388 | 9 | protected static function url($input): bool |
|
392 | |||
393 | /** |
||
394 | * @param $input |
||
395 | * @param $length |
||
396 | * @return bool |
||
397 | */ |
||
398 | protected static function max_length($input, $length): bool |
||
402 | |||
403 | /** |
||
404 | * @param $input |
||
405 | * @param $length |
||
406 | * @return bool |
||
407 | */ |
||
408 | protected static function min_length($input, $length): bool |
||
412 | |||
413 | /** |
||
414 | * @param $input |
||
415 | * @param $length |
||
416 | * @return bool |
||
417 | */ |
||
418 | protected static function exact_length($input, $length): bool |
||
422 | |||
423 | /** |
||
424 | * @param $input |
||
425 | * @param $param |
||
426 | * @return bool |
||
427 | */ |
||
428 | 4 | protected static function equals($input, $param): bool |
|
432 | |||
433 | /** |
||
434 | * @param $input |
||
435 | * @return bool |
||
436 | */ |
||
437 | protected static function is_path($input): bool |
||
441 | |||
442 | /** |
||
443 | * @param $input |
||
444 | * @return bool |
||
445 | */ |
||
446 | protected static function is_filename($input): bool |
||
450 | } |
||
451 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.