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 |
||
15 | class UserController extends Controller |
||
16 | { |
||
17 | /** |
||
18 | * @inheritdoc |
||
19 | */ |
||
20 | public function behaviors() |
||
33 | |||
34 | /** |
||
35 | * Lists all User models. |
||
36 | * @return mixed |
||
37 | */ |
||
38 | public function actionIndex() |
||
48 | |||
49 | /** |
||
50 | * Displays a single User model. |
||
51 | * @param integer $id |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function actionView($id) |
||
60 | |||
61 | /** |
||
62 | * Creates a new User model. |
||
63 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
64 | * @return mixed |
||
65 | */ |
||
66 | View Code Duplication | public function actionCreate() |
|
78 | |||
79 | /** |
||
80 | * Updates an existing User model. |
||
81 | * If update is successful, the browser will be redirected to the 'view' page. |
||
82 | * @param integer $id |
||
83 | * @return mixed |
||
84 | */ |
||
85 | View Code Duplication | public function actionUpdate($id) |
|
97 | |||
98 | /** |
||
99 | * Deletes an existing User model. |
||
100 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
101 | * @param integer $id |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function actionDelete($id) |
||
110 | |||
111 | /** |
||
112 | * Change user password |
||
113 | * @param int $id user identifier |
||
114 | * @return \yii\web\Response |
||
115 | */ |
||
116 | public function actionChangePassword($id) |
||
123 | |||
124 | /** |
||
125 | * Block user |
||
126 | * @param int $id user identifier |
||
127 | * @return \yii\web\Response |
||
128 | */ |
||
129 | public function actionBlock($id) |
||
134 | |||
135 | /** |
||
136 | * Unblock user |
||
137 | * @param int $id user identifier |
||
138 | * @return \yii\web\Response |
||
139 | */ |
||
140 | public function actionUnblock($id) |
||
145 | |||
146 | /** |
||
147 | * Finds the User model based on its primary key value. |
||
148 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
149 | * @param integer $id |
||
150 | * @return User the loaded model |
||
151 | * @throws NotFoundHttpException if the model cannot be found |
||
152 | */ |
||
153 | protected function findModel($id) |
||
161 | |||
162 | } |
||
163 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.