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 | View Code Duplication | class AdminUpdateUser extends FormModel |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * Constructor injects with DI container and the id to update. |
||
16 | * |
||
17 | * @param Anax\DI\DIInterface $di a service container |
||
18 | * @param integer $id to update |
||
19 | */ |
||
20 | public function __construct(DIInterface $di, $id) |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Get details on item to load form with. |
||
69 | * |
||
70 | * @param integer $id get details on item with id. |
||
71 | * |
||
72 | * @return $user |
||
73 | */ |
||
74 | public function getItemDetails($id) |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Callback for submit-button which should return true if it could |
||
85 | * carry out its work and false if something failed. |
||
86 | * |
||
87 | * @return boolean true if okey, false if something went wrong. |
||
88 | */ |
||
89 | public function callbackSubmit() |
||
101 | } |
||
102 |
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.