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 |
||
27 | class WidgetController extends Controller |
||
28 | { |
||
29 | /** |
||
30 | * @var string Path to widget directory. |
||
31 | */ |
||
32 | private $_dir; |
||
33 | |||
34 | /** |
||
35 | * @var string Path to temporary directory of widget. |
||
36 | */ |
||
37 | private $_tmp; |
||
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | View Code Duplication | public function behaviors() |
|
74 | |||
75 | /** |
||
76 | * Scan widget directory to get list of all available widgets and list all active widgets. |
||
77 | * |
||
78 | * @return mixed |
||
79 | */ |
||
80 | public function actionIndex() |
||
81 | { |
||
82 | $config = []; |
||
83 | $active = []; |
||
84 | $available = []; |
||
85 | $spaces = ArrayHelper::getValue(Yii::$app->params, 'widget', []); |
||
86 | |||
87 | if (!is_dir($this->_dir)) { |
||
88 | FileHelper::createDirectory($this->_dir, 0755); |
||
89 | } |
||
90 | |||
91 | foreach (scandir($this->_dir) as $widget) { |
||
92 | if (is_dir($this->_dir . $widget) && $widget !== '.' && $widget !== '..') { |
||
93 | $configPath = $this->_dir . $widget . '/config/main.php'; |
||
94 | if (is_file($configPath)) { |
||
95 | $config = require($configPath); |
||
96 | $config['directory'] = $widget; |
||
97 | } |
||
98 | $available[$widget] = $config; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | foreach ($spaces as $space) { |
||
103 | $model = Widget::find() |
||
104 | ->where(['location' => $space['location']]) |
||
105 | ->orderBy(['order' => SORT_ASC]) |
||
106 | ->all(); |
||
107 | $active[$space['location']] = $model; |
||
108 | } |
||
109 | |||
110 | return $this->render('index', [ |
||
111 | 'active' => $active, |
||
112 | 'available' => $available, |
||
113 | 'spaces' => $spaces, |
||
114 | ]); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Register new widget. |
||
119 | * Widget zip uploaded to temporary directory and extracted there. |
||
120 | * Check the widget directory and move the first directory of the extracted widget. |
||
121 | * If the widget configuration is valid, save the widget, if not remove the widget. |
||
122 | * If registration is successful, the browser will be redirected to the 'index' page. |
||
123 | * |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function actionCreate() |
||
209 | |||
210 | /** |
||
211 | * Delete widget and activated widgets. |
||
212 | * |
||
213 | * @param $id string |
||
214 | * |
||
215 | * @return \yii\web\Response |
||
216 | */ |
||
217 | public function actionDelete($id) |
||
224 | |||
225 | /** |
||
226 | * Activated widget via ajax |
||
227 | * |
||
228 | * @param $id string |
||
229 | * |
||
230 | * @return null|string |
||
231 | */ |
||
232 | public function actionAjaxActivate($id) |
||
257 | |||
258 | /** |
||
259 | * Update activated widget via ajax. |
||
260 | * |
||
261 | * @param $id integer |
||
262 | */ |
||
263 | public function actionAjaxUpdate($id) |
||
272 | |||
273 | /** |
||
274 | * Delete active widget via ajax. |
||
275 | * |
||
276 | * @param $id integer |
||
277 | */ |
||
278 | public function actionAjaxDelete($id) |
||
282 | |||
283 | /** |
||
284 | * Save order for widget. |
||
285 | */ |
||
286 | public function actionAjaxSaveOrder() |
||
294 | |||
295 | /** |
||
296 | * @inheritdoc |
||
297 | */ |
||
298 | public function beforeAction($action) |
||
313 | |||
314 | /** |
||
315 | * Finds the Widget model based on its primary key value. |
||
316 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
317 | * |
||
318 | * @param integer $id |
||
319 | * |
||
320 | * @return Widget the loaded model |
||
321 | * @throws NotFoundHttpException if the model cannot be found |
||
322 | */ |
||
323 | protected function findModel($id) |
||
331 | } |
||
332 |
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.