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 |
||
35 | class AccountController extends \hipanel\base\CrudController |
||
36 | { |
||
37 | public function behaviors() |
||
53 | |||
54 | public function actions() |
||
55 | { |
||
56 | return array_merge(parent::actions(), [ |
||
57 | 'index' => [ |
||
58 | 'class' => IndexAction::class, |
||
59 | 'data' => function ($action) { |
||
60 | return [ |
||
61 | 'stateData' => $action->controller->getStateData(), |
||
62 | 'typeData' => $action->controller->getTypeData(), |
||
63 | ]; |
||
64 | }, |
||
65 | 'collection' => [ |
||
66 | 'class' => Collection::class, |
||
67 | 'model' => new AccountValues(['scenario' => 'default']), |
||
68 | ], |
||
69 | 'on beforeFetchLoad' => function (Event $event): void { |
||
70 | $event->sender->getDataProvider()->query->withValues(); |
||
71 | }, |
||
72 | 'filterStorageMap' => [ |
||
73 | 'login_like' => 'hosting.account.login', |
||
74 | 'server' => 'server.server.name', |
||
75 | 'state' => 'hosting.account.state', |
||
76 | 'type' => 'hosting.account.type', |
||
77 | 'client_id' => 'client.client.id', |
||
78 | 'seller_id' => 'client.client.seller_id', |
||
79 | ], |
||
80 | ], |
||
81 | 'view' => [ |
||
82 | 'class' => ViewAction::class, |
||
83 | 'on beforePerform' => function (Event $event) { |
||
84 | $event->sender->getDataProvider()->query->withBlocking(); |
||
85 | }, |
||
86 | 'data' => function ($action) { |
||
87 | return [ |
||
88 | 'blockReasons' => $action->controller->getBlockReasons(), |
||
89 | ]; |
||
90 | }, |
||
91 | ], |
||
92 | 'create' => [ |
||
93 | 'class' => SmartCreateAction::class, |
||
94 | 'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'), |
||
95 | 'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'), |
||
96 | ], |
||
97 | 'create-ftponly' => [ |
||
98 | 'class' => SmartCreateAction::class, |
||
99 | 'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'), |
||
100 | 'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'), |
||
101 | ], |
||
102 | 'change-password' => [ |
||
103 | 'class' => SmartUpdateAction::class, |
||
104 | 'view' => '_changePasswordModal', |
||
105 | 'POST' => [ |
||
106 | 'save' => true, |
||
107 | 'success' => [ |
||
108 | 'class' => RenderJsonAction::class, |
||
109 | 'return' => function ($action) { |
||
110 | return ['success' => !$action->collection->hasErrors()]; |
||
111 | }, |
||
112 | ], |
||
113 | ], |
||
114 | ], |
||
115 | 'set-allowed-ips' => [ |
||
116 | 'class' => SmartUpdateAction::class, |
||
117 | 'view' => '_ipRestrictionsModal', |
||
118 | 'success' => Yii::t('hipanel:hosting', 'Allowed IPs changing task has been successfully added to queue'), |
||
119 | 'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change allowed IPs'), |
||
120 | ], |
||
121 | 'set-mail-settings' => [ |
||
122 | 'class' => SmartUpdateAction::class, |
||
123 | 'view' => '_setMailSettings', |
||
124 | 'collection' => [ |
||
125 | 'class' => Collection::class, |
||
126 | 'model' => new AccountValues(['scenario' => 'set-mail-settings']), |
||
127 | ], |
||
128 | 'on beforeFetchLoad' => function (Event $event): void { |
||
129 | $event->sender->getDataProvider()->query->withValues(); |
||
130 | }, |
||
131 | 'success' => Yii::t('hipanel:hosting', 'Mail settings where changed'), |
||
132 | 'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change mail settings'), |
||
133 | ], |
||
134 | 'set-system-settings' => [ |
||
135 | 'class' => SmartUpdateAction::class, |
||
136 | 'view' => '_setSystemSettings', |
||
137 | 'success' => Yii::t('hipanel:hosting:account', 'System settings where changed'), |
||
138 | 'error' => Yii::t('hipanel:hosting:account', 'An error occurred when trying to change system settings'), |
||
139 | ], |
||
140 | 'set-ghost-options' => [ |
||
141 | 'class' => SmartUpdateAction::class, |
||
142 | 'view' => '_setGhostOptions', |
||
143 | 'collection' => [ |
||
144 | 'class' => Collection::class, |
||
145 | 'model' => new AccountValues(['scenario' => 'set-ghost-options']), |
||
146 | ], |
||
147 | 'on beforeFetchLoad' => function (Event $event): void { |
||
148 | $event->sender->getDataProvider()->query->withValues(); |
||
149 | }, |
||
150 | 'success' => Yii::t('hipanel:hosting:account', 'Global vhost options where changed'), |
||
151 | 'error' => Yii::t('hipanel:hosting:account', 'An error occurred when trying to change global vhost options'), |
||
152 | ], |
||
153 | 'validate-sgo-form' => [ |
||
154 | 'class' => ValidateFormAction::class, |
||
155 | 'collection' => [ |
||
156 | 'class' => Collection::class, |
||
157 | 'model' => new AccountValues(['scenario' => 'default']), |
||
158 | ], |
||
159 | ], |
||
160 | 'enable-block' => [ |
||
161 | 'class' => SmartUpdateAction::class, |
||
162 | 'success' => Yii::t('hipanel:hosting', 'Account was blocked successfully'), |
||
163 | 'error' => Yii::t('hipanel:hosting', 'Error during the account blocking'), |
||
164 | 'POST html' => [ |
||
165 | 'save' => true, |
||
166 | 'success' => [ |
||
167 | 'class' => RedirectAction::class, |
||
168 | ], |
||
169 | ], |
||
170 | View Code Duplication | 'on beforeSave' => function (Event $event) { |
|
|
|||
171 | /** @var \hipanel\actions\Action $action */ |
||
172 | $action = $event->sender; |
||
173 | $type = Yii::$app->request->post('type'); |
||
174 | $comment = Yii::$app->request->post('comment'); |
||
175 | if (!empty($type)) { |
||
176 | foreach ($action->collection->models as $model) { |
||
177 | $model->setAttributes([ |
||
178 | 'type' => $type, |
||
179 | 'comment' => $comment, |
||
180 | ]); |
||
181 | } |
||
182 | } |
||
183 | }, |
||
184 | ], |
||
185 | 'bulk-enable-block-modal' => [ |
||
186 | 'class' => PrepareBulkAction::class, |
||
187 | 'view' => '_bulkEnableBlock', |
||
188 | 'data' => function ($action, $data) { |
||
189 | return array_merge($data, [ |
||
190 | 'blockReasons' => $this->getBlockReasons(), |
||
191 | ]); |
||
192 | }, |
||
193 | ], |
||
194 | 'disable-block' => [ |
||
195 | 'class' => SmartUpdateAction::class, |
||
196 | 'success' => Yii::t('hipanel:hosting', 'Account was unblocked successfully'), |
||
197 | 'error' => Yii::t('hipanel:hosting', 'Error during the account unblocking'), |
||
198 | 'POST html' => [ |
||
199 | 'save' => true, |
||
200 | 'success' => [ |
||
201 | 'class' => RedirectAction::class, |
||
202 | ], |
||
203 | ], |
||
204 | View Code Duplication | 'on beforeSave' => function (Event $event) { |
|
205 | /** @var \hipanel\actions\Action $action */ |
||
206 | $action = $event->sender; |
||
207 | $type = Yii::$app->request->post('type'); |
||
208 | $comment = Yii::$app->request->post('comment'); |
||
209 | if (!empty($type)) { |
||
210 | foreach ($action->collection->models as $model) { |
||
211 | $model->setAttribute('comment', $comment); |
||
212 | } |
||
213 | } |
||
214 | }, |
||
215 | ], |
||
216 | 'bulk-disable-block-modal' => [ |
||
217 | 'class' => PrepareBulkAction::class, |
||
218 | 'scenario' => 'disable-block', |
||
219 | 'view' => '_bulkDisableBlock', |
||
220 | 'data' => function ($action, $data) { |
||
221 | return array_merge($data, [ |
||
222 | 'blockReasons' => $this->getBlockReasons(), |
||
223 | ]); |
||
224 | }, |
||
225 | ], |
||
226 | 'validate-form' => [ |
||
227 | 'class' => ValidateFormAction::class, |
||
228 | ], |
||
229 | 'single-validate-form' => [ |
||
230 | 'class' => ValidateFormAction::class, |
||
231 | 'validatedInputId' => false, |
||
232 | ], |
||
233 | 'delete' => [ |
||
234 | 'class' => SmartDeleteAction::class, |
||
235 | 'success' => Yii::t('hipanel:hosting', 'Account deleting task has been added to queue'), |
||
236 | 'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to delete account'), |
||
237 | ], |
||
238 | 'bulk-delete-modal' => [ |
||
239 | 'class' => PrepareBulkAction::class, |
||
240 | 'view' => '_bulkDelete', |
||
241 | ], |
||
242 | 'get-directories-list' => [ |
||
243 | 'class' => SearchAction::class, |
||
244 | 'findOptions' => ['with_directories' => true], |
||
245 | 'ajaxResponseFormatter' => function ($action) { |
||
246 | $results = []; |
||
247 | |||
248 | $model = $action->collection->first; |
||
249 | $pathLike = Yii::$app->request->post('path_like'); |
||
250 | |||
251 | foreach ($model['path'] as $path) { |
||
252 | if ($pathLike) { |
||
253 | if (preg_match('|' . $pathLike . '|', $path)) { |
||
254 | array_unshift($results, ['id' => $path, 'text' => $path]); |
||
255 | continue; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | $results[] = ['id' => $path, 'text' => $path]; |
||
260 | } |
||
261 | |||
262 | return $results; |
||
263 | }, |
||
264 | ], |
||
265 | ]); |
||
266 | } |
||
267 | |||
268 | public function getStateData() |
||
272 | |||
273 | public function getTypeData() |
||
277 | } |
||
278 |
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.