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 VERSION = '0.1'; |
||
23 | const ITEM_PER_PAGE = 10; |
||
24 | |||
25 | public $type = 'app'; |
||
26 | |||
27 | /** |
||
28 | * List feedback post messages with notifications |
||
29 | * @return string |
||
30 | * @throws \Ffcms\Core\Exception\NativeException |
||
31 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
32 | */ |
||
33 | View Code Duplication | public function actionIndex() |
|
|
|||
34 | { |
||
35 | // set current page and offset |
||
36 | $page = (int)$this->request->query->get('page'); |
||
37 | $offset = $page * self::ITEM_PER_PAGE; |
||
38 | |||
39 | // get feedback posts AR table |
||
40 | $query = new FeedbackPost(); |
||
41 | |||
42 | // build pagination |
||
43 | $pagination = new SimplePagination([ |
||
44 | 'url' => ['feedback/index'], |
||
45 | 'page' => $page, |
||
46 | 'step' => self::ITEM_PER_PAGE, |
||
47 | 'total' => $query->count() |
||
48 | ]); |
||
49 | |||
50 | // build listing objects |
||
51 | $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
||
52 | |||
53 | // render output |
||
54 | return $this->view->render('index', [ |
||
55 | 'records' => $records, |
||
56 | 'pagination' => $pagination |
||
57 | ]); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Read feedback post and answer and add answer to thread post |
||
62 | * @param int $id |
||
63 | * @return string |
||
64 | * @throws \Ffcms\Core\Exception\NativeException |
||
65 | * @throws NotFoundException |
||
66 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
67 | */ |
||
68 | public function actionRead($id) |
||
96 | |||
97 | /** |
||
98 | * Edit feedback post or answer |
||
99 | * @param string $type |
||
100 | * @param int $id |
||
101 | * @return string |
||
102 | * @throws \Ffcms\Core\Exception\NativeException |
||
103 | * @throws NotFoundException |
||
104 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
105 | */ |
||
106 | public function actionUpdate($type, $id) |
||
107 | { |
||
108 | // get active record based on type (post or answer for post) |
||
109 | $record = null; |
||
110 | $postId = $id; |
||
111 | switch ($type) { |
||
112 | case 'post': |
||
113 | $record = FeedbackPost::find($id); |
||
114 | break; |
||
115 | case 'answer': |
||
116 | $record = FeedbackAnswer::find($id); |
||
117 | if ($record !== null && $record !== false) { |
||
118 | $postId = (int)$record->getFeedbackPost()->id; |
||
119 | } |
||
120 | break; |
||
121 | } |
||
122 | |||
123 | // try what we got |
||
124 | View Code Duplication | if ($record === null || $record === false) { |
|
125 | throw new NotFoundException(__('Feedback item is not founded')); |
||
126 | } |
||
127 | |||
128 | // initialize model |
||
129 | $model = new FormUpdate($record); |
||
130 | if ($model->send()) { |
||
131 | if ($model->validate()) { |
||
132 | $model->make(); |
||
133 | App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed')); |
||
134 | $this->response->redirect('feedback/read/' . $postId); |
||
135 | } else { |
||
136 | App::$Session->getFlashBag()->add('danger', __('Updating is failed')); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | // render output view |
||
141 | return $this->view->render('update', [ |
||
142 | 'model' => $model->filter() |
||
143 | ]); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Turn feedback request post - close, open, readed |
||
148 | * @param string $direction |
||
149 | * @param int $id |
||
150 | * @return null |
||
151 | * @throws NotFoundException |
||
152 | */ |
||
153 | public function actionTurn($direction, $id) |
||
187 | |||
188 | /** |
||
189 | * Delete feedback post or answer |
||
190 | * @param string $type |
||
191 | * @param int $id |
||
192 | * @return string |
||
193 | * @throws \Ffcms\Core\Exception\NativeException |
||
194 | * @throws NotFoundException |
||
195 | * @throws \Exception |
||
196 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
197 | */ |
||
198 | public function actionDelete($type, $id) |
||
239 | |||
240 | /** |
||
241 | * Settings of feedback application |
||
242 | * @return string |
||
243 | * @throws \Ffcms\Core\Exception\NativeException |
||
244 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
245 | */ |
||
246 | View Code Duplication | public function actionSettings() |
|
269 | |||
270 | } |
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.