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:
1 | <?php |
||
19 | class DuplicateCardValidator |
||
20 | { |
||
21 | /** |
||
22 | * @var Duplicate |
||
23 | */ |
||
24 | private $duplicateCard; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $status = [ |
||
30 | 'LostMyCard', |
||
31 | 'CardWasStolen', |
||
32 | 'CardWasDamaged', |
||
33 | 'CardNotDelivered', |
||
34 | 'UnrecognizedOnlinePurchase' |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * DuplicateCardValidator constructor. |
||
39 | * |
||
40 | * @param Duplicate $duplicateCard |
||
41 | */ |
||
42 | public function __construct(Duplicate $duplicateCard) |
||
46 | |||
47 | /** |
||
48 | * This validate the duplicate card data |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | public function validate(): void |
||
59 | |||
60 | /** |
||
61 | * This validate duplicate card status |
||
62 | * |
||
63 | * @return void |
||
64 | * @throws \InvalidArgumentException |
||
65 | */ |
||
66 | public function validateStatus() |
||
75 | |||
76 | /** |
||
77 | * This validate a duplicate card document |
||
78 | * |
||
79 | * @return void |
||
80 | * @throws \InvalidArgumentException |
||
81 | */ |
||
82 | View Code Duplication | private function validateDocumentNumber() |
|
92 | |||
93 | /** |
||
94 | * This validate a duplicate card password |
||
95 | * |
||
96 | * @return void |
||
97 | * @throws \InvalidArgumentException |
||
98 | */ |
||
99 | private function validatePassword() |
||
106 | |||
107 | /** |
||
108 | * This validate a duplicate card address |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | private function validateAddress() |
||
119 | } |
||
120 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.