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 |
||
26 | class PostCommentController extends Controller |
||
27 | { |
||
28 | /** |
||
29 | * @inheritdoc |
||
30 | */ |
||
31 | View Code Duplication | public function behaviors() |
|
53 | |||
54 | |||
55 | /** |
||
56 | * Lists all PostComment models on specific post type. |
||
57 | * If there is post_id the action will generate list of all PostComment models based on post_id. |
||
58 | * |
||
59 | * @param integer $posttype Post type ID |
||
60 | * @param null|integer $post Post ID |
||
61 | * @throws \yii\web\NotFoundHttpException |
||
62 | * @return string |
||
63 | */ |
||
64 | public function actionIndex($posttype, $post = null) |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Updates an existing PostComment model. |
||
88 | * If update is successful, the browser will be redirected to the 'view' page. |
||
89 | * |
||
90 | * @param integer $id |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function actionUpdate($id) |
||
94 | { |
||
95 | $model = $this->findModel($id); |
||
96 | |||
97 | if ($model->load(Yii::$app->request->post())) { |
||
98 | $model->date = date('Y-m-d H:i:s', strtotime($model->date)); |
||
99 | if ($model->save()) { |
||
100 | return $this->redirect(['update', 'id' => $model->id]); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $this->render('update', [ |
||
105 | 'model' => $model, |
||
106 | ]); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Deletes an existing PostComment model. |
||
111 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
112 | * |
||
113 | * @param integer $id |
||
114 | * @return mixed |
||
115 | */ |
||
116 | View Code Duplication | public function actionDelete($id) |
|
130 | |||
131 | /** |
||
132 | * Bulk action for PostComment triggered when button 'Apply' clicked. |
||
133 | * The action depends on the value of the dropdown next to the button. |
||
134 | * Only accept POST HTTP method. |
||
135 | */ |
||
136 | View Code Duplication | public function actionBulkAction() |
|
163 | |||
164 | /** |
||
165 | * Reply an existing PostComment model. |
||
166 | * If reply is successful, the browser will be redirected to 'update' page. |
||
167 | * |
||
168 | * @param int $id Find PostComment model based on id as parent. |
||
169 | * @return string |
||
170 | */ |
||
171 | public function actionReply($id) |
||
189 | |||
190 | /** |
||
191 | * Finds the PostComment model based on its primary key value. |
||
192 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
193 | * |
||
194 | * @param integer $id |
||
195 | * @return PostComment the loaded model |
||
196 | * @throws NotFoundHttpException if the model cannot be found |
||
197 | */ |
||
198 | protected function findModel($id) |
||
206 | |||
207 | /** |
||
208 | * Finds the PostType model based on its primary key value. |
||
209 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
210 | * |
||
211 | * @param integer $id |
||
212 | * @return PostType the loaded model |
||
213 | * @throws NotFoundHttpException if the model cannot be found |
||
214 | */ |
||
215 | protected function findPostType($id) |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Finds the Post model based on its primary key value. |
||
227 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
228 | * |
||
229 | * @param integer $id |
||
230 | * @return Post the loaded model |
||
231 | * @throws NotFoundHttpException if the model cannot be found |
||
232 | */ |
||
233 | protected function findPost($id) |
||
241 | } |
||
242 |
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.