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 |
||
18 | View Code Duplication | class LessController extends Controller |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var boolean whether to enable CSRF validation for the actions in this controller. |
||
22 | * CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true. |
||
23 | */ |
||
24 | public $enableCsrfValidation = false; |
||
25 | |||
26 | /** |
||
27 | * @inheritdoc |
||
28 | */ |
||
29 | public function behaviors() |
||
48 | |||
49 | /** |
||
50 | * Lists all Less models. |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public function actionIndex() |
||
71 | |||
72 | /** |
||
73 | * Displays a single Less model. |
||
74 | * |
||
75 | * @param integer $id |
||
76 | * |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function actionView($id) |
||
92 | |||
93 | /** |
||
94 | * Creates a new Less model. |
||
95 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function actionCreate() |
||
114 | |||
115 | /** |
||
116 | * Updates an existing Less model. |
||
117 | * If update is successful, the browser will be redirected to the 'view' page. |
||
118 | * |
||
119 | * @param integer $id |
||
120 | * |
||
121 | * @return mixed |
||
122 | */ |
||
123 | public function actionUpdate($id) |
||
138 | |||
139 | /** |
||
140 | * Deletes an existing Less model. |
||
141 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
142 | * |
||
143 | * @param integer $id |
||
144 | * |
||
145 | * @return mixed |
||
146 | */ |
||
147 | public function actionDelete($id) |
||
171 | |||
172 | /** |
||
173 | * Finds the Less model based on its primary key value. |
||
174 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
175 | * |
||
176 | * @param integer $id |
||
177 | * |
||
178 | * @return Less the loaded model |
||
179 | * @throws HttpException if the model cannot be found |
||
180 | */ |
||
181 | protected function findModel($id) |
||
189 | } |
||
190 |
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.