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 | * @param $errors |
||
35 | * @param $namings |
||
36 | */ |
||
37 | 48 | private function __construct($errors, $namings) |
|
42 | |||
43 | /** |
||
44 | * @param $inputs |
||
45 | * @param array $rules |
||
46 | * @param array|null $naming |
||
47 | * @return Validator |
||
48 | * @throws ValidooException |
||
49 | */ |
||
50 | 48 | public static function validate($inputs, array $rules, array $naming = null): self |
|
91 | |||
92 | /** |
||
93 | * @param $closure |
||
94 | * @param $params |
||
95 | * @param $rule |
||
96 | * @return mixed |
||
97 | * @throws ValidooException |
||
98 | */ |
||
99 | 48 | private static function doValidation($closure, $params, $rule) |
|
117 | |||
118 | /** |
||
119 | * Gets the parameter names of a rule |
||
120 | * @param $rule |
||
121 | * @return mixed |
||
122 | */ |
||
123 | 48 | private static function getParams($rule) |
|
136 | |||
137 | /** |
||
138 | * Handle parameter with input name |
||
139 | * eg: equals(:name) |
||
140 | * @param mixed $params |
||
141 | * @param $inputs |
||
142 | * @return mixed |
||
143 | */ |
||
144 | 48 | private static function getParamValues($params, $inputs) |
|
153 | |||
154 | /** |
||
155 | * @param null $input |
||
156 | * @return bool |
||
157 | */ |
||
158 | 9 | protected static function required($input = null): bool |
|
162 | |||
163 | /** |
||
164 | * @param $input |
||
165 | * @return bool |
||
166 | */ |
||
167 | protected static function numeric($input): bool |
||
171 | |||
172 | /** |
||
173 | * @param $input |
||
174 | * @return bool |
||
175 | */ |
||
176 | 4 | protected static function email($input): bool |
|
180 | |||
181 | /** |
||
182 | * @param $input |
||
183 | * @return bool |
||
184 | */ |
||
185 | 2 | protected static function isdir($input): bool |
|
189 | |||
190 | /** |
||
191 | * @param $input |
||
192 | * @return bool |
||
193 | */ |
||
194 | 11 | protected static function integer($input): bool |
|
198 | |||
199 | /** |
||
200 | * @param $input |
||
201 | * @return bool |
||
202 | */ |
||
203 | protected static function float($input): bool |
||
207 | |||
208 | /** |
||
209 | * @param $input |
||
210 | * @return bool |
||
211 | */ |
||
212 | 4 | protected static function alpha($input): bool |
|
216 | |||
217 | /** |
||
218 | * @param $input |
||
219 | * @return bool |
||
220 | */ |
||
221 | protected static function alpha_numeric($input): bool |
||
225 | |||
226 | /** |
||
227 | * @param $input |
||
228 | * @return bool |
||
229 | */ |
||
230 | 4 | protected static function ip($input): bool |
|
234 | |||
235 | /** |
||
236 | * @param $input |
||
237 | * @return bool |
||
238 | */ |
||
239 | 9 | protected static function url($input): bool |
|
243 | |||
244 | /** |
||
245 | * @param $input |
||
246 | * @param $length |
||
247 | * @return bool |
||
248 | */ |
||
249 | protected static function max_length($input, $length): bool |
||
253 | |||
254 | /** |
||
255 | * @param $input |
||
256 | * @param $length |
||
257 | * @return bool |
||
258 | */ |
||
259 | protected static function min_length($input, $length): bool |
||
263 | |||
264 | /** |
||
265 | * @param $input |
||
266 | * @param $length |
||
267 | * @return bool |
||
268 | */ |
||
269 | protected static function exact_length($input, $length): bool |
||
273 | |||
274 | /** |
||
275 | * @param $input |
||
276 | * @param $param |
||
277 | * @return bool |
||
278 | */ |
||
279 | 4 | protected static function equals($input, $param): bool |
|
283 | |||
284 | /** |
||
285 | * @param $input |
||
286 | * @return bool |
||
287 | */ |
||
288 | protected static function is_filename($input): bool |
||
292 | |||
293 | /** |
||
294 | * @return bool |
||
295 | */ |
||
296 | 46 | public function isSuccess() |
|
300 | |||
301 | /** |
||
302 | * @param array $errors_array |
||
303 | */ |
||
304 | 1 | public function customErrors(array $errors_array) |
|
316 | |||
317 | /** |
||
318 | * @param string|null $lang |
||
319 | * @return array |
||
320 | * @throws ValidooException |
||
321 | */ |
||
322 | 3 | public function getErrors(string $lang = null): array |
|
367 | |||
368 | /** |
||
369 | * @return string |
||
370 | */ |
||
371 | 3 | protected function getDefaultLang(): string |
|
375 | |||
376 | /* |
||
377 | * TODO: need improvements for tel and urn urls. |
||
378 | * check out url.test.php for the test result |
||
379 | * urn syntax: http://www.faqs.org/rfcs/rfc2141.html |
||
380 | * |
||
381 | */ |
||
382 | |||
383 | /** |
||
384 | * @param string|null $lang |
||
385 | * @return array|mixed |
||
386 | */ |
||
387 | 3 | protected function getDefaultErrorTexts(string $lang = null) |
|
402 | |||
403 | /** |
||
404 | * @param string $input_name |
||
405 | * @return mixed|string |
||
406 | */ |
||
407 | 3 | protected function handleNaming(string $input_name) |
|
417 | |||
418 | /** |
||
419 | * @param array $params |
||
420 | * @return array |
||
421 | */ |
||
422 | 3 | protected function handleParameterNaming(array $params) |
|
436 | |||
437 | /** |
||
438 | * @param string $input_name |
||
439 | * @param string|null $rule_name |
||
440 | * @return bool |
||
441 | */ |
||
442 | public function has(string $input_name, string $rule_name = null): bool |
||
450 | |||
451 | /** |
||
452 | * @return array |
||
453 | */ |
||
454 | final public function getResults(): array |
||
458 | } |
||
459 |