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 IsoCodesValidator 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 IsoCodesValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class IsoCodesValidator extends BaseValidator |
||
11 | { |
||
12 | /** |
||
13 | * Validate a BBAN code |
||
14 | * |
||
15 | * @param $attribute |
||
16 | * @param $value |
||
17 | * @param $parameters |
||
18 | * @return mixed |
||
19 | */ |
||
20 | public function validateBban($attribute, $value, $parameters) |
||
24 | |||
25 | /** |
||
26 | * Validate a BSN (Dutch citizen service number) |
||
27 | * |
||
28 | * @param $attribute |
||
29 | * @param $value |
||
30 | * @param $parameters |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function validateBsn($attribute, $value, $parameters) |
||
37 | |||
38 | /** |
||
39 | * Validate a CIF code |
||
40 | * |
||
41 | * @param $attribute |
||
42 | * @param $value |
||
43 | * @param $parameters |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public function validateCif($attribute, $value, $parameters) |
||
50 | |||
51 | /** |
||
52 | * Validate a credit card number |
||
53 | * |
||
54 | * @param $attribute |
||
55 | * @param $value |
||
56 | * @param $parameters |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function validateCreditcard($attribute, $value, $parameters) |
||
63 | |||
64 | /** |
||
65 | * Validate a EAN-8 code |
||
66 | * |
||
67 | * @param $attribute |
||
68 | * @param $value |
||
69 | * @param $parameters |
||
70 | * @return mixed |
||
71 | */ |
||
72 | public function validateEan8($attribute, $value, $parameters) |
||
76 | |||
77 | /** |
||
78 | * Validate a EAN-13 code |
||
79 | * |
||
80 | * @param $attribute |
||
81 | * @param $value |
||
82 | * @param $parameters |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function validateEan13($attribute, $value, $parameters) |
||
89 | |||
90 | /** |
||
91 | * Validate a GTIN-8 code |
||
92 | * |
||
93 | * @param $attribute |
||
94 | * @param $value |
||
95 | * @param $parameters |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function validateGtin8($attribute, $value, $parameters) |
||
102 | |||
103 | /** |
||
104 | * Validate a GTIN-12 code |
||
105 | * |
||
106 | * @param $attribute |
||
107 | * @param $value |
||
108 | * @param $parameters |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function validateGtin12($attribute, $value, $parameters) |
||
115 | |||
116 | /** |
||
117 | * Validate a GTIN-13 code |
||
118 | * |
||
119 | * @param $attribute |
||
120 | * @param $value |
||
121 | * @param $parameters |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function validateGtin13($attribute, $value, $parameters) |
||
128 | |||
129 | /** |
||
130 | * Validate a GTIN-14 code |
||
131 | * |
||
132 | * @param $attribute |
||
133 | * @param $value |
||
134 | * @param $parameters |
||
135 | * @return mixed |
||
136 | */ |
||
137 | public function validateGtin14($attribute, $value, $parameters) |
||
141 | |||
142 | /** |
||
143 | * Validate an IBAN |
||
144 | * |
||
145 | * @param $attribute |
||
146 | * @param $value |
||
147 | * @param $parameters |
||
148 | * @return mixed |
||
149 | */ |
||
150 | public function validateIban($attribute, $value, $parameters) |
||
154 | |||
155 | /** |
||
156 | * Validate a "numéro de sécurité sociale" (INSEE) |
||
157 | * |
||
158 | * @param $attribute |
||
159 | * @param $value |
||
160 | * @param $parameters |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public function validateInsee($attribute, $value, $parameters) |
||
167 | |||
168 | public function validateIpaddress($attribute, $value, $parameters) |
||
172 | |||
173 | /** |
||
174 | * Validate an ISBN |
||
175 | * |
||
176 | * @param $attribute |
||
177 | * @param $value |
||
178 | * @param $parameters |
||
179 | * @return mixed |
||
180 | */ |
||
181 | public function validateIsbn($attribute, $value, $parameters) |
||
188 | |||
189 | /** |
||
190 | * Validate an "International Securities Identification Number" (ISIN) |
||
191 | * |
||
192 | * @param $attribute |
||
193 | * @param $value |
||
194 | * @param $parameters |
||
195 | * @return mixed |
||
196 | */ |
||
197 | public function validateIsin($attribute, $value, $parameters) |
||
201 | |||
202 | /** |
||
203 | * Validate a MAC address |
||
204 | * |
||
205 | * @param $attribute |
||
206 | * @param $value |
||
207 | * @param $parameters |
||
208 | * @return mixed |
||
209 | */ |
||
210 | public function validateMac($attribute, $value, $parameters) |
||
214 | |||
215 | /** |
||
216 | * Validate a "Número de Identificación Fiscal" (NIF) |
||
217 | * |
||
218 | * @param $attribute |
||
219 | * @param $value |
||
220 | * @param $parameters |
||
221 | * @return mixed |
||
222 | */ |
||
223 | public function validateNif($attribute, $value, $parameters) |
||
227 | |||
228 | /** |
||
229 | * Validate a "Organisme Type12 Norme B2" |
||
230 | * |
||
231 | * @param $attribute |
||
232 | * @param $value |
||
233 | * @param $parameters |
||
234 | * @return mixed |
||
235 | */ |
||
236 | View Code Duplication | public function validateOrganismeType12NormeB2($attribute, $value, $parameters) |
|
243 | |||
244 | /** |
||
245 | * Validate a phone number |
||
246 | * |
||
247 | * @param $attribute |
||
248 | * @param $value |
||
249 | * @param $parameters |
||
250 | * @return mixed |
||
251 | */ |
||
252 | View Code Duplication | public function validatePhonenumber($attribute, $value, $parameters) |
|
259 | |||
260 | /** |
||
261 | * Validate a Stock Exchange Daily Official List (SEDOL) |
||
262 | * |
||
263 | * @param $attribute |
||
264 | * @param $value |
||
265 | * @param $parameters |
||
266 | * @return mixed |
||
267 | */ |
||
268 | public function validateSedol($attribute, $value, $parameters) |
||
272 | |||
273 | /** |
||
274 | * Validate "Système d’Identification du Répertoire des Entreprises" (SIREN) |
||
275 | * |
||
276 | * @param $attribute |
||
277 | * @param $value |
||
278 | * @param $parameters |
||
279 | * @return mixed |
||
280 | */ |
||
281 | public function validateSiren($attribute, $value, $parameters) |
||
285 | |||
286 | /** |
||
287 | * Validate "Système d’Identification du Répertoire des ETablissements" (SIRET) |
||
288 | * |
||
289 | * @param $attribute |
||
290 | * @param $value |
||
291 | * @param $parameters |
||
292 | * @return mixed |
||
293 | */ |
||
294 | public function validateSiret($attribute, $value, $parameters) |
||
298 | |||
299 | /** |
||
300 | * Validate a European/International Article Number (SSCC) |
||
301 | * |
||
302 | * @param $attribute |
||
303 | * @param $value |
||
304 | * @param $parameters |
||
305 | * @return mixed |
||
306 | */ |
||
307 | public function validateSscc($attribute, $value, $parameters) |
||
311 | |||
312 | /** |
||
313 | * Validate a Social Security Number (SSN) |
||
314 | * |
||
315 | * @param $attribute |
||
316 | * @param $value |
||
317 | * @param $parameters |
||
318 | * @return mixed |
||
319 | */ |
||
320 | public function validateSsn($attribute, $value, $parameters) |
||
324 | |||
325 | /** |
||
326 | * Validate structured communication |
||
327 | * |
||
328 | * @param $attribute |
||
329 | * @param $value |
||
330 | * @param $parameters |
||
331 | * @return mixed |
||
332 | */ |
||
333 | public function validateStructuredCommunication($attribute, $value, $parameters) |
||
337 | |||
338 | /** |
||
339 | * Validate a SWIFT/BIC |
||
340 | * |
||
341 | * @param $attribute |
||
342 | * @param $value |
||
343 | * @param $parameters |
||
344 | * @return mixed |
||
345 | */ |
||
346 | public function validateSwiftBic($attribute, $value, $parameters) |
||
350 | |||
351 | /** |
||
352 | * Validate a UK National Insurance Number |
||
353 | * |
||
354 | * @param $attribute |
||
355 | * @param $value |
||
356 | * @param $parameters |
||
357 | * @return mixed |
||
358 | */ |
||
359 | public function validateUknin($attribute, $value, $parameters) |
||
363 | |||
364 | /** |
||
365 | * Validate a Universal Product Code |
||
366 | * |
||
367 | * @param $attribute |
||
368 | * @param $value |
||
369 | * @param $parameters |
||
370 | * @return mixed |
||
371 | */ |
||
372 | public function validateUpca($attribute, $value, $parameters) |
||
376 | |||
377 | /** |
||
378 | * Validate Value Added Tax (VAT) |
||
379 | * |
||
380 | * @param $attribute |
||
381 | * @param $value |
||
382 | * @param $parameters |
||
383 | * @return mixed |
||
384 | */ |
||
385 | public function validateVat($attribute, $value, $parameters) |
||
389 | |||
390 | /** |
||
391 | * Validate a zip code |
||
392 | * |
||
393 | * @param $attribute |
||
394 | * @param $value |
||
395 | * @param $parameters |
||
396 | * @return mixed |
||
397 | */ |
||
398 | View Code Duplication | public function validateZipcode($attribute, $value, $parameters) |
|
405 | |||
406 | /** |
||
407 | * Execute the validation function |
||
408 | * and catch every other Exception from underlying libraries |
||
409 | * so we will only display the Laravel validation error |
||
410 | * |
||
411 | * @param $validator |
||
412 | * @param $value |
||
413 | * @param string $reference |
||
414 | * @return mixed |
||
415 | */ |
||
416 | protected function runIsoCodesValidator($validator, $value, $reference = '') |
||
428 | |||
429 | /** |
||
430 | * Replace all country place-holders |
||
431 | * |
||
432 | * @param $message |
||
433 | * @param $parameter |
||
434 | * @return mixed |
||
435 | */ |
||
436 | protected function countryReplacer($message, $parameter) |
||
440 | |||
441 | /** |
||
442 | * Replace all value place-holders |
||
443 | * |
||
444 | * @param $message |
||
445 | * @param $attribute |
||
446 | * @return mixed |
||
447 | */ |
||
448 | protected function valueReplacer($message, $attribute) |
||
452 | |||
453 | /** |
||
454 | * Replace all place-holders for the bban rule |
||
455 | * |
||
456 | * @param $message |
||
457 | * @param $attribute |
||
458 | * @param $rule |
||
459 | * @param $parameter |
||
460 | * @return mixed |
||
461 | */ |
||
462 | public function replaceBban($message, $attribute, $rule, $parameter) |
||
466 | |||
467 | /** |
||
468 | * Replace all place-holders for the bsn rule |
||
469 | * |
||
470 | * @param $message |
||
471 | * @param $attribute |
||
472 | * @param $rule |
||
473 | * @param $parameter |
||
474 | * @return mixed |
||
475 | */ |
||
476 | public function replaceBsn($message, $attribute, $rule, $parameter) |
||
480 | |||
481 | /** |
||
482 | * Replace all place-holders for the cif rule |
||
483 | * |
||
484 | * @param $message |
||
485 | * @param $attribute |
||
486 | * @param $rule |
||
487 | * @param $parameter |
||
488 | * @return mixed |
||
489 | */ |
||
490 | public function replaceCif($message, $attribute, $rule, $parameter) |
||
494 | |||
495 | /** |
||
496 | * Replace all place-holders for the creditcard rule |
||
497 | * |
||
498 | * @param $message |
||
499 | * @param $attribute |
||
500 | * @param $rule |
||
501 | * @param $parameter |
||
502 | * @return mixed |
||
503 | */ |
||
504 | public function replaceCreditcard($message, $attribute, $rule, $parameter) |
||
508 | |||
509 | /** |
||
510 | * Replace all place-holders for the ena8 rule |
||
511 | * |
||
512 | * @param $message |
||
513 | * @param $attribute |
||
514 | * @param $rule |
||
515 | * @param $parameter |
||
516 | * @return mixed |
||
517 | */ |
||
518 | public function replaceEan8($message, $attribute, $rule, $parameter) |
||
522 | |||
523 | /** |
||
524 | * Replace all place-holders for the ean13 rule |
||
525 | * |
||
526 | * @param $message |
||
527 | * @param $attribute |
||
528 | * @param $rule |
||
529 | * @param $parameter |
||
530 | * @return mixed |
||
531 | */ |
||
532 | public function replaceEan13($message, $attribute, $rule, $parameter) |
||
536 | |||
537 | /** |
||
538 | * Replace all place-holders for the gitin8 rule |
||
539 | * |
||
540 | * @param $message |
||
541 | * @param $attribute |
||
542 | * @param $rule |
||
543 | * @param $parameter |
||
544 | * @return mixed |
||
545 | */ |
||
546 | public function replaceGtin8($message, $attribute, $rule, $parameter) |
||
550 | |||
551 | /** |
||
552 | * Replace all place-holders for the gtin12 rule |
||
553 | * |
||
554 | * @param $message |
||
555 | * @param $attribute |
||
556 | * @param $rule |
||
557 | * @param $parameter |
||
558 | * @return mixed |
||
559 | */ |
||
560 | public function replaceGtin12($message, $attribute, $rule, $parameter) |
||
564 | |||
565 | /** |
||
566 | * Replace all place-holders for the gtin13 rule |
||
567 | * |
||
568 | * @param $message |
||
569 | * @param $attribute |
||
570 | * @param $rule |
||
571 | * @param $parameter |
||
572 | * @return mixed |
||
573 | */ |
||
574 | public function replaceGtin13($message, $attribute, $rule, $parameter) |
||
578 | |||
579 | /** |
||
580 | * Replace all place-holders for the gtin14 rule |
||
581 | * |
||
582 | * @param $message |
||
583 | * @param $attribute |
||
584 | * @param $rule |
||
585 | * @param $parameter |
||
586 | * @return mixed |
||
587 | */ |
||
588 | public function replaceGtin14($message, $attribute, $rule, $parameter) |
||
592 | |||
593 | /** |
||
594 | * Replace all place-holders for the iban rule |
||
595 | * |
||
596 | * @param $message |
||
597 | * @param $attribute |
||
598 | * @param $rule |
||
599 | * @param $parameter |
||
600 | * @return mixed |
||
601 | */ |
||
602 | public function replaceIban($message, $attribute, $rule, $parameter) |
||
606 | |||
607 | /** |
||
608 | * Replace all place-holders for the insee rule |
||
609 | * |
||
610 | * @param $message |
||
611 | * @param $attribute |
||
612 | * @param $rule |
||
613 | * @param $parameter |
||
614 | * @return mixed |
||
615 | */ |
||
616 | public function replaceInsee($message, $attribute, $rule, $parameter) |
||
620 | |||
621 | /** |
||
622 | * Replace all place-holders for the ipaddress rule |
||
623 | * |
||
624 | * @param $message |
||
625 | * @param $attribute |
||
626 | * @param $rule |
||
627 | * @param $parameter |
||
628 | * @return mixed |
||
629 | */ |
||
630 | public function replaceIpaddress($message, $attribute, $rule, $parameter) |
||
634 | |||
635 | /** |
||
636 | * Replace all place-holders for the isbn rule |
||
637 | * |
||
638 | * @param $message |
||
639 | * @param $attribute |
||
640 | * @param $rule |
||
641 | * @param $parameter |
||
642 | * @return mixed |
||
643 | */ |
||
644 | public function replaceIsbn($message, $attribute, $rule, $parameter) |
||
645 | { |
||
646 | return $this->valueReplacer($message, $attribute); |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * Replace all place-holders for the isin rule |
||
651 | * |
||
652 | * @param $message |
||
653 | * @param $attribute |
||
654 | * @param $rule |
||
655 | * @param $parameter |
||
656 | * @return mixed |
||
657 | */ |
||
658 | public function replaceIsin($message, $attribute, $rule, $parameter) |
||
662 | |||
663 | /** |
||
664 | * Replace all place-holders for the mac rule |
||
665 | * |
||
666 | * @param $message |
||
667 | * @param $attribute |
||
668 | * @param $rule |
||
669 | * @param $parameter |
||
670 | * @return mixed |
||
671 | */ |
||
672 | public function replaceMac($message, $attribute, $rule, $parameter) |
||
676 | |||
677 | /** |
||
678 | * Replace all place-holders for the nif rule |
||
679 | * |
||
680 | * @param $message |
||
681 | * @param $attribute |
||
682 | * @param $rule |
||
683 | * @param $parameter |
||
684 | * @return mixed |
||
685 | */ |
||
686 | public function replaceNif($message, $attribute, $rule, $parameter) |
||
690 | |||
691 | /** |
||
692 | * Replace all place-holders for the organisme_type12_norme_b2 rule |
||
693 | * |
||
694 | * @param $message |
||
695 | * @param $attribute |
||
696 | * @param $rule |
||
697 | * @param $parameter |
||
698 | * @return mixed |
||
699 | */ |
||
700 | public function replaceOrganismeType12NormeB2($message, $attribute, $rule, $parameter) |
||
704 | |||
705 | /** |
||
706 | * Replace all place-holders for the phonenumber rule |
||
707 | * |
||
708 | * @param $message |
||
709 | * @param $attribute |
||
710 | * @param $rule |
||
711 | * @param $parameter |
||
712 | * @return mixed |
||
713 | */ |
||
714 | protected function replacePhonenumber($message, $attribute, $rule, $parameter) |
||
721 | |||
722 | /** |
||
723 | * Replace all place-holders for the sedol rule |
||
724 | * |
||
725 | * @param $message |
||
726 | * @param $attribute |
||
727 | * @param $rule |
||
728 | * @param $parameter |
||
729 | * @return mixed |
||
730 | */ |
||
731 | public function replaceSedol($message, $attribute, $rule, $parameter) |
||
735 | |||
736 | /** |
||
737 | * Replace all place-holders for the siren rule |
||
738 | * |
||
739 | * @param $message |
||
740 | * @param $attribute |
||
741 | * @param $rule |
||
742 | * @param $parameter |
||
743 | * @return mixed |
||
744 | */ |
||
745 | public function replaceSiren($message, $attribute, $rule, $parameter) |
||
749 | |||
750 | /** |
||
751 | * Replace all place-holders for the siret rule |
||
752 | * |
||
753 | * @param $message |
||
754 | * @param $attribute |
||
755 | * @param $rule |
||
756 | * @param $parameter |
||
757 | * @return mixed |
||
758 | */ |
||
759 | public function replaceSiret($message, $attribute, $rule, $parameter) |
||
763 | |||
764 | /** |
||
765 | * Replace all place-holders for the sscc rule |
||
766 | * |
||
767 | * @param $message |
||
768 | * @param $attribute |
||
769 | * @param $rule |
||
770 | * @param $parameter |
||
771 | * @return mixed |
||
772 | */ |
||
773 | public function replaceSscc($message, $attribute, $rule, $parameter) |
||
777 | |||
778 | /** |
||
779 | * Replace all place-holders for the ssn rule |
||
780 | * |
||
781 | * @param $message |
||
782 | * @param $attribute |
||
783 | * @param $rule |
||
784 | * @param $parameter |
||
785 | * @return mixed |
||
786 | */ |
||
787 | public function replaceSSn($message, $attribute, $rule, $parameter) |
||
791 | |||
792 | /** |
||
793 | * Replace all place-holders for the structured_communication rule |
||
794 | * |
||
795 | * @param $message |
||
796 | * @param $attribute |
||
797 | * @param $rule |
||
798 | * @param $parameter |
||
799 | * @return mixed |
||
800 | */ |
||
801 | public function replaceStructuredCommunication($message, $attribute, $rule, $parameter) |
||
805 | |||
806 | /** |
||
807 | * Replace all place-holders for the swift_bic rule |
||
808 | * |
||
809 | * @param $message |
||
810 | * @param $attribute |
||
811 | * @param $rule |
||
812 | * @param $parameter |
||
813 | * @return mixed |
||
814 | */ |
||
815 | public function replaceSwiftBic($message, $attribute, $rule, $parameter) |
||
819 | |||
820 | /** |
||
821 | * Replace all place-holders for the zipcode rule |
||
822 | * |
||
823 | * @param $message |
||
824 | * @param $attribute |
||
825 | * @param $rule |
||
826 | * @param $parameter |
||
827 | * @return mixed |
||
828 | */ |
||
829 | public function replaceZipcode($message, $attribute, $rule, $parameter) |
||
836 | } |
||
837 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.