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 |
||
19 | class ScreenTemplateController extends BaseController |
||
20 | { |
||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | public function behaviors() |
||
42 | |||
43 | /** |
||
44 | * Lists all ScreenTemplate models. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | View Code Duplication | public function actionIndex() |
|
58 | |||
59 | /** |
||
60 | * Displays a single ScreenTemplate model. |
||
61 | * |
||
62 | * @param int $id |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | public function actionView($id) |
||
79 | |||
80 | /** |
||
81 | * Creates a new ScreenTemplate model. |
||
82 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
83 | * |
||
84 | * @return \yii\web\Response|string redirect or render |
||
85 | */ |
||
86 | View Code Duplication | public function actionCreate() |
|
99 | |||
100 | /** |
||
101 | * Updates an existing ScreenTemplate model. |
||
102 | * If update is successful, the browser will be redirected to the 'view' page. |
||
103 | * |
||
104 | * @param int $id |
||
105 | * |
||
106 | * @return \yii\web\Response|string redirect or render |
||
107 | */ |
||
108 | View Code Duplication | public function actionUpdate($id) |
|
121 | |||
122 | /** |
||
123 | * Deletes an existing ScreenTemplate model. |
||
124 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
125 | * |
||
126 | * @param int $id |
||
127 | * |
||
128 | * @return \yii\web\Response |
||
129 | */ |
||
130 | public function actionDelete($id) |
||
136 | |||
137 | /** |
||
138 | * Finds the ScreenTemplate model based on its primary key value. |
||
139 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
140 | * |
||
141 | * @param int $id |
||
142 | * |
||
143 | * @return ScreenTemplate the loaded model |
||
144 | * |
||
145 | * @throws NotFoundHttpException if the model cannot be found |
||
146 | */ |
||
147 | View Code Duplication | protected function findModel($id) |
|
155 | |||
156 | /** |
||
157 | * Create a field and save it. |
||
158 | * |
||
159 | * @api |
||
160 | * |
||
161 | * @param int $templateId |
||
162 | * |
||
163 | * @return string json status |
||
164 | */ |
||
165 | public function actionAddField($templateId) |
||
182 | |||
183 | /** |
||
184 | * Retrieve a field spec. |
||
185 | * |
||
186 | * @api |
||
187 | * |
||
188 | * @param int $id field id |
||
189 | * |
||
190 | * @return string json field |
||
191 | */ |
||
192 | public function actionGetField($id) |
||
204 | |||
205 | /** |
||
206 | * Read POST data and update field, or display AJAX popup form. |
||
207 | * |
||
208 | * @param int $id field id |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function actionEditField($id) |
||
250 | |||
251 | /** |
||
252 | * Update field position. |
||
253 | * |
||
254 | * @api |
||
255 | * |
||
256 | * @param int $id field id |
||
257 | * |
||
258 | * @return string json status |
||
259 | */ |
||
260 | public function actionSetFieldPos($id = null) |
||
281 | |||
282 | /** |
||
283 | * Delete a field. |
||
284 | * |
||
285 | * @api |
||
286 | * |
||
287 | * @param int $id field id |
||
288 | * |
||
289 | * @return string json status |
||
290 | */ |
||
291 | public function actionDeleteField($id) |
||
302 | |||
303 | /** |
||
304 | * Builds an array of backgrounds usable in view. |
||
305 | * |
||
306 | * @return string[] backgrounds |
||
307 | */ |
||
308 | public static function backgroundsArray() |
||
323 | |||
324 | /** |
||
325 | * Custom min/max float rand. |
||
326 | * |
||
327 | * @param float $min |
||
328 | * @param float $max |
||
329 | * |
||
330 | * @return float random float |
||
331 | */ |
||
332 | public static function randf($min = 0.0, $max = 1.0) |
||
336 | } |
||
337 |
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.