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 UpdateCommForm 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 | 2 | public function __construct(DIInterface $di, $id, $sessid) |
|
28 | |||
29 | |||
30 | /** |
||
31 | * Converts json-string back to variables |
||
32 | * |
||
33 | * @param string $fromjson the jsoncode |
||
34 | * @return the extracted comment-text |
||
35 | */ |
||
36 | 2 | public function decode($fromjson) |
|
44 | |||
45 | /** |
||
46 | * Create the form. |
||
47 | * |
||
48 | */ |
||
49 | 2 | public function aForm($id, $sessid, $comm, $comt) |
|
63 | |||
64 | |||
65 | |||
66 | /** |
||
67 | * Get details on item to load form with. |
||
68 | * |
||
69 | * @param integer $id get details on item with id. |
||
70 | * |
||
71 | * @return Comm |
||
72 | */ |
||
73 | 2 | View Code Duplication | public function getCommDetails($id) |
80 | |||
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() |
||
122 | } |
||
123 |
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.