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 |
||
25 | class TariffController extends \hipanel\base\CrudController |
||
26 | { |
||
27 | View Code Duplication | public function behaviors() |
|
41 | |||
42 | 1 | public function actions() |
|
43 | { |
||
44 | 1 | return array_merge(parent::actions(), [ |
|
45 | 1 | 'index' => [ |
|
46 | 'class' => IndexAction::class, |
||
47 | 'data' => function () { |
||
48 | return [ |
||
49 | 'types' => Ref::getList('type,tariff', 'hipanel:finance:tariff:types'), |
||
50 | ]; |
||
51 | 1 | }, |
|
52 | ], |
||
53 | 'search' => [ |
||
54 | 'class' => ComboSearchAction::class, |
||
55 | ], |
||
56 | 'validate-form' => [ |
||
57 | 'class' => ValidateFormAction::class, |
||
58 | ], |
||
59 | 'set-note' => [ |
||
60 | 'class' => SmartUpdateAction::class, |
||
61 | 1 | 'success' => Yii::t('hipanel', 'Note updated'), |
|
62 | ], |
||
63 | 'delete' => [ |
||
64 | 'class' => SmartDeleteAction::class, |
||
65 | 1 | 'success' => Yii::t('hipanel:finance:tariff', 'Tariff deleted'), |
|
66 | ], |
||
67 | ]); |
||
68 | } |
||
69 | |||
70 | View Code Duplication | public function actionCreateDomain($parent_id = null) |
|
71 | { |
||
72 | /** @var DomainTariffManager $manager */ |
||
73 | $manager = TariffManagerFactory::createByType('domain', $parent_id); |
||
74 | $form = $manager->form; |
||
75 | |||
76 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
77 | $manager->insert(); |
||
78 | |||
79 | return $this->redirect(['view', 'id' => $form->id]); |
||
80 | } |
||
81 | |||
82 | return $this->render('domain/create', ['model' => $form]); |
||
83 | } |
||
84 | |||
85 | View Code Duplication | public function actionCreateSvds($parent_id = null) |
|
86 | { |
||
87 | /** @var DomainTariffManager $manager */ |
||
88 | $manager = TariffManagerFactory::createByType('svds', $parent_id); |
||
89 | |||
90 | $form = $manager->form; |
||
91 | |||
92 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
93 | $manager->insert(); |
||
94 | |||
95 | return $this->redirect(['view', 'id' => $form->id]); |
||
96 | } |
||
97 | |||
98 | return $this->render('vds/create', ['model' => $form]); |
||
99 | } |
||
100 | |||
101 | View Code Duplication | public function actionCreateOvds($parent_id = null) |
|
102 | { |
||
103 | /** @var DomainTariffManager $manager */ |
||
104 | $manager = TariffManagerFactory::createByType('ovds', $parent_id); |
||
105 | $form = $manager->form; |
||
106 | |||
107 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
108 | $manager->insert(); |
||
109 | |||
110 | return $this->redirect(['view', 'id' => $form->id]); |
||
111 | } |
||
112 | |||
113 | return $this->render('vds/create', ['model' => $form]); |
||
114 | } |
||
115 | |||
116 | View Code Duplication | public function actionCreateCertificate($parent_id = null) |
|
117 | { |
||
118 | /** @var CertificateResource $manager */ |
||
119 | $manager = TariffManagerFactory::createByType('certificate', $parent_id); |
||
120 | $form = $manager->form; |
||
121 | |||
122 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
123 | $manager->insert(); |
||
124 | |||
125 | return $this->redirect(['view', 'id' => $form->id]); |
||
126 | } |
||
127 | |||
128 | return $this->render('certificate/create', ['model' => $form]); |
||
129 | } |
||
130 | |||
131 | View Code Duplication | public function actionCreateServer($parent_id = null) |
|
132 | { |
||
133 | /** @var CertificateResource $manager */ |
||
134 | $manager = TariffManagerFactory::createByType('server', $parent_id); |
||
135 | $form = $manager->form; |
||
136 | |||
137 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
138 | $manager->insert(); |
||
139 | |||
140 | return $this->redirect(['view', 'id' => $form->id]); |
||
141 | } |
||
142 | |||
143 | return $this->render('server/create', ['model' => $form]); |
||
144 | } |
||
145 | |||
146 | View Code Duplication | public function actionUpdate($id) |
|
147 | { |
||
148 | $manager = TariffManagerFactory::createById($id, ['scenario' => 'update']); |
||
149 | $form = $manager->form; |
||
150 | |||
151 | if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) { |
||
152 | $manager->update(); |
||
153 | |||
154 | return $this->redirect(['view', 'id' => $form->id]); |
||
155 | } |
||
156 | |||
157 | return $this->render($manager->getType() . '/update', ['model' => $form]); |
||
158 | } |
||
159 | |||
160 | public function actionCopy() |
||
169 | |||
170 | public function actionView($id) |
||
176 | } |
||
177 |
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.