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 |
||
24 | class SettingController extends Controller |
||
25 | { |
||
26 | /** |
||
27 | * @inheritdoc |
||
28 | */ |
||
29 | public function behaviors() |
||
56 | |||
57 | /** |
||
58 | * Lists all Option models. |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | View Code Duplication | public function actionIndex() |
|
72 | |||
73 | /** |
||
74 | * Displays single Option model. |
||
75 | * |
||
76 | * @param integer $id |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function actionView($id) |
||
85 | |||
86 | /** |
||
87 | * Creates a new Option model. |
||
88 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
89 | * |
||
90 | * @return mixed |
||
91 | */ |
||
92 | View Code Duplication | public function actionCreate() |
|
104 | |||
105 | /** |
||
106 | * Updates an existing Option model. |
||
107 | * If update is successful, the browser will be redirected to the 'view' page. |
||
108 | * |
||
109 | * @param integer $id |
||
110 | * @return mixed |
||
111 | */ |
||
112 | View Code Duplication | public function actionUpdate($id) |
|
124 | |||
125 | /** |
||
126 | * Deletes an existing Option model. |
||
127 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
128 | * |
||
129 | * @param integer $id |
||
130 | * @return mixed |
||
131 | */ |
||
132 | public function actionDelete($id) |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Render option page for specific group of option. |
||
142 | * It will use group file if exist. |
||
143 | * |
||
144 | * @param string $id |
||
145 | * @return mixed |
||
146 | */ |
||
147 | public function actionGroup($id) |
||
169 | |||
170 | /** |
||
171 | * Finds the Option model based on its primary key value. |
||
172 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
173 | * |
||
174 | * @param integer $id |
||
175 | * @return Option the loaded model |
||
176 | * @throws NotFoundHttpException if the model cannot be found |
||
177 | */ |
||
178 | protected function findModel($id) |
||
186 | |||
187 | /** |
||
188 | * Finds the Option models based on their group. |
||
189 | * If the models are not found, a 404 HTTP exception will be thrown. |
||
190 | * |
||
191 | * @param string $id |
||
192 | * @return Option the loaded model |
||
193 | * @throws NotFoundHttpException if the models cannot be found |
||
194 | */ |
||
195 | protected function findModelByGroup($id) |
||
203 | } |
||
204 |
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.