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:
Complex classes like PropertiesController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PropertiesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class PropertiesController extends Controller |
||
25 | { |
||
26 | |||
27 | public function behaviors() |
||
28 | { |
||
29 | return [ |
||
30 | 'access' => [ |
||
31 | 'class' => AccessControl::className(), |
||
32 | 'rules' => [ |
||
33 | [ |
||
34 | 'allow' => true, |
||
35 | 'roles' => ['property manage'], |
||
36 | ], |
||
37 | ], |
||
38 | ], |
||
39 | ]; |
||
40 | } |
||
41 | |||
42 | public function actions() |
||
43 | { |
||
44 | return [ |
||
45 | 'save-info' => [ |
||
46 | 'class' => SaveInfoAction::className(), |
||
47 | ], |
||
48 | 'addImage' => [ |
||
49 | 'class' => AddImageAction::className(), |
||
50 | ], |
||
51 | ]; |
||
52 | } |
||
53 | |||
54 | View Code Duplication | public function actionIndex() |
|
|
|||
55 | { |
||
56 | $searchModel = new PropertyGroup(); |
||
57 | $dataProvider = $searchModel->search($_GET); |
||
58 | |||
59 | return $this->render( |
||
60 | 'index', |
||
61 | [ |
||
62 | 'dataProvider' => $dataProvider, |
||
63 | 'searchModel' => $searchModel, |
||
64 | ] |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | public function actionGroup($id = null) |
||
69 | { |
||
70 | if ($id === null) { |
||
71 | $model = new PropertyGroup(); |
||
72 | } else { |
||
73 | $model = PropertyGroup::findById($id); |
||
74 | } |
||
75 | |||
76 | if ($model->load(\Yii::$app->request->post()) && $model->validate()) { |
||
77 | $save_result = $model->save(); |
||
78 | if ($save_result) { |
||
79 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
80 | $returnUrl = Yii::$app->request->get('returnUrl', ['/backend/properties/index']); |
||
81 | switch (Yii::$app->request->post('action', 'save')) { |
||
82 | case 'next': |
||
83 | return $this->redirect( |
||
84 | [ |
||
85 | '/backend/properties/group', |
||
86 | 'returnUrl' => $returnUrl, |
||
87 | ] |
||
88 | ); |
||
89 | case 'back': |
||
90 | return $this->redirect($returnUrl); |
||
91 | default: |
||
92 | return $this->redirect( |
||
93 | [ |
||
94 | '/backend/properties/group', |
||
95 | 'id' => $model->id, |
||
96 | 'returnUrl' => $returnUrl, |
||
97 | ] |
||
98 | ); |
||
99 | } |
||
100 | } else { |
||
101 | Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $searchModel = new Property(); |
||
106 | $searchModel->property_group_id = $model->id; |
||
107 | $dataProvider = $searchModel->search($_GET); |
||
108 | |||
109 | |||
110 | return $this->render( |
||
111 | 'group', |
||
112 | [ |
||
113 | 'model' => $model, |
||
114 | 'dataProvider' => $dataProvider, |
||
115 | 'searchModel' => $searchModel, |
||
116 | ] |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param $value_type |
||
122 | * @return string |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | private function getColumnType($value_type) |
||
126 | { |
||
127 | switch ($value_type) { |
||
128 | case 'STRING': |
||
129 | return 'TINYTEXT'; |
||
130 | case 'NUMBER': |
||
131 | return 'FLOAT'; |
||
132 | default: |
||
133 | throw new \Exception('Unknown value type'); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public function actionEditProperty($property_group_id, $id = null) |
||
138 | { |
||
139 | if ($id === null) { |
||
140 | $model = new Property(); |
||
141 | $model->handler_additional_params = '[]'; |
||
142 | } else { |
||
143 | $model = Property::findById($id); |
||
144 | } |
||
145 | $object = Object::getForClass(Property::className()); |
||
146 | $model->property_group_id = $property_group_id; |
||
147 | |||
148 | if ($model->load(\Yii::$app->request->post()) && $model->validate()) { |
||
149 | $propertyHandler = PropertyHandlers::createHandler($model->handler); |
||
150 | if (!$propertyHandler->changePropertyType($model)) { |
||
151 | if ($model->is_column_type_stored) { |
||
152 | if ($model->isNewRecord) { |
||
153 | $object = Object::findById($model->group->object_id); |
||
154 | Yii::$app->db->createCommand() |
||
155 | ->addColumn($object->column_properties_table_name, $model->key, "TINYTEXT") |
||
156 | ->execute(); |
||
157 | if ($object->object_class == Form::className()) { |
||
158 | $submissionObject = Object::getForClass(Submission::className()); |
||
159 | $col_type = $this->getColumnType($model->value_type); |
||
160 | Yii::$app->db->createCommand() |
||
161 | ->addColumn($submissionObject->column_properties_table_name, $model->key, $col_type) |
||
162 | ->execute(); |
||
163 | } |
||
164 | } else { |
||
165 | View Code Duplication | if ($model->key != $model->getOldAttribute('key')) { |
|
166 | $object = Object::findById($model->group->object_id); |
||
167 | Yii::$app->db->createCommand() |
||
168 | ->renameColumn( |
||
169 | $object->column_properties_table_name, |
||
170 | $model->getOldAttribute('key'), |
||
171 | $model->key |
||
172 | )->execute(); |
||
173 | if ($object->object_class == Form::className()) { |
||
174 | $submissionObject = Object::getForClass(Submission::className()); |
||
175 | Yii::$app->db->createCommand() |
||
176 | ->renameColumn( |
||
177 | $submissionObject->column_properties_table_name, |
||
178 | $model->getOldAttribute('key'), |
||
179 | $model->key |
||
180 | )->execute(); |
||
181 | } |
||
182 | } |
||
183 | View Code Duplication | if ($model->value_type != $model->getOldAttribute('value_type')) { |
|
184 | $object = Object::findById($model->group->object_id); |
||
185 | $new_type = $this->getColumnType($model->value_type); |
||
186 | Yii::$app->db->createCommand() |
||
187 | ->alterColumn( |
||
188 | $object->column_properties_table_name, |
||
189 | $model->getOldAttribute('key'), |
||
190 | $new_type |
||
191 | )->execute(); |
||
192 | if ($object->object_class == Form::className()) { |
||
193 | $submissionObject = Object::getForClass(Submission::className()); |
||
194 | Yii::$app->db->createCommand() |
||
195 | ->renameColumn( |
||
196 | $submissionObject->column_properties_table_name, |
||
197 | $model->getOldAttribute('key'), |
||
198 | $new_type |
||
199 | )->execute(); |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | $save_result = $model->save(); |
||
207 | View Code Duplication | if ($save_result) { |
|
208 | $this->runAction('save-info'); |
||
209 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
210 | $returnUrl = Yii::$app->request->get( |
||
211 | 'returnUrl', |
||
212 | [ |
||
213 | '/backend/properties/group', |
||
214 | 'id' => $property_group_id, |
||
215 | ] |
||
216 | ); |
||
217 | switch (Yii::$app->request->post('action', 'save')) { |
||
218 | case 'next': |
||
219 | return $this->redirect( |
||
220 | [ |
||
221 | '/backend/properties/edit-property', |
||
222 | 'property_group_id' => $property_group_id, |
||
223 | 'returnUrl' => $returnUrl, |
||
224 | ] |
||
225 | ); |
||
226 | case 'back': |
||
227 | return $this->redirect($returnUrl); |
||
228 | default: |
||
229 | return $this->redirect( |
||
230 | Url::toRoute( |
||
231 | [ |
||
232 | '/backend/properties/edit-property', |
||
233 | 'id' => $model->id, |
||
234 | 'property_group_id' => $model->property_group_id, |
||
235 | 'returnUrl' => $returnUrl, |
||
236 | ] |
||
237 | ) |
||
238 | ); |
||
239 | } |
||
240 | } else { |
||
241 | Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | $searchModel = new PropertyStaticValues(); |
||
246 | |||
247 | $searchModel->property_id = $model->id; |
||
248 | $dataProvider = $searchModel->search($_GET); |
||
249 | |||
250 | return $this->render( |
||
251 | 'edit-property', |
||
252 | [ |
||
253 | 'model' => $model, |
||
254 | 'dataProvider' => $dataProvider, |
||
255 | 'searchModel' => $searchModel, |
||
256 | 'fieldinterpretParentId' => 0, |
||
257 | 'object' => $object, |
||
258 | ] |
||
259 | ); |
||
260 | } |
||
261 | |||
262 | public function actionEditStaticValue($property_id, $id = null) |
||
263 | { |
||
264 | if ($id === null) { |
||
265 | $model = new PropertyStaticValues(); |
||
266 | } else { |
||
267 | $model = PropertyStaticValues::findOne($id); |
||
268 | } |
||
269 | $object = Object::getForClass(PropertyStaticValues::className()); |
||
270 | $model->property_id = $property_id; |
||
271 | $post = \Yii::$app->request->post(); |
||
272 | if ($model->load($post) && $model->validate()) { |
||
273 | $save_result = $model->save(); |
||
274 | if ($save_result) { |
||
275 | $this->runAction('save-info'); |
||
276 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
277 | $returnUrl = Yii::$app->request->get( |
||
278 | 'returnUrl', |
||
279 | [ |
||
280 | '/backend/properties/edit-property', |
||
281 | 'id' => $model->property_id, |
||
282 | 'property_group_id' => $model->property->property_group_id, |
||
283 | ] |
||
284 | ); |
||
285 | switch (Yii::$app->request->post('action', 'save')) { |
||
286 | case 'next': |
||
287 | return $this->redirect( |
||
288 | [ |
||
289 | '/backend/properties/edit-static-value', |
||
290 | 'property_id' => $model->property_id, |
||
291 | 'returnUrl' => $returnUrl, |
||
292 | ] |
||
293 | ); |
||
294 | case 'back': |
||
295 | return $this->redirect($returnUrl); |
||
296 | default: |
||
297 | return $this->redirect( |
||
298 | Url::toRoute( |
||
299 | [ |
||
300 | '/backend/properties/edit-static-value', |
||
301 | 'id' => $model->id, |
||
302 | 'property_id' => $model->property_id, |
||
303 | 'returnUrl' => $returnUrl, |
||
304 | ] |
||
305 | ) |
||
306 | ); |
||
307 | } |
||
308 | } else { |
||
309 | Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); |
||
310 | } |
||
311 | } |
||
312 | return $this->render( |
||
313 | 'edit-static-value', |
||
314 | [ |
||
315 | 'model' => $model, |
||
316 | 'object' => $object, |
||
317 | ] |
||
318 | ); |
||
319 | } |
||
320 | |||
321 | |||
322 | public function actionAddStaticValue($key, $value, $returnUrl, $objectId = null, $objectModelId = null) |
||
323 | { |
||
324 | $model = new PropertyStaticValues(); |
||
325 | /** @var Property $property */ |
||
326 | $property = Property::findOne(['key'=>$key]); |
||
327 | if (is_null($property)) { |
||
328 | throw new NotFoundHttpException; |
||
329 | } |
||
330 | $model->property_id = $property->id; |
||
331 | if (Yii::$app->request->isPost) { |
||
332 | if ($model->load(Yii::$app->request->post()) && $model->save()) { |
||
333 | $tags = [ |
||
334 | ActiveRecordHelper::getCommonTag(Property::className()), |
||
335 | ActiveRecordHelper::getObjectTag(Property::className(), $property->id), |
||
336 | ActiveRecordHelper::getCommonTag(PropertyGroup::className()), |
||
337 | ActiveRecordHelper::getObjectTag(PropertyGroup::className(), $property->property_group_id), |
||
338 | ]; |
||
339 | if (!is_null($objectId) && !is_null($objectModelId)) { |
||
340 | if ($property->multiple == 0) { |
||
341 | $propertyStaticValueIds = PropertyStaticValues::find() |
||
342 | ->select('id') |
||
343 | ->where(['property_id' => $property->id]) |
||
344 | ->column(); |
||
345 | ObjectStaticValues::deleteAll( |
||
346 | [ |
||
347 | 'object_id' => $objectId, |
||
348 | 'object_model_id' => $objectModelId, |
||
349 | 'property_static_value_id' => $propertyStaticValueIds, |
||
350 | ] |
||
351 | ); |
||
352 | } |
||
353 | $objectStaticValues = new ObjectStaticValues; |
||
354 | $objectStaticValues->attributes = [ |
||
355 | 'object_id' => $objectId, |
||
356 | 'object_model_id' => $objectModelId, |
||
357 | 'property_static_value_id' => $model->id, |
||
358 | ]; |
||
359 | $objectStaticValues->save(); |
||
360 | $tags[] = ActiveRecordHelper::getCommonTag(Object::findById($objectId)->object_class); |
||
361 | $tags[] = ActiveRecordHelper::getObjectTag( |
||
362 | Object::findById($objectId)->object_class, |
||
363 | $objectModelId |
||
364 | ); |
||
365 | } |
||
366 | TagDependency::invalidate(Yii::$app->cache, $tags); |
||
367 | return $this->redirect($returnUrl); |
||
368 | } |
||
369 | } elseif ($value !== "") { |
||
370 | $model->name = $value; |
||
371 | $model->value = $value; |
||
372 | $model->slug = Helper::createSlug($value); |
||
373 | $model->sort_order = 0; |
||
374 | } |
||
375 | return $this->renderAjax('ajax-static-value', ['model' => $model]); |
||
376 | } |
||
377 | |||
378 | |||
379 | public function actionDeleteStaticValue($id, $property_id, $property_group_id) |
||
380 | { |
||
381 | /** @var PropertyStaticValues $model */ |
||
382 | $model = PropertyStaticValues::findOne($id); |
||
383 | if (is_null($model)) { |
||
384 | throw new NotFoundHttpException; |
||
385 | } |
||
386 | $model->delete(); |
||
387 | Yii::$app->session->setFlash('danger', Yii::t('app', 'Object removed')); |
||
388 | return $this->redirect( |
||
389 | Url::to( |
||
390 | [ |
||
391 | '/backend/properties/edit-property', |
||
392 | 'id'=>$property_id, |
||
393 | 'property_group_id'=>$property_group_id |
||
394 | ] |
||
395 | ) |
||
396 | ); |
||
397 | } |
||
398 | |||
399 | View Code Duplication | public function actionDeleteProperty($id, $property_group_id) |
|
400 | { |
||
401 | /** @var Property $model */ |
||
402 | $model = Property::findOne($id); |
||
403 | if (is_null($model)) { |
||
404 | throw new NotFoundHttpException; |
||
405 | } |
||
406 | $model->delete(); |
||
407 | Yii::$app->session->setFlash('danger', Yii::t('app', 'Object removed')); |
||
408 | return $this->redirect( |
||
409 | Url::to( |
||
410 | [ |
||
411 | '/backend/properties/group', |
||
412 | 'id'=>$property_group_id, |
||
413 | ] |
||
414 | ) |
||
415 | ); |
||
416 | } |
||
417 | |||
418 | public function actionRemoveAllProperties($group_id) |
||
419 | { |
||
420 | $items = Yii::$app->request->post('items', []); |
||
421 | if (!empty($items)) { |
||
422 | $items = Property::find()->where(['in', 'id', $items])->all(); |
||
423 | foreach ($items as $item) { |
||
424 | $item->delete(); |
||
425 | } |
||
426 | } |
||
427 | return $this->redirect(['group', 'id' => $group_id]); |
||
428 | } |
||
429 | |||
430 | View Code Duplication | public function actionDeleteGroup($id) |
|
431 | { |
||
432 | /** @var PropertyGroup $model */ |
||
433 | $model = PropertyGroup::findOne($id); |
||
434 | if (is_null($model)) { |
||
435 | throw new NotFoundHttpException; |
||
436 | } |
||
437 | $model->delete(); |
||
438 | Yii::$app->session->setFlash('danger', Yii::t('app', 'Object removed')); |
||
439 | return $this->redirect( |
||
440 | Url::to( |
||
441 | [ |
||
442 | '/backend/properties/index', |
||
443 | ] |
||
444 | ) |
||
445 | ); |
||
446 | } |
||
447 | |||
448 | public function actionRemoveAllGroups() |
||
466 | |||
467 | public function actionHandlers() |
||
471 | } |
||
472 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: