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 |
||
| 25 | class MediaCommentController extends Controller |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @inheritdoc |
||
| 29 | */ |
||
| 30 | public function behaviors() |
||
| 31 | { |
||
| 32 | return [ |
||
| 33 | 'access' => [ |
||
| 34 | 'class' => AccessControl::className(), |
||
| 35 | 'rules' => [ |
||
| 36 | [ |
||
| 37 | 'actions' => ['index', 'update', 'delete', 'bulk-action', 'reply'], |
||
| 38 | 'allow' => true, |
||
| 39 | 'roles' => ['editor'], |
||
| 40 | ], |
||
| 41 | ], |
||
| 42 | ], |
||
| 43 | 'verbs' => [ |
||
| 44 | 'class' => VerbFilter::className(), |
||
| 45 | 'actions' => [ |
||
| 46 | 'delete' => ['post'], |
||
| 47 | 'bulk-action' => ['post'], |
||
| 48 | ], |
||
| 49 | ], |
||
| 50 | ]; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Lists all MediaComment models. |
||
| 55 | * If there is media, the action will generate list of all MediaComment models based on Media ID. |
||
| 56 | * |
||
| 57 | * @param null|integer $media Media ID |
||
| 58 | * @return mixed |
||
| 59 | */ |
||
| 60 | public function actionIndex($media = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Updates an existing MediaComment model. |
||
| 81 | * If update is successful, the browser will be redirected to the 'update' page. |
||
| 82 | * |
||
| 83 | * @param integer $id |
||
| 84 | * @return mixed |
||
| 85 | */ |
||
| 86 | public function actionUpdate($id) |
||
| 87 | { |
||
| 88 | $model = $this->findModel($id); |
||
| 89 | |||
| 90 | if ($model->load(Yii::$app->request->post())) { |
||
| 91 | $model->date = date('Y-m-d H:i:s', strtotime($model->date)); |
||
| 92 | if ($model->save()) { |
||
| 93 | return $this->redirect(['update', 'id' => $model->id]); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return $this->render('update', [ |
||
| 98 | 'model' => $model, |
||
| 99 | ]); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Deletes an existing MediaComment model. |
||
| 104 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
| 105 | * |
||
| 106 | * @param integer $id |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function actionDelete($id) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Bulk action for MediaComment triggered when button 'Apply' clicked. |
||
| 126 | * The action depends on the value of the dropdown next to the button. |
||
| 127 | * Only accept POST HTTP method. |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function actionBulkAction() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Reply an existing MediaComment model. |
||
| 159 | * If reply is successful, the browser will be redirected to 'update' page. |
||
| 160 | * |
||
| 161 | * @param int $id Find MediaComment model based on id as its parent |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function actionReply($id) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Finds the MediaComment model based on its primary key value. |
||
| 187 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
| 188 | * |
||
| 189 | * @param integer $id |
||
| 190 | * @return MediaComment the loaded model |
||
| 191 | * @throws NotFoundHttpException if the model cannot be found |
||
| 192 | */ |
||
| 193 | protected function findModel($id) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Finds the Media model based on its primary key value. |
||
| 204 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
| 205 | * |
||
| 206 | * @param integer $id |
||
| 207 | * @return Media the loaded model |
||
| 208 | * @throws NotFoundHttpException if the model cannot be found |
||
| 209 | */ |
||
| 210 | protected function findMedia($id) |
||
| 218 | } |
||
| 219 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: