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 |
||
19 | class FormController extends Controller |
||
20 | { |
||
21 | |||
22 | public function behaviors() |
||
23 | { |
||
24 | return [ |
||
25 | 'access' => [ |
||
26 | 'class' => AccessControl::className(), |
||
27 | 'rules' => [ |
||
28 | [ |
||
29 | 'allow' => true, |
||
30 | 'roles' => ['form manage'], |
||
31 | ], |
||
32 | ], |
||
33 | ], |
||
34 | ]; |
||
35 | } |
||
36 | |||
37 | View Code Duplication | public function actionIndex() |
|
|
|||
38 | { |
||
39 | $searchModel = new Form(); |
||
40 | $dataProvider = $searchModel->search($_GET); |
||
41 | |||
42 | return $this->render( |
||
43 | 'index', |
||
44 | [ |
||
45 | 'dataProvider' => $dataProvider, |
||
46 | 'searchModel' => $searchModel, |
||
47 | ] |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | public function actionEdit($id = null) |
||
52 | { |
||
53 | $model = new Form(); |
||
54 | if ($id > 0) { |
||
55 | $model = Form::findOne($id); |
||
56 | } |
||
57 | |||
58 | /** @var \app\models\Object $object */ |
||
59 | $object = Object::getForClass(Form::className()); |
||
60 | |||
61 | $propIds = (new Query())->select('property_group_id')->from([ObjectPropertyGroup::tableName()])->where( |
||
62 | [ |
||
63 | 'and', |
||
64 | 'object_id = :object', |
||
65 | 'object_model_id = :id' |
||
66 | ], |
||
67 | [ |
||
68 | ':object' => $object->id, |
||
69 | ':id' => $id |
||
70 | ] |
||
71 | )->column(); |
||
72 | |||
73 | $post = \Yii::$app->request->post(); |
||
74 | $properties = isset($post['Form']['properties']) ? $post['Form']['properties'] : []; |
||
75 | |||
76 | if ($model->load($post) && $model->validate()) { |
||
77 | if ($model->save()) { |
||
78 | $id = $model->id; |
||
79 | $remove = []; |
||
80 | $add = []; |
||
81 | |||
82 | foreach ($propIds as $value) { |
||
83 | $key = array_search($value, $properties); |
||
84 | if ($key === false) { |
||
85 | $remove[] = $value; |
||
86 | } else { |
||
87 | unset($properties[$key]); |
||
88 | } |
||
89 | } |
||
90 | foreach ($properties as $value) { |
||
91 | $add[] = [ |
||
92 | $value, |
||
93 | $object->id, |
||
94 | $id |
||
95 | ]; |
||
96 | } |
||
97 | |||
98 | Yii::$app->db->createCommand()->delete( |
||
99 | ObjectPropertyGroup::tableName(), |
||
100 | [ |
||
101 | 'and', |
||
102 | 'object_id = :object', |
||
103 | 'object_model_id = :id', |
||
104 | ['in', 'property_group_id', $remove] |
||
105 | ], |
||
106 | [ |
||
107 | ':object' => $object->id, |
||
108 | ':id' => $id, |
||
109 | ] |
||
110 | )->execute(); |
||
111 | |||
112 | View Code Duplication | if (!empty($add)) { |
|
113 | Yii::$app->db->createCommand()->batchInsert( |
||
114 | ObjectPropertyGroup::tableName(), |
||
115 | ['property_group_id', 'object_id', 'object_model_id'], |
||
116 | $add |
||
117 | )->execute(); |
||
118 | } |
||
119 | |||
120 | \Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); |
||
121 | $returnUrl = Yii::$app->request->get('returnUrl', ['/backend/form/index', 'id' => $model->id]); |
||
122 | switch (Yii::$app->request->post('action', 'save')) { |
||
123 | case 'next': |
||
124 | return $this->redirect( |
||
125 | [ |
||
126 | '/backend/form/edit', |
||
127 | 'returnUrl' => $returnUrl, |
||
128 | ] |
||
129 | ); |
||
130 | case 'back': |
||
131 | return $this->redirect($returnUrl); |
||
132 | default: |
||
133 | return $this->redirect( |
||
134 | Url::toRoute( |
||
135 | [ |
||
136 | '/backend/form/edit', |
||
137 | 'id' => $model->id, |
||
138 | 'returnUrl' => $returnUrl, |
||
139 | ] |
||
140 | ) |
||
141 | ); |
||
142 | } |
||
143 | } else { |
||
144 | \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $items = ArrayHelper::map( |
||
149 | PropertyGroup::find()->where( |
||
150 | 'object_id = :object', |
||
151 | [ |
||
152 | ':object' => $object->id, |
||
153 | ] |
||
154 | )->all(), |
||
155 | 'id', |
||
156 | 'name' |
||
157 | ); |
||
158 | |||
159 | return $this->render( |
||
160 | 'edit', |
||
161 | [ |
||
162 | 'model' => $model, |
||
163 | 'items' => $items, |
||
164 | 'selected' => $propIds, |
||
165 | ] |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | public function actionView($id, $show_deleted = 0) |
||
170 | { |
||
171 | $form = Form::findById($id); |
||
172 | $propertyGroups = $form->getPropertyGroups(); |
||
173 | $submission = new Submission(); |
||
174 | |||
175 | $dynamicModel = new DynamicSearchModel($submission, $propertyGroups); |
||
176 | $data = $dynamicModel->search(Yii::$app->request->get()); |
||
177 | $data->query->andWhere('form_id = :form_id', [':form_id' => $form->id]); |
||
178 | $data->query->andWhere(['is_deleted' => $show_deleted]); |
||
179 | $data->query->andFilterWhere(['sending_status' => $dynamicModel->sending_status]); |
||
180 | $data->query->andFilterWhere(['like', 'ip', $dynamicModel->ip]); |
||
181 | $data->query->andFilterWhere(['like', 'user_agent', $dynamicModel->user_agent]); |
||
182 | |||
183 | |||
184 | return $this->render( |
||
185 | 'view', |
||
186 | [ |
||
187 | 'searchModel' => $dynamicModel, |
||
188 | 'dataProvider' => $data, |
||
189 | 'form' => $form, |
||
190 | ] |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | public function actionViewSubmission($id) |
||
195 | { |
||
196 | $submission = Submission::findOne($id); |
||
197 | if ($submission === null) { |
||
198 | throw new NotFoundHttpException('Submission not found'); |
||
199 | } |
||
200 | |||
201 | return $this->render( |
||
202 | 'view-submission', |
||
203 | [ |
||
204 | 'submission' => $submission, |
||
205 | ] |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | public function actionDeleteSubmission($id) |
||
210 | { |
||
211 | $submission = Submission::findOne($id); |
||
212 | $form_id = $submission->form_id; |
||
213 | if ($submission === null) { |
||
214 | throw new NotFoundHttpException('Submission not found'); |
||
215 | } |
||
216 | $res = false; |
||
217 | if ($submission->is_deleted == 1) { |
||
218 | if ($submission->delete()) { |
||
219 | $res = true; |
||
220 | } |
||
221 | } else { |
||
222 | $submission->setAttribute('is_deleted', 1); |
||
223 | if ($submission->save()) { |
||
224 | $res = true; |
||
225 | } |
||
226 | } |
||
227 | View Code Duplication | if ($res === false) { |
|
228 | Yii::$app->session->setFlash('error', Yii::t('app', 'Object not removed')); |
||
229 | } else { |
||
230 | Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed')); |
||
231 | } |
||
232 | return $this->redirect(Url::toRoute(['view', 'id' => $form_id])); |
||
233 | } |
||
234 | |||
235 | public function actionRestoreSubmission($id) |
||
236 | { |
||
237 | $submission = Submission::findOne($id); |
||
238 | if ($submission === null) { |
||
239 | throw new NotFoundHttpException('Submission not found'); |
||
240 | } |
||
241 | $submission->setAttribute('is_deleted', 0); |
||
242 | $submission->save(); |
||
243 | Yii::$app->session->setFlash('success', Yii::t('app', 'Object successfully restored')); |
||
244 | |||
245 | return $this->redirect(Url::toRoute(['view', 'id' => $submission->form_id])); |
||
246 | } |
||
247 | |||
248 | View Code Duplication | public function actionDownload($key, $submissionId) |
|
249 | { |
||
250 | $submission = Submission::findOne($submissionId); |
||
251 | if ($submission === null) { |
||
252 | throw new NotFoundHttpException('Submission not found'); |
||
253 | } |
||
254 | $prop = $submission->getPropertyValuesByKey($key); |
||
255 | return \Yii::$app->response->sendFile( |
||
256 | Yii::getAlias(Yii::$app->getModule('core')->visitorsFileUploadPath) . |
||
257 | DIRECTORY_SEPARATOR . |
||
258 | $prop->values[0]['value'] |
||
259 | ); |
||
260 | } |
||
261 | |||
262 | View Code Duplication | public function actionDelete($id = null) |
|
276 | |||
277 | public function actionRemoveAll() |
||
278 | { |
||
279 | $items = Yii::$app->request->post('items', []); |
||
280 | if (!empty($items)) { |
||
289 | } |
||
290 |
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: