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 |
||
17 | class Profile extends AdminController |
||
18 | { |
||
19 | const VERSION = 0.1; |
||
20 | const ITEM_PER_PAGE = 10; |
||
21 | |||
22 | public $type = 'app'; |
||
23 | |||
24 | /** |
||
25 | * List all profiles in website with pagination |
||
26 | * @return string |
||
27 | * @throws \Ffcms\Core\Exception\NativeException |
||
28 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
29 | */ |
||
30 | View Code Duplication | public function actionIndex() |
|
|
|||
31 | { |
||
32 | // init Active Record |
||
33 | $query = new ProfileRecords(); |
||
34 | |||
35 | // set current page and offset |
||
36 | $page = (int)$this->request->query->get('page'); |
||
37 | $offset = $page * self::ITEM_PER_PAGE; |
||
38 | |||
39 | // build pagination |
||
40 | $pagination = new SimplePagination([ |
||
41 | 'url' => ['profile/index'], |
||
42 | 'page' => $page, |
||
43 | 'step' => self::ITEM_PER_PAGE, |
||
44 | 'total' => $query->count() |
||
45 | ]); |
||
46 | |||
47 | // build listing objects |
||
48 | $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
||
49 | |||
50 | // display viewer |
||
51 | return $this->view->render('index', [ |
||
52 | 'records' => $records, |
||
53 | 'pagination' => $pagination |
||
54 | ]); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Redirect to user controller |
||
59 | * @param $id |
||
60 | */ |
||
61 | public function actionDelete($id) |
||
65 | |||
66 | /** |
||
67 | * Profile edit action |
||
68 | * @param int $id |
||
69 | * @return string |
||
70 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
71 | * @throws \Ffcms\Core\Exception\NativeException |
||
72 | * @throws NotFoundException |
||
73 | */ |
||
74 | public function actionUpdate($id) |
||
106 | |||
107 | /** |
||
108 | * List additional fields |
||
109 | * @return string |
||
110 | * @throws \Ffcms\Core\Exception\NativeException |
||
111 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
112 | */ |
||
113 | public function actionFieldlist() |
||
121 | |||
122 | /** |
||
123 | * Add and edit additional fields data |
||
124 | * @param int $id |
||
125 | * @return string |
||
126 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
127 | * @throws \Ffcms\Core\Exception\NativeException |
||
128 | */ |
||
129 | public function actionFieldupdate($id) |
||
152 | |||
153 | /** |
||
154 | * Delete custom field action |
||
155 | * @param int $id |
||
156 | * @return string |
||
157 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
158 | * @throws \Ffcms\Core\Exception\NativeException |
||
159 | * @throws ForbiddenException |
||
160 | */ |
||
161 | public function actionFielddelete($id) |
||
185 | |||
186 | /** |
||
187 | * Show profiles settings |
||
188 | * @return string |
||
189 | * @throws \Ffcms\Core\Exception\NativeException |
||
190 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
191 | */ |
||
192 | View Code Duplication | public function actionSettings() |
|
210 | |||
211 | |||
212 | } |
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.