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 DeleteForm extends FormModel |
||
13 | { |
||
14 | /** |
||
15 | * Constructor injects with DI container. |
||
16 | * |
||
17 | * @param Anax\DI\DIInterface $di a service container |
||
18 | */ |
||
19 | View Code Duplication | public function __construct(DIInterface $di, $userId) |
|
42 | |||
43 | |||
44 | |||
45 | /** |
||
46 | * Get all items as array suitable for display in select option dropdown. |
||
47 | * |
||
48 | * @return array with key value of all items. |
||
49 | */ |
||
50 | protected function getAllItems($userId) |
||
62 | |||
63 | |||
64 | |||
65 | /** |
||
66 | * Callback for submit-button which should return true if it could |
||
67 | * carry out its work and false if something failed. |
||
68 | * |
||
69 | * @return boolean true if okey, false if something went wrong. |
||
70 | */ |
||
71 | View Code Duplication | public function callbackSubmit() |
|
80 | } |
||
81 |
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.