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 |
||
12 | class DeleteUserForm extends FormModel |
||
13 | { |
||
14 | /** |
||
15 | * Constructor injects with DI container. |
||
16 | * |
||
17 | * @param Anax\DI\DIInterface $di a service container |
||
18 | */ |
||
19 | 2 | public function __construct(DIInterface $di) |
|
47 | |||
48 | |||
49 | |||
50 | /** |
||
51 | * Get all items as array suitable for display in select option dropdown. |
||
52 | * |
||
53 | * @return array with key value of all items. |
||
54 | */ |
||
55 | // protected function getAllItems() |
||
56 | 2 | public function getAllItems() |
|
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * Callback for submit-button which should return true if it could |
||
74 | * carry out its work and false if something failed. |
||
75 | * |
||
76 | * @return boolean true if okey, false if something went wrong. |
||
77 | */ |
||
78 | View Code Duplication | public function callbackSubmit() |
|
88 | |||
89 | /** |
||
90 | * Callback for submit-button which should return true if it could |
||
91 | * carry out its work and false if something failed. |
||
92 | * |
||
93 | * @return boolean true if okey, false if something went wrong. |
||
94 | */ |
||
95 | View Code Duplication | public function callbackSubmit2() |
|
105 | } |
||
106 |
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.