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 |
||
| 11 | class UserController extends ModelController |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @inheritdoc |
||
| 16 | */ |
||
| 17 | public function filters() |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @inheritdoc |
||
| 26 | */ |
||
| 27 | public function accessRules() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Updates a password |
||
| 54 | */ |
||
| 55 | public function actionChangePassword() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Creates a new user |
||
| 85 | */ |
||
| 86 | public function actionCreate() |
||
| 87 | { |
||
| 88 | $model = new User(); |
||
| 89 | |||
| 90 | View Code Duplication | if ($this->saveFromPost($model)) |
|
| 91 | { |
||
| 92 | $this->log('"%s" created user "%s"', Yii::app()->user->name, |
||
| 93 | $model->username); |
||
| 94 | |||
| 95 | Yii::app()->user->setFlash('success', Yii::t('User', 'Created user {username}', |
||
| 96 | array('{username}'=>'<em>'.$model->username.'</em>'))); |
||
| 97 | |||
| 98 | $this->redirect(array('admin')); |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->render('create', array( |
||
| 102 | 'model'=>$model, |
||
| 103 | )); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Updates a user |
||
| 108 | * @param int $id the user ID |
||
| 109 | */ |
||
| 110 | public function actionUpdate($id) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Deletes a user |
||
| 149 | * @param int $id the user ID |
||
| 150 | */ |
||
| 151 | public function actionDelete($id) |
||
| 161 | |||
| 162 | } |
||
| 163 |