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 ManageController 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 ManageController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ManageController extends BackendController |
||
24 | { |
||
25 | public function behaviors() |
||
26 | { |
||
27 | return [ |
||
28 | 'access' => [ |
||
29 | 'class' => AccessControl::className(), |
||
30 | 'rules' => [ |
||
31 | [ |
||
32 | 'allow' => true, |
||
33 | 'actions' => ['get-robots'], |
||
34 | 'roles' => ['?'] |
||
35 | ], |
||
36 | [ |
||
37 | 'allow' => true, |
||
38 | 'actions' => ['flush-meta-cache', 'flush-counter-cache', 'flush-robots-cache'], |
||
39 | 'roles' => ['cache manage'], |
||
40 | ], |
||
41 | [ |
||
42 | 'allow' => false, |
||
43 | 'actions' => ['flush-meta-cache', 'flush-counter-cache', 'flush-robots-cache'], |
||
44 | ], |
||
45 | [ |
||
46 | 'allow' => true, |
||
47 | 'roles' => ['seo manage'], |
||
48 | ], |
||
49 | ], |
||
50 | ], |
||
51 | [ |
||
52 | 'class' => 'yii\filters\PageCache', |
||
53 | 'only' => ['GetRobots'], |
||
54 | 'duration' => 24 * 60 * 60, |
||
55 | 'dependency' => ActiveRecordHelper::getCommonTag(Config::className()), |
||
56 | ] |
||
57 | ]; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Index page |
||
62 | * @return string |
||
63 | */ |
||
64 | public function actionIndex() |
||
68 | |||
69 | /** |
||
70 | * return robots.txt |
||
71 | */ |
||
72 | public function actionGetRobots() |
||
73 | { |
||
74 | $robots = Robots::getRobots(); |
||
75 | $response = \Yii::$app->response; |
||
76 | $response->headers->set('Content-Type', 'text/plain'); |
||
77 | $response->format = \yii\web\Response::FORMAT_RAW; |
||
78 | $response->data = $robots; |
||
79 | \Yii::$app->end(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Show list of Meta tag models |
||
84 | * @return string |
||
85 | */ |
||
86 | View Code Duplication | public function actionMeta() |
|
|
|||
87 | { |
||
88 | $searchModel = new Meta(); |
||
89 | $dataProvider = $searchModel->search($_GET); |
||
90 | return $this->render( |
||
91 | 'meta', |
||
92 | [ |
||
93 | 'dataProvider' => $dataProvider, |
||
94 | 'searchModel' => $searchModel, |
||
95 | ] |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Updates an existing Meta model. |
||
101 | * If update is successful, the browser will be redirected to the 'meta' page. |
||
102 | * @param $id |
||
103 | * @return string |
||
104 | * @throws \yii\web\NotFoundHttpException |
||
105 | */ |
||
106 | View Code Duplication | public function actionUpdateMeta($id) |
|
107 | { |
||
108 | /* @var $model Meta */ |
||
109 | $model = Meta::find()->where( |
||
110 | [ |
||
111 | 'key' => $id, |
||
112 | ] |
||
113 | )->one(); |
||
114 | if ($model !== null) { |
||
115 | if ($model->load($_POST) && $model->validate()) { |
||
116 | if ($model->save()) { |
||
117 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
118 | $returnUrl = Yii::$app->request->get('returnUrl', ['meta']); |
||
119 | switch (Yii::$app->request->post('action', 'save')) { |
||
120 | case 'next': |
||
121 | return $this->redirect( |
||
122 | [ |
||
123 | 'create-meta', |
||
124 | 'returnUrl' => $returnUrl, |
||
125 | ] |
||
126 | ); |
||
127 | case 'back': |
||
128 | return $this->redirect($returnUrl); |
||
129 | default: |
||
130 | return $this->redirect( |
||
131 | Url::toRoute( |
||
132 | [ |
||
133 | 'update-meta', |
||
134 | 'id' => $model->getPrimaryKey(), |
||
135 | 'returnUrl' => $returnUrl, |
||
136 | ] |
||
137 | ) |
||
138 | ); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | } else { |
||
143 | return $this->render( |
||
144 | 'updateMeta', |
||
145 | [ |
||
146 | 'model' => $model, |
||
147 | ] |
||
148 | ); |
||
149 | } |
||
150 | } else { |
||
151 | throw new NotFoundHttpException('Meta tag '.$id.' not found'); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Creates a new Meta model. |
||
157 | * If creation is successful, the browser will be redirected to the 'meta' page. |
||
158 | * @return string|\yii\web\Response |
||
159 | */ |
||
160 | public function actionCreateMeta() |
||
161 | { |
||
162 | $model = new Meta(); |
||
163 | if ($model->load($_POST) && $model->save()) { |
||
164 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
165 | $returnUrl = Yii::$app->request->get('returnUrl', ['meta']); |
||
166 | switch (Yii::$app->request->post('action', 'save')) { |
||
167 | case 'next': |
||
168 | return $this->redirect( |
||
169 | [ |
||
170 | 'create-meta', |
||
171 | 'returnUrl' => $returnUrl, |
||
172 | ] |
||
173 | ); |
||
174 | case 'back': |
||
175 | return $this->redirect($returnUrl); |
||
176 | default: |
||
177 | return $this->redirect( |
||
178 | Url::toRoute( |
||
179 | [ |
||
180 | 'update-meta', |
||
181 | 'id' => $model->key, |
||
182 | 'returnUrl' => $returnUrl, |
||
183 | ] |
||
184 | ) |
||
185 | ); |
||
186 | } |
||
187 | } else { |
||
188 | return $this->render( |
||
189 | 'createMeta', |
||
190 | [ |
||
191 | 'model' => $model, |
||
192 | ] |
||
193 | ); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Deletes an existing Meta model. |
||
199 | * If deletion is successful, the browser will be redirected to the 'meta' page. |
||
200 | * @param $id |
||
201 | * @return \yii\web\Response |
||
202 | */ |
||
203 | public function actionDeleteMeta($id) |
||
204 | { |
||
205 | Meta::deleteAll('`key` = :id', [':id' => $id]); |
||
206 | return $this->redirect(['meta']); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Deletes an existing Meta models. |
||
211 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
212 | * @return \yii\web\Response |
||
213 | */ |
||
214 | public function actionDeleteMetas() |
||
215 | { |
||
216 | if (isset($_POST['metas'])) { |
||
217 | return Meta::deleteAll( |
||
218 | [ |
||
219 | 'in', |
||
220 | 'key', |
||
221 | $_POST['metas'], |
||
222 | ] |
||
223 | ); |
||
224 | } |
||
225 | return false; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Deletes meta value from cache |
||
230 | * @return bool if no error happens during deletion |
||
231 | */ |
||
232 | public function actionFlushMetaCache() |
||
236 | |||
237 | /** |
||
238 | * Show list of Counter models |
||
239 | * @return string |
||
240 | */ |
||
241 | public function actionCounter() |
||
242 | { |
||
243 | $searchModel = new Counter(); |
||
244 | $dataProvider = $searchModel->search($_GET); |
||
245 | AceHelper::setAceScript($this); |
||
246 | return $this->render( |
||
247 | 'counter', |
||
248 | [ |
||
249 | 'dataProvider' => $dataProvider, |
||
250 | 'searchModel' => $searchModel, |
||
251 | ] |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Updates an existing Counter model. |
||
257 | * If update is successful, the browser will be redirected to the 'meta' page. |
||
258 | * @param $id |
||
259 | * @return string |
||
260 | * @throws \yii\web\NotFoundHttpException |
||
261 | */ |
||
262 | View Code Duplication | public function actionUpdateCounter($id) |
|
263 | { |
||
264 | /* @var $model Counter */ |
||
265 | $model = Counter::find()->where( |
||
266 | [ |
||
267 | 'id' => $id, |
||
268 | ] |
||
269 | )->one(); |
||
270 | AceHelper::setAceScript($this); |
||
271 | if ($model !== null) { |
||
272 | if ($model->load($_POST) && $model->validate()) { |
||
273 | if ($model->save()) { |
||
274 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
275 | $returnUrl = Yii::$app->request->get('returnUrl', ['counter']); |
||
276 | switch (Yii::$app->request->post('action', 'save')) { |
||
277 | case 'next': |
||
278 | return $this->redirect( |
||
279 | [ |
||
280 | 'create-counter', |
||
281 | 'returnUrl' => $returnUrl, |
||
282 | ] |
||
283 | ); |
||
284 | case 'back': |
||
285 | return $this->redirect($returnUrl); |
||
286 | default: |
||
287 | return $this->redirect( |
||
288 | Url::toRoute( |
||
289 | [ |
||
290 | 'update-counter', |
||
291 | 'id' => $model->id, |
||
292 | 'returnUrl' => $returnUrl |
||
293 | ] |
||
294 | ) |
||
295 | ); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | } else { |
||
300 | return $this->render( |
||
301 | 'updateCounter', |
||
302 | [ |
||
303 | 'model' => $model, |
||
304 | ] |
||
305 | ); |
||
306 | } |
||
307 | } else { |
||
308 | throw new NotFoundHttpException('counter #'.$id.' not found'); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Creates a new Counter model. |
||
314 | * If creation is successful, the browser will be redirected to the 'counter' page. |
||
315 | * @return string|\yii\web\Response |
||
316 | */ |
||
317 | public function actionCreateCounter() |
||
318 | { |
||
319 | $model = new Counter(); |
||
320 | AceHelper::setAceScript($this); |
||
321 | if ($model->load($_POST) && $model->save()) { |
||
322 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
323 | $returnUrl = Yii::$app->request->get('returnUrl', ['counter']); |
||
324 | if (Yii::$app->request->post('action', 'back') == 'next') { |
||
325 | $route = ['create-counter', 'returnUrl' => $returnUrl]; |
||
326 | if (!is_null(Yii::$app->request->get('parent_id', null))) { |
||
327 | $route['parent_id'] = Yii::$app->request->get('parent_id'); |
||
328 | } |
||
329 | return $this->redirect($route); |
||
330 | } else { |
||
331 | return $this->redirect($returnUrl); |
||
332 | } |
||
333 | |||
334 | } else { |
||
335 | return $this->render( |
||
336 | 'createCounter', |
||
337 | [ |
||
338 | 'model' => $model, |
||
339 | ] |
||
340 | ); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Deletes an existing Meta model. |
||
346 | * If deletion is successful, the browser will be redirected to the 'counter' page. |
||
347 | * @param $id |
||
348 | * @return \yii\web\Response |
||
349 | */ |
||
350 | public function actionDeleteCounter($id) |
||
351 | { |
||
352 | Counter::deleteAll('`id` = :id', [':id' => $id]); |
||
353 | return $this->redirect(['counter']); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Deletes an existing Counter models. |
||
358 | * If deletion is successful, the browser will be redirected to the 'counter' page. |
||
359 | * @return \yii\web\Response |
||
360 | */ |
||
361 | public function actionDeleteCounters() |
||
362 | { |
||
363 | if (isset($_POST['counters'])) { |
||
364 | return Counter::deleteAll( |
||
365 | [ |
||
366 | 'in', |
||
367 | 'id', |
||
368 | $_POST['counters'], |
||
369 | ] |
||
370 | ); |
||
371 | } |
||
372 | return false; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Deletes counter value from cache |
||
377 | * @return bool if no error happens during deletion |
||
378 | */ |
||
379 | public function actionFlushCounterCache() |
||
383 | |||
384 | /** |
||
385 | * Update robots.txt |
||
386 | * @return string|\yii\web\Response |
||
387 | * @throws \yii\web\NotFoundHttpException |
||
388 | */ |
||
389 | public function actionRobots() |
||
390 | { |
||
391 | $model = Robots::getModel(); |
||
392 | if ($model === null) { |
||
393 | $model = new Robots(); |
||
394 | } |
||
395 | |||
396 | if (Yii::$app->request->isPost) { |
||
397 | if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
||
398 | $model->save(); |
||
399 | return $this->refresh(); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | return $this->render('robots', ['model' => $model]); |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Show list of Redirect models |
||
408 | * @return string |
||
409 | */ |
||
410 | View Code Duplication | public function actionRedirect() |
|
411 | { |
||
412 | $searchModel = new Redirect(['scenario' => 'search']); |
||
413 | $dataProvider = $searchModel->search($_GET); |
||
414 | return $this->render( |
||
415 | 'redirect', |
||
416 | [ |
||
417 | 'dataProvider' => $dataProvider, |
||
418 | 'searchModel' => $searchModel, |
||
419 | ] |
||
420 | ); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Creates a new Redirect model. |
||
425 | * If creation is successful, the browser will be redirected to the 'redirect' page. |
||
426 | * @return string|\yii\web\Response |
||
427 | */ |
||
428 | public function actionCreateRedirect() |
||
429 | { |
||
430 | $model = new Redirect(['active' => true]); |
||
431 | if ($model->load($_POST) && $model->save()) { |
||
432 | $action = Yii::$app->request->post('action', 'save'); |
||
433 | $returnUrl = Yii::$app->request->get('returnUrl', ['index']); |
||
434 | switch ($action) { |
||
435 | case 'next': |
||
436 | return $this->redirect( |
||
437 | [ |
||
438 | 'create-redirect', |
||
439 | 'returnUrl' => $returnUrl, |
||
440 | ] |
||
441 | ); |
||
442 | case 'back': |
||
443 | return $this->redirect($returnUrl); |
||
444 | default: |
||
445 | return $this->redirect( |
||
446 | Url::toRoute( |
||
447 | [ |
||
448 | 'update-redirect', |
||
449 | 'id' => $model->id, |
||
450 | 'returnUrl' => $returnUrl, |
||
451 | ] |
||
452 | ) |
||
453 | ); |
||
454 | } |
||
455 | } else { |
||
456 | return $this->render( |
||
457 | 'createRedirect', |
||
458 | [ |
||
459 | 'model' => $model, |
||
460 | ] |
||
461 | ); |
||
462 | } |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Creates a new Redirect models. |
||
467 | * @return string |
||
468 | */ |
||
469 | public function actionCreateRedirects() |
||
470 | { |
||
471 | if (isset($_POST['redirects'])) { |
||
472 | $all = 0; |
||
473 | $added = 0; |
||
474 | View Code Duplication | if (isset($_POST['redirects']['static']) && !empty($_POST['redirects']['static'])) { |
|
475 | $static = explode("\n", $_POST['redirects']['static']); |
||
476 | foreach ($static as $redirectStr) { |
||
477 | $all++; |
||
478 | $r = explode("\t", $redirectStr); |
||
479 | $redirect = new Redirect( |
||
480 | [ |
||
481 | 'type' => Redirect::TYPE_STATIC, |
||
482 | 'from' => trim($r[0]), |
||
483 | 'to' => trim($r[1]), |
||
484 | 'active' => true, |
||
485 | ] |
||
486 | ); |
||
487 | if ($redirect->save()) { |
||
488 | $added++; |
||
489 | } |
||
490 | } |
||
491 | } |
||
492 | View Code Duplication | if (isset($_POST['redirects']['regular']) && !empty($_POST['redirects']['regular'])) { |
|
493 | $regular = explode("\n", $_POST['redirects']['regular']); |
||
494 | foreach ($regular as $redirectStr) { |
||
495 | $all++; |
||
496 | $r = explode("\t", $redirectStr); |
||
497 | $redirect = new Redirect( |
||
498 | [ |
||
499 | 'type' => Redirect::TYPE_PREG, |
||
500 | 'from' => trim($r[0]), |
||
501 | 'to' => trim($r[1]), |
||
502 | 'active' => true, |
||
503 | ] |
||
504 | ); |
||
505 | if ($redirect->save()) { |
||
506 | $added++; |
||
507 | } |
||
508 | } |
||
509 | } |
||
510 | |||
511 | Yii::$app->session->setFlash('success', Yii::t('app', 'Records has been saved')); |
||
512 | $returnUrl = Yii::$app->request->get('returnUrl', ['/redirect']); |
||
513 | switch (Yii::$app->request->post('action', 'save')) { |
||
514 | case 'next': |
||
515 | return $this->redirect( |
||
516 | [ |
||
517 | 'create-redirects', |
||
518 | 'returnUrl' => $returnUrl, |
||
519 | ] |
||
520 | ); |
||
521 | case 'back': |
||
522 | return $this->redirect($returnUrl); |
||
523 | default: |
||
524 | return $this->redirect( |
||
525 | Url::toRoute( |
||
526 | [ |
||
527 | 'redirects', |
||
528 | 'returnUrl' => $returnUrl, |
||
529 | ] |
||
530 | ) |
||
531 | ); |
||
532 | } |
||
533 | |||
534 | } else { |
||
535 | return $this->render('createRedirects'); |
||
536 | } |
||
537 | |||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Updates an existing Redirect model. |
||
542 | * If update is successful, the browser will be redirected to the 'redirect' page. |
||
543 | * @param $id |
||
544 | * @return string |
||
545 | * @throws \yii\web\NotFoundHttpException |
||
546 | */ |
||
547 | View Code Duplication | public function actionUpdateRedirect($id) |
|
548 | { |
||
549 | /* @var $model Redirect */ |
||
550 | $model = Redirect::find()->where( |
||
551 | [ |
||
552 | 'id' => $id, |
||
553 | ] |
||
554 | )->one(); |
||
555 | if ($model !== null) { |
||
556 | if ($model->load($_POST) && $model->validate()) { |
||
557 | if ($model->save()) { |
||
558 | Yii::$app->session->setFlash('success', Yii::t('app', 'Records has been saved')); |
||
559 | $returnUrl = Yii::$app->request->get('returnUrl', ['redirect']); |
||
560 | switch (Yii::$app->request->post('action', 'save')) { |
||
561 | case 'next': |
||
562 | return $this->redirect( |
||
563 | [ |
||
564 | 'create-redirect', |
||
565 | 'returnUrl' => $returnUrl, |
||
566 | ] |
||
567 | ); |
||
568 | case 'back': |
||
569 | return $this->redirect($returnUrl); |
||
570 | default: |
||
571 | return $this->redirect( |
||
572 | Url::toRoute( |
||
573 | [ |
||
574 | 'update-redirect', |
||
575 | 'id' => $model->id, |
||
576 | 'returnUrl' => $returnUrl, |
||
577 | ] |
||
578 | ) |
||
579 | ); |
||
580 | } |
||
581 | } |
||
582 | } else { |
||
583 | return $this->render( |
||
584 | 'updateRedirect', |
||
585 | [ |
||
586 | 'model' => $model, |
||
587 | ] |
||
588 | ); |
||
589 | } |
||
590 | } else { |
||
591 | throw new NotFoundHttpException('redirect #'.$id.' not found'); |
||
592 | } |
||
593 | } |
||
594 | |||
595 | /** |
||
596 | * Deletes an existing Redirect model. |
||
597 | * If deletion is successful, the browser will be redirected to the 'redirect' page. |
||
598 | * @param $id |
||
599 | * @return \yii\web\Response |
||
600 | */ |
||
601 | public function actionDeleteRedirect($id) |
||
602 | { |
||
603 | Redirect::deleteAll('`id` = :id', [':id' => $id]); |
||
604 | return $this->redirect(['redirect']); |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * Deletes an existing Redirect models. |
||
609 | * If deletion is successful, the browser will be redirected to the 'redirect' page. |
||
610 | * @return \yii\web\Response |
||
611 | */ |
||
612 | public function actionDeleteRedirects() |
||
613 | { |
||
614 | if (isset($_POST['redirects'])) { |
||
615 | return Redirect::deleteAll( |
||
616 | [ |
||
617 | 'in', |
||
618 | 'id', |
||
619 | $_POST['redirects'], |
||
620 | ] |
||
621 | ); |
||
622 | } |
||
623 | return false; |
||
624 | } |
||
625 | |||
626 | public function actionGenerateRedirectFile() |
||
630 | |||
631 | public function actionDeleteRedirectFile() |
||
635 | |||
636 | public function actionEcommerce() |
||
637 | { |
||
638 | $yaCounter = $gaCounter = ['id' => '', 'active' => 0]; |
||
639 | |||
640 | if (Yii::$app->request->isPost) { |
||
641 | $gaCounter = array_merge($gaCounter, Yii::$app->request->post('GoogleCounter', [])); |
||
642 | $yaCounter = array_merge($yaCounter, Yii::$app->request->post('YandexCounter', [])); |
||
643 | |||
644 | $model = Config::getModelByKey('ecommerceCounters'); |
||
645 | if (empty($model)) { |
||
646 | $model = new Config(); |
||
647 | $model->key = 'ecommerceCounters'; |
||
648 | } |
||
649 | $model->value = Json::encode(['google' => $gaCounter, 'yandex' => $yaCounter]); |
||
650 | $model->save(); |
||
651 | } |
||
652 | |||
653 | $counters = Config::getModelByKey('ecommerceCounters'); |
||
654 | if (!empty($counters)) { |
||
668 | |||
669 | public static function renderEcommerceCounters(Event $event) |
||
709 | } |
||
710 |
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: