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 |
||
24 | class Comments extends AdminController |
||
25 | { |
||
26 | const VERSION = 0.1; |
||
27 | const ITEM_PER_PAGE = 10; |
||
28 | |||
29 | const TYPE_COMMENT = 'comment'; |
||
30 | const TYPE_ANSWER = 'answer'; |
||
31 | |||
32 | public $type = 'widget'; |
||
33 | |||
34 | /** |
||
35 | * List user comments with pagination |
||
36 | * @return string |
||
37 | * @throws \Ffcms\Core\Exception\NativeException |
||
38 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
39 | */ |
||
40 | View Code Duplication | public function actionIndex() |
|
|
|||
41 | { |
||
42 | // set current page and offset |
||
43 | $page = (int)$this->request->query->get('page'); |
||
44 | $offset = $page * self::ITEM_PER_PAGE; |
||
45 | |||
46 | // initialize active record model |
||
47 | $query = new CommentPost(); |
||
48 | |||
49 | // make pagination |
||
50 | $pagination = new SimplePagination([ |
||
51 | 'url' => ['comments/index'], |
||
52 | 'page' => $page, |
||
53 | 'step' => self::ITEM_PER_PAGE, |
||
54 | 'total' => $query->count() |
||
55 | ]); |
||
56 | |||
57 | // get result as active records object with offset |
||
58 | $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
||
59 | |||
60 | // render output view |
||
61 | return $this->view->render('index', [ |
||
62 | 'records' => $records, |
||
63 | 'pagination' => $pagination |
||
64 | ]); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * List comment - read comment and list answers |
||
69 | * @param int $id |
||
70 | * @return string |
||
71 | * @throws \Ffcms\Core\Exception\NativeException |
||
72 | * @throws NotFoundException |
||
73 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
74 | */ |
||
75 | public function actionRead($id) |
||
88 | |||
89 | /** |
||
90 | * Commentaries and answers edit action |
||
91 | * @param string $type |
||
92 | * @param int $id |
||
93 | * @throws NotFoundException |
||
94 | * @return string |
||
95 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
96 | * @throws \Ffcms\Core\Exception\NativeException |
||
97 | */ |
||
98 | public function actionEdit($type, $id) |
||
130 | |||
131 | /** |
||
132 | * Delete comments and answers single or multiply items |
||
133 | * @param string $type |
||
134 | * @param int $id |
||
135 | * @return string |
||
136 | * @throws NotFoundException |
||
137 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
138 | * @throws \Ffcms\Core\Exception\NativeException |
||
139 | */ |
||
140 | public function actionDelete($type, $id = 0) |
||
185 | |||
186 | /** |
||
187 | * Moderate guest comments and answer - make it publish |
||
188 | * @param string $type |
||
189 | * @param int $id |
||
190 | * @return string |
||
191 | * @throws NotFoundException |
||
192 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
193 | * @throws \Ffcms\Core\Exception\NativeException |
||
194 | */ |
||
195 | public function actionPublish($type, $id = 0) |
||
239 | |||
240 | /** |
||
241 | * List answers |
||
242 | * @return string |
||
243 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
244 | * @throws \Ffcms\Core\Exception\NativeException |
||
245 | */ |
||
246 | View Code Duplication | public function actionAnswerlist() |
|
272 | |||
273 | /** |
||
274 | * Comment widget global settings |
||
275 | * @return string |
||
276 | * @throws \Ffcms\Core\Exception\NativeException |
||
277 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
278 | */ |
||
279 | View Code Duplication | public function actionSettings() |
|
300 | |||
301 | |||
302 | |||
303 | |||
304 | } |
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.