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 |
||
20 | class Feedback extends Controller |
||
21 | { |
||
22 | const ITEM_PER_PAGE = 10; |
||
23 | |||
24 | /** |
||
25 | * This action is not allowed there |
||
26 | * @throws NotFoundException |
||
27 | */ |
||
28 | public function actionIndex() |
||
32 | |||
33 | /** |
||
34 | * Add new feedback message action |
||
35 | * @return string |
||
36 | * @throws \Ffcms\Core\Exception\NativeException |
||
37 | * @throws ForbiddenException |
||
38 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
39 | */ |
||
40 | public function actionCreate() |
||
41 | { |
||
42 | // get configs |
||
43 | $configs = $this->getConfigs(); |
||
44 | View Code Duplication | if (!App::$User->isAuth() && (int)$configs['guestAdd'] !== 1) { |
|
|
|||
45 | throw new ForbiddenException(__('Feedback available only for authorized users')); |
||
46 | } |
||
47 | |||
48 | // initialize model |
||
49 | $model = new FormFeedbackAdd((int)$configs['useCaptcha'] === 1); |
||
50 | if ($model->send()) { |
||
51 | if ($model->validate()) { |
||
52 | // if validation is passed save data to db and get row |
||
53 | $record = $model->make(); |
||
54 | App::$Session->getFlashBag()->add('success', __('Your message was added successful')); |
||
55 | $this->response->redirect('feedback/read/' . $record->id . '/' . $record->hash); |
||
56 | } else { |
||
57 | App::$Session->getFlashBag()->add('error', __('Message is not sended! Please, fix issues in form below')); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | // render output view |
||
62 | return $this->view->render('create', [ |
||
63 | 'model' => $model->filter(), |
||
64 | 'useCaptcha' => (int)$configs['useCaptcha'] === 1 |
||
65 | ]); |
||
66 | } |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Read feedback message and answers and work with add answer model |
||
71 | * @param int $id |
||
72 | * @param string $hash |
||
73 | * @return string |
||
74 | * @throws \Ffcms\Core\Exception\NativeException |
||
75 | * @throws ForbiddenException |
||
76 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
77 | */ |
||
78 | public function actionRead($id, $hash) |
||
116 | |||
117 | /** |
||
118 | * @param int $id |
||
119 | * @param string $hash |
||
120 | * @return string |
||
121 | * @throws \Ffcms\Core\Exception\NativeException |
||
122 | * @throws ForbiddenException |
||
123 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
124 | */ |
||
125 | public function actionClose($id, $hash) |
||
160 | |||
161 | /** |
||
162 | * List feedback requests messages from authorized user |
||
163 | * @return string |
||
164 | * @throws \Ffcms\Core\Exception\NativeException |
||
165 | * @throws ForbiddenException |
||
166 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
167 | */ |
||
168 | public function actionList() |
||
202 | |||
203 | } |
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.