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 |
||
17 | class AddressingAccountValidator |
||
18 | { |
||
19 | /** @var AddressingAccount */ |
||
20 | private $addressingAccount; |
||
21 | |||
22 | /** |
||
23 | * @param AddressingAccount $addressingAccount |
||
24 | */ |
||
25 | public function __construct(AddressingAccount $addressingAccount) |
||
29 | |||
30 | /** |
||
31 | * Validate the attributes of the Addressing Account class |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function validate(): void |
||
41 | |||
42 | /** |
||
43 | * This validates a branch account |
||
44 | * |
||
45 | * @return void |
||
46 | * @throws \InvalidArgumentException |
||
47 | */ |
||
48 | private function validateBranch() |
||
55 | |||
56 | /** |
||
57 | * This validates a number account |
||
58 | * |
||
59 | * @return void |
||
60 | * @throws \InvalidArgumentException |
||
61 | */ |
||
62 | private function validateNumber() |
||
69 | |||
70 | /** |
||
71 | * This validates a type account |
||
72 | * |
||
73 | * @return void |
||
74 | * @throws \InvalidArgumentException |
||
75 | */ |
||
76 | View Code Duplication | private function validateType() |
|
92 | } |
||
93 |
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.