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 |
||
11 | class UpdateForm extends FormModel |
||
12 | { |
||
13 | /** |
||
14 | * Constructor injects with DI container and the id to update. |
||
15 | * |
||
16 | * @param Psr\Container\ContainerInterface $di a service container |
||
17 | * @param integer $id to update |
||
18 | */ |
||
19 | public function __construct(ContainerInterface $di, $id) |
||
65 | |||
66 | |||
67 | |||
68 | /** |
||
69 | * Get details on item to load form with. |
||
70 | * |
||
71 | * @param integer $id get details on item with id. |
||
72 | * |
||
73 | * @return User |
||
74 | */ |
||
75 | public function getItemDetails($id) : object |
||
82 | |||
83 | |||
84 | |||
85 | /** |
||
86 | * Callback for submit-button which should return true if it could |
||
87 | * carry out its work and false if something failed. |
||
88 | * |
||
89 | * @return bool true if okey, false if something went wrong. |
||
90 | */ |
||
91 | View Code Duplication | public function callbackSubmit() : bool |
|
102 | |||
103 | |||
104 | |||
105 | /** |
||
106 | * Callback what to do if the form was successfully submitted, this |
||
107 | * happen when the submit callback method returns true. This method |
||
108 | * can/should be implemented by the subclass for a different behaviour. |
||
109 | */ |
||
110 | public function callbackSuccess() |
||
115 | |||
116 | |||
117 | |||
118 | // /** |
||
119 | // * Callback what to do if the form was unsuccessfully submitted, this |
||
120 | // * happen when the submit callback method returns false or if validation |
||
121 | // * fails. This method can/should be implemented by the subclass for a |
||
122 | // * different behaviour. |
||
123 | // */ |
||
124 | // public function callbackFail() |
||
125 | // { |
||
126 | // $this->di->get("response")->redirectSelf()->send(); |
||
127 | // } |
||
128 | } |
||
129 |
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.