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 | abstract class CRUDController extends JSONController |
||
13 | { |
||
14 | /** |
||
15 | * Make sure that the data of a form is valid, only called when creating a |
||
16 | * new object |
||
17 | * @param Form $form The submitted form |
||
18 | * @return void |
||
19 | */ |
||
20 | 1 | protected function validateNew($form) |
|
23 | |||
24 | /** |
||
25 | * Make sure that the data of a form is valid, only called when editing an |
||
26 | * existing object |
||
27 | * @param Form $form The submitted form |
||
28 | * @param PermissionModel $model The model being edited |
||
29 | * @return void |
||
30 | */ |
||
31 | protected function validateEdit($form, $model) |
||
32 | { |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Make sure that the data of a form is valid |
||
37 | * @param Form $form The submitted form |
||
38 | * @return void |
||
39 | */ |
||
40 | protected function validate($form) |
||
43 | |||
44 | /** |
||
45 | * Delete a model |
||
46 | * @param PermissionModel $model The model we want to delete |
||
47 | * @param Player $me The user who wants to delete the model |
||
48 | * @param Closure|null $onSuccess Something to do when the model is deleted |
||
49 | * @throws ForbiddenException |
||
50 | * @return mixed The response to show to the user |
||
51 | */ |
||
52 | 1 | protected function delete(PermissionModel $model, Player $me, $onSuccess = null) |
|
98 | |||
99 | protected function restore(PermissionModel $model, Player $me, $onSuccess) |
||
124 | |||
125 | 1 | /** |
|
126 | * Create a model |
||
127 | * |
||
128 | * This method requires that you have implemented enter() and a form creator |
||
129 | 1 | * for the model |
|
130 | * |
||
131 | * @param Player $me The user who wants to create the model |
||
132 | * @param Closure|null $onSuccess The function to call on success |
||
133 | * @throws ForbiddenException |
||
134 | * @return mixed The response to show to the user |
||
135 | */ |
||
136 | protected function create(Player $me, $onSuccess = null) |
||
166 | |||
167 | /** |
||
168 | * Edit a model |
||
169 | * |
||
170 | * This method requires that you have implemented update() and a form creator |
||
171 | * for the model |
||
172 | * |
||
173 | * @param PermissionModel $model The model we want to edit |
||
174 | * @param Player $me The user who wants to edit the model |
||
175 | * @param string $type The name of the variable to pass to the view |
||
176 | 1 | * @throws ForbiddenException |
|
177 | * @return mixed The response to show to the user |
||
178 | 1 | */ |
|
179 | protected function edit(PermissionModel $model, Player $me, $type) |
||
202 | |||
203 | /** |
||
204 | * Find whether a player can delete a model |
||
205 | * |
||
206 | * @param Player $player The player who wants to delete the model |
||
207 | * @param PermissionModel $model The model that will be deleted |
||
208 | * @param bool $hard Whether to hard-delete the model instead of soft-deleting it |
||
209 | * @return bool |
||
210 | */ |
||
211 | protected function canDelete($player, $model, $hard = false) |
||
215 | 1 | ||
216 | /** |
||
217 | 1 | * Find whether a player can create a model |
|
218 | 1 | * |
|
219 | * @param Player $player The player who wants to create a model |
||
220 | * @return bool |
||
221 | */ |
||
222 | protected function canCreate($player) |
||
228 | |||
229 | /** |
||
230 | 1 | * Find whether a player can edit a model |
|
231 | * |
||
232 | 1 | * @param Player $player The player who wants to delete the model |
|
233 | 1 | * @param PermissionModel $model The model which will be edited |
|
234 | * @return bool |
||
235 | 1 | */ |
|
236 | protected function canEdit($player, $model) |
||
240 | |||
241 | /** |
||
242 | * Get a redirection response to a model |
||
243 | * |
||
244 | 1 | * Goes to a list of models of the same type if the provided model does not |
|
245 | * have a URL |
||
246 | 1 | * |
|
247 | 1 | * @param ModelInterface $model The model to redirect to |
|
248 | * @return Response |
||
249 | 1 | */ |
|
250 | 1 | protected function redirectTo($model) |
|
258 | |||
259 | /** |
||
260 | * Get a redirection response to a list of models |
||
261 | * |
||
262 | * @param ModelInterface $model The model to whose list we should redirect |
||
263 | 1 | * @return Response |
|
264 | */ |
||
265 | 1 | protected function redirectToList($model) |
|
272 | 1 | ||
273 | /** |
||
274 | * Dynamically get the form to show to the user |
||
275 | 1 | * |
|
276 | * @param \Model|null $model The model being edited, `null` if we're creating one |
||
277 | 1 | * @return ModelFormCreator |
|
278 | */ |
||
279 | private function getFormCreator($model = null) |
||
289 | |||
290 | /** |
||
291 | * Get a message to show to the user |
||
292 | * @todo Use the $escape parameter |
||
293 | * @param \ModelInterface|string $model The model (or type) to show a message for |
||
294 | * @param string $action The action that will be performed (softDelete, hardDelete, create or edit) |
||
295 | * @param string $status The message's status (confirm, error or success) |
||
296 | 1 | * @return string |
|
297 | */ |
||
298 | private function getMessage($model, $action, $status, $escape = true) |
||
324 | 1 | ||
325 | 1 | /** |
|
326 | * Get a list of messages to show to the user |
||
327 | * @param string $type The type of the model that the message refers to |
||
328 | 1 | * @param string $name The name of the model |
|
329 | 1 | * @return array |
|
330 | */ |
||
331 | protected function getMessages($type, $name = '') |
||
404 | } |
||
405 |
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.