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 |
||
23 | class ItemController extends Controller |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @inheritdoc |
||
28 | */ |
||
29 | public function behaviors() |
||
30 | { |
||
31 | return [ |
||
32 | 'verbs' => [ |
||
33 | 'class' => VerbFilter::className(), |
||
34 | 'actions' => [ |
||
35 | 'delete' => ['post'], |
||
36 | 'assign' => ['post'], |
||
37 | 'remove' => ['post'], |
||
38 | ], |
||
39 | ], |
||
40 | ]; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Lists all AuthItem models. |
||
45 | * @return mixed |
||
46 | */ |
||
47 | View Code Duplication | public function actionIndex() |
|
57 | |||
58 | /** |
||
59 | * Displays a single AuthItem model. |
||
60 | * @param string $id |
||
61 | * @return mixed |
||
62 | */ |
||
63 | public function actionView($id) |
||
69 | |||
70 | /** |
||
71 | * Creates a new AuthItem model. |
||
72 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function actionCreate() |
||
85 | |||
86 | /** |
||
87 | * Updates an existing AuthItem model. |
||
88 | * If update is successful, the browser will be redirected to the 'view' page. |
||
89 | * @param string $id |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public function actionUpdate($id) |
||
101 | |||
102 | /** |
||
103 | * Deletes an existing AuthItem model. |
||
104 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
105 | * @param string $id |
||
106 | * @return mixed |
||
107 | */ |
||
108 | View Code Duplication | public function actionDelete($id) |
|
116 | |||
117 | /** |
||
118 | * Assign items |
||
119 | * @param string $id |
||
120 | * @return array |
||
121 | */ |
||
122 | View Code Duplication | public function actionAssign($id) |
|
131 | |||
132 | /** |
||
133 | * Assign items |
||
134 | * @param string $id |
||
135 | * @return array |
||
136 | */ |
||
137 | public function actionGetUsers($id) |
||
145 | |||
146 | /** |
||
147 | * Assign or remove items |
||
148 | * @param string $id |
||
149 | * @return array |
||
150 | */ |
||
151 | View Code Duplication | public function actionRemove($id) |
|
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | public function getViewPath() |
||
168 | |||
169 | /** |
||
170 | * Label use in view |
||
171 | * @throws NotSupportedException |
||
172 | */ |
||
173 | public function labels() |
||
177 | |||
178 | /** |
||
179 | * Type of Auth Item. |
||
180 | * @return integer |
||
181 | */ |
||
182 | public function getType() |
||
186 | |||
187 | /** |
||
188 | * Finds the AuthItem model based on its primary key value. |
||
189 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
190 | * @param string $id |
||
191 | * @return AuthItem the loaded model |
||
192 | * @throws NotFoundHttpException if the model cannot be found |
||
193 | */ |
||
194 | protected function findModel($id) |
||
204 | } |
||
205 |
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.