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 TariffController extends \hipanel\base\CrudController |
||
25 | { |
||
26 | 1 | public function actions() |
|
27 | { |
||
28 | return [ |
||
29 | 'index' => [ |
||
30 | 1 | 'class' => IndexAction::class, |
|
31 | 1 | 'data' => function () { |
|
32 | return [ |
||
33 | 'types' => Ref::getList('type,tariff', 'hipanel:finance:tariff:types'), |
||
34 | ]; |
||
35 | 1 | }, |
|
36 | 1 | ], |
|
37 | 'search' => [ |
||
38 | 1 | 'class' => ComboSearchAction::class, |
|
39 | 1 | ], |
|
40 | 'validate-form' => [ |
||
41 | 1 | 'class' => ValidateFormAction::class, |
|
42 | 1 | ], |
|
43 | 'set-note' => [ |
||
44 | 1 | 'class' => SmartUpdateAction::class, |
|
45 | 1 | 'success' => Yii::t('hipanel', 'Note updated'), |
|
46 | 1 | ], |
|
47 | 'delete' => [ |
||
48 | 1 | 'class' => SmartDeleteAction::class, |
|
49 | 1 | 'success' => Yii::t('hipanel:finance:tariff', 'Tariff deleted'), |
|
50 | 1 | ], |
|
51 | 1 | ]; |
|
52 | } |
||
53 | |||
54 | View Code Duplication | public function actionCreateDomain($parent_id = null) |
|
|
|||
55 | { |
||
56 | /** @var DomainTariffManager $manager */ |
||
57 | $manager = TariffManagerFactory::createByType('domain', $parent_id); |
||
58 | $form = $manager->form; |
||
59 | |||
60 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
61 | $manager->insert(); |
||
62 | return $this->redirect(['view', 'id' => $form->id]); |
||
63 | } |
||
64 | |||
65 | return $this->render('domain/create', ['model' => $form]); |
||
66 | } |
||
67 | |||
68 | View Code Duplication | public function actionCreateSvds($parent_id = null) |
|
69 | { |
||
70 | /** @var DomainTariffManager $manager */ |
||
71 | $manager = TariffManagerFactory::createByType('svds', $parent_id); |
||
72 | |||
73 | $form = $manager->form; |
||
74 | |||
75 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
76 | $manager->insert(); |
||
77 | return $this->redirect(['view', 'id' => $form->id]); |
||
78 | } |
||
79 | |||
80 | return $this->render('vds/create', ['model' => $form]); |
||
81 | } |
||
82 | |||
83 | View Code Duplication | public function actionCreateOvds($parent_id = null) |
|
96 | |||
97 | View Code Duplication | public function actionCreateCertificate($parent_id = null) |
|
110 | |||
111 | View Code Duplication | public function actionCreateServer($parent_id = null) |
|
112 | { |
||
113 | /** @var CertificateResource $manager */ |
||
114 | $manager = TariffManagerFactory::createByType('server', $parent_id); |
||
115 | $form = $manager->form; |
||
116 | |||
117 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
118 | $manager->insert(); |
||
119 | return $this->redirect(['view', 'id' => $form->id]); |
||
120 | } |
||
121 | |||
122 | return $this->render('server/create', ['model' => $form]); |
||
123 | } |
||
124 | |||
125 | View Code Duplication | public function actionUpdate($id) |
|
126 | { |
||
127 | $manager = TariffManagerFactory::createById($id, ['scenario' => 'update']); |
||
128 | $form = $manager->form; |
||
129 | |||
130 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
131 | $manager->update(); |
||
132 | return $this->redirect(['view', 'id' => $form->id]); |
||
133 | } |
||
134 | |||
135 | return $this->render($manager->getType() . '/update', ['model' => $form]); |
||
136 | } |
||
137 | |||
138 | public function actionCopy() |
||
139 | { |
||
140 | $id = Yii::$app->request->post('selection')[0]; |
||
141 | $manager = TariffManagerFactory::createById($id, ['scenario' => 'create']); |
||
142 | $form = $manager->form; |
||
143 | |||
144 | return $this->render($manager->getType() . '/copy', ['model' => $form]); |
||
145 | } |
||
146 | |||
147 | public function actionView($id) |
||
153 | } |
||
154 |
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.