Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Validation 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 Validation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Validation { |
||
|
|||
14 | |||
15 | /** |
||
16 | * validation errors |
||
17 | * |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $errors = []; |
||
22 | |||
23 | /** |
||
24 | * Custom rule messages. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $ruleMessages = []; |
||
29 | |||
30 | /** |
||
31 | * Start the validation using values and rules passed in $data |
||
32 | * |
||
33 | * @param array $data |
||
34 | * @param bool $skip To skip validations as soon as one of the rules fails. |
||
35 | * |
||
36 | * @throws Exception if rule method doesn't exist |
||
37 | * @return bool |
||
38 | */ |
||
39 | public function validate($data, $skip = false){ |
||
97 | |||
98 | /** |
||
99 | * Determine if a given value is empty, |
||
100 | * excluding '0', false, 0, 0.0, and files uploaded with UPLOAD_ERR_NO_FILE error, |
||
101 | * because these could be perfectly valid values, |
||
102 | * then, the validation methods has to decide if this value is valid or not. |
||
103 | * |
||
104 | * @param mixed $value |
||
105 | * @return bool |
||
106 | * |
||
107 | */ |
||
108 | private function isEmpty($value){ |
||
124 | |||
125 | /** |
||
126 | * Determine if a given rules has 'required' rule |
||
127 | * |
||
128 | * @param array $rules |
||
129 | * @return bool |
||
130 | */ |
||
131 | private function isRequired($rules){ |
||
134 | |||
135 | /** |
||
136 | * Determine if a given rule has arguments, Ex: max(4) |
||
137 | * |
||
138 | * @param string $rule |
||
139 | * @return bool |
||
140 | */ |
||
141 | private function isruleHasArgs($rule) { |
||
144 | |||
145 | /** |
||
146 | * get rule name for rules that have args |
||
147 | * |
||
148 | * @param string $rule |
||
149 | * @return string |
||
150 | */ |
||
151 | private function getRuleName($rule){ |
||
154 | |||
155 | /** |
||
156 | * get arguments for rules that have args |
||
157 | * |
||
158 | * @param string $rule |
||
159 | * @return array |
||
160 | */ |
||
161 | private function getRuleArgs($rule){ |
||
173 | |||
174 | /** |
||
175 | * Add a custom rule message. |
||
176 | * This message will be displayed instead of default. |
||
177 | * |
||
178 | * @param string $rule |
||
179 | * @param string $message |
||
180 | * @return array |
||
181 | */ |
||
182 | public function addRuleMessage($rule, $message){ |
||
185 | |||
186 | /** |
||
187 | * Add an error |
||
188 | * |
||
189 | * @param string $rule |
||
190 | * @param string $placeholder for field |
||
191 | * @param mixed $value |
||
192 | * @param array $args |
||
193 | * |
||
194 | */ |
||
195 | private function addError($rule, $placeholder, $value, $args = []){ |
||
232 | |||
233 | /** |
||
234 | * Checks if validation has passed. |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function passes(){ |
||
241 | |||
242 | /** |
||
243 | * get all errors |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public function errors(){ |
||
250 | |||
251 | /** |
||
252 | * clear all existing errors |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function clearErrors(){ |
||
259 | |||
260 | /** *********************************************** **/ |
||
261 | /** ************** Validations ************** **/ |
||
262 | /** *********************************************** **/ |
||
263 | |||
264 | /** |
||
265 | * Is value not empty? |
||
266 | * |
||
267 | * @param mixed $value |
||
268 | * @return bool |
||
269 | */ |
||
270 | /*private function required($value){ |
||
271 | return !$this->isEmpty($value); |
||
272 | }*/ |
||
273 | |||
274 | /** |
||
275 | * min string length |
||
276 | * |
||
277 | * @param string $str |
||
278 | * @param array $args(min) |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | private function minLen($str, $args){ |
||
285 | |||
286 | /** |
||
287 | * max string length |
||
288 | * |
||
289 | * @param string $str |
||
290 | * @param array $args(max) |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | private function maxLen($str, $args){ |
||
297 | |||
298 | /** |
||
299 | * check if number between given range of numbers |
||
300 | * |
||
301 | * @param int $num |
||
302 | * @param array $args(min,max) |
||
303 | * @return bool |
||
304 | */ |
||
305 | private function rangeNum($num, $args){ |
||
308 | |||
309 | /** |
||
310 | * check if value is a valid number |
||
311 | * |
||
312 | * @param string|integer $value |
||
313 | * @return bool |
||
314 | */ |
||
315 | private function integer($value){ |
||
318 | |||
319 | /** |
||
320 | * check if value(s) is in a given array |
||
321 | * |
||
322 | * @param string|array $value |
||
323 | * @param array $arr |
||
324 | * @return bool |
||
325 | */ |
||
326 | private function inArray($value, $arr){ |
||
338 | |||
339 | /** |
||
340 | * check if value is contains alphabetic characters and numbers |
||
341 | * |
||
342 | * @param mixed $value |
||
343 | * @return bool |
||
344 | */ |
||
345 | private function alphaNum($value){ |
||
348 | |||
349 | /** |
||
350 | * check if value is contains alphabetic characters, numbers and spaces |
||
351 | * |
||
352 | * @param mixed $value |
||
353 | * @return bool |
||
354 | */ |
||
355 | private function alphaNumWithSpaces($value){ |
||
358 | |||
359 | /** |
||
360 | * check if password has at least |
||
361 | * - one lowercase letter |
||
362 | * - one uppercase letter |
||
363 | * - one number |
||
364 | * - one special(non-word) character |
||
365 | * |
||
366 | * @param mixed $value |
||
367 | * @return bool |
||
368 | * @see http://stackoverflow.com/questions/8141125/regex-for-password-php |
||
369 | * @see http://code.runnable.com/UmrnTejI6Q4_AAIM/how-to-validate-complex-passwords-using-regular-expressions-for-php-and-pcre |
||
370 | */ |
||
371 | private function password($value) { |
||
374 | |||
375 | /** |
||
376 | * check if value is equals to another value(strings) |
||
377 | * |
||
378 | * @param string $value |
||
379 | * @param array $args(value) |
||
380 | * @return bool |
||
381 | */ |
||
382 | private function equals($value, $args){ |
||
385 | |||
386 | /** |
||
387 | * check if value is not equal to another value(strings) |
||
388 | * |
||
389 | * @param string $value |
||
390 | * @param array $args(value) |
||
391 | * @return bool |
||
392 | */ |
||
393 | private function notEqual($value, $args){ |
||
396 | |||
397 | /** |
||
398 | * check if value is a valid email |
||
399 | * |
||
400 | * @param string $email |
||
401 | * @return bool |
||
402 | */ |
||
403 | private function email($email){ |
||
406 | |||
407 | /** *********************************************** **/ |
||
408 | /** ************ Database Validations *********** **/ |
||
409 | /** *********************************************** **/ |
||
410 | |||
411 | /** |
||
412 | * check if a value of a column is unique. |
||
413 | * |
||
414 | * @param string $value |
||
415 | * @param array $args(table, column) |
||
416 | * @return bool |
||
417 | */ |
||
418 | private function unique($value, $args){ |
||
431 | |||
432 | /** |
||
433 | * check if email is unique |
||
434 | * This will check if email exists and activated. |
||
435 | * |
||
436 | * @param string $email |
||
437 | * @return bool |
||
438 | */ |
||
439 | private function emailUnique($email){ |
||
477 | |||
478 | /** *********************************************** **/ |
||
479 | /** ************ Login Validations *********** **/ |
||
480 | /** *********************************************** **/ |
||
481 | |||
482 | /** |
||
483 | * check if user credentials are valid or not. |
||
484 | * |
||
485 | * @param array $user |
||
486 | * @return bool |
||
487 | * @see Login::doLogin() |
||
488 | */ |
||
489 | private function credentials($user){ |
||
495 | |||
496 | /** |
||
497 | * check if user has exceeded number of failed logins or number of forgotten password attempts. |
||
498 | * |
||
499 | * @param array $attempts |
||
500 | * @return bool |
||
501 | */ |
||
502 | private function attempts($attempts){ |
||
528 | |||
529 | /** *********************************************** **/ |
||
530 | /** ************ File Validations *********** **/ |
||
531 | /** *********************************************** **/ |
||
532 | |||
533 | /** |
||
534 | * checks if file unique. |
||
535 | * |
||
536 | * @param array $path |
||
537 | * @return bool |
||
538 | * |
||
539 | * @see |
||
540 | */ |
||
541 | private function fileUnique($path){ |
||
544 | |||
545 | /** |
||
546 | * checks for file errors |
||
547 | * |
||
548 | * @param array $file |
||
549 | * @return bool |
||
550 | */ |
||
551 | private function fileErrors($file){ |
||
554 | |||
555 | /** |
||
556 | * checks if file uploaded successfully via HTTP POST |
||
557 | * |
||
558 | * @param array $file |
||
559 | * @return bool |
||
560 | * |
||
561 | * @see |
||
562 | */ |
||
563 | private function fileUploaded($file){ |
||
566 | |||
567 | /** |
||
568 | * checks from file size |
||
569 | * |
||
570 | * @param array $file |
||
571 | * @param array $args(min,max) |
||
572 | * @return bool |
||
573 | */ |
||
574 | private function fileSize($file, $args){ |
||
592 | |||
593 | /** |
||
594 | * checks from image size(dimensions) |
||
595 | * |
||
596 | * @param array $file |
||
597 | * @param array $dimensions(width,height) |
||
598 | * @return bool |
||
599 | */ |
||
600 | private function imageSize($file, $dimensions){ |
||
615 | |||
616 | /** |
||
617 | * validate mime type |
||
618 | * |
||
619 | * @param array $file |
||
620 | * @param array $mimeTypes |
||
621 | * @throws Exception if finfo_open() doesn't exists |
||
622 | * @return bool |
||
623 | */ |
||
624 | View Code Duplication | private function mimeType($file, $mimeTypes){ |
|
642 | |||
643 | /** |
||
644 | * validate file extension returned from pathinfo() Vs mapped mime type to extension |
||
645 | * |
||
646 | * This reveal un desired errors in case of files with extension: zip, csv, ..etc |
||
647 | * |
||
648 | * @param array $file |
||
649 | * @param array $extension |
||
650 | * @return bool |
||
651 | */ |
||
652 | private function fileExtension($file, $extension){ |
||
659 | |||
660 | /** *********************************************** **/ |
||
661 | /** ************ Default Messages *********** **/ |
||
662 | /** *********************************************** **/ |
||
663 | |||
664 | /** |
||
665 | * get default message for a rule |
||
666 | * |
||
667 | * Instead of passing your custom message every time, |
||
668 | * you can define a set of default messages. |
||
669 | * |
||
670 | * The pitfall of this method is, if you changed the validation method name, |
||
671 | * you need to change it here as well. |
||
672 | * |
||
673 | * @param string $rule |
||
674 | * @return mixed |
||
675 | */ |
||
676 | private static function defaultMessages($rule){ |
||
705 | } |
||
706 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.