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 |
||
28 | class PageController extends \app\backend\components\BackendController |
||
29 | { |
||
30 | |||
31 | const BACKEND_PAGE_EDIT = 'backend-page-edit'; |
||
32 | const BACKEND_PAGE_EDIT_SAVE = 'backend-page-edit-save'; |
||
33 | const BACKEND_PAGE_EDIT_FORM = 'backend-page-edit-form'; |
||
34 | const BACKEND_PAGE_AFTER_SAVE = 'backend-page-after-save'; |
||
35 | |||
36 | public function behaviors() |
||
37 | { |
||
38 | return [ |
||
39 | 'access' => [ |
||
40 | 'class' => AccessControl::className(), |
||
41 | 'rules' => [ |
||
42 | [ |
||
43 | 'allow' => true, |
||
44 | 'roles' => ['content manage'], |
||
45 | ], |
||
46 | ], |
||
47 | ], |
||
48 | ]; |
||
49 | } |
||
50 | |||
51 | View Code Duplication | public function actions() |
|
52 | { |
||
53 | return [ |
||
54 | 'getTree' => [ |
||
55 | 'class' => AdjacencyFullTreeDataAction::className(), |
||
56 | 'class_name' => Page::className(), |
||
57 | 'model_label_attribute' => 'name', |
||
58 | ], |
||
59 | 'addImage' => [ |
||
60 | 'class' => AddImageAction::className(), |
||
61 | ], |
||
62 | 'upload' => [ |
||
63 | 'class' => UploadAction::className(), |
||
64 | 'upload' => 'theme/resources/product-images', |
||
65 | ], |
||
66 | 'remove' => [ |
||
67 | 'class' => RemoveAction::className(), |
||
68 | 'uploadDir' => 'theme/resources/product-images', |
||
69 | ], |
||
70 | 'save-info' => [ |
||
71 | 'class' => SaveInfoAction::className(), |
||
72 | ], |
||
73 | 'property-handler' => [ |
||
74 | 'class' => PropertyHandler::className(), |
||
75 | 'modelName' => Page::className() |
||
76 | ], |
||
77 | 'move' => [ |
||
78 | 'class' => TreeNodeMoveAction::className(), |
||
79 | 'className' => Page::className(), |
||
80 | 'saveAttributes' => ['slug_compiled'], |
||
81 | ], |
||
82 | 'reorder' => [ |
||
83 | 'class' => TreeNodesReorderAction::className(), |
||
84 | 'className' => Page::className(), |
||
85 | ], |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | View Code Duplication | public function actionIndex($parent_id = 1) |
|
111 | |||
112 | public function actionEdit($parent_id, $id = null) |
||
113 | { |
||
114 | $object = Object::getForClass(Page::className()); |
||
115 | |||
116 | /** @var null|Page|HasProperties $model */ |
||
117 | $model = new Page; |
||
118 | $model->published = 1; |
||
119 | if ($id !== null) { |
||
120 | $model = Page::findOne($id); |
||
121 | if ($model === null) { |
||
122 | throw new NotFoundHttpException; |
||
123 | } |
||
124 | } |
||
125 | $model->parent_id = $parent_id; |
||
126 | |||
127 | $event = new BackendEntityEditEvent($model); |
||
128 | $this->trigger(self::BACKEND_PAGE_EDIT, $event); |
||
129 | |||
130 | $post = \Yii::$app->request->post(); |
||
131 | if ($event->isValid && $model->load($post)) { |
||
132 | $saveStateEvent = new BackendEntityEditEvent($model); |
||
133 | $this->trigger(self::BACKEND_PAGE_EDIT_SAVE, $saveStateEvent); |
||
134 | |||
135 | if ($saveStateEvent->isValid && $model->validate()) { |
||
136 | $save_result = $model->save(); |
||
137 | $model->saveProperties($post); |
||
138 | |||
139 | View Code Duplication | if (null !== $view_object = ViewObject::getByModel($model, true)) { |
|
140 | if ($view_object->load($post, 'ViewObject')) { |
||
141 | if ($view_object->view_id <= 0) { |
||
142 | $view_object->delete(); |
||
143 | } else { |
||
144 | $view_object->save(); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | if ($save_result) { |
||
150 | $modelAfterSaveEvent = new BackendEntityEditEvent($model); |
||
151 | $this->trigger(self::BACKEND_PAGE_AFTER_SAVE, $modelAfterSaveEvent); |
||
152 | |||
153 | $this->runAction('save-info', ['model_id' => $model->id]); |
||
154 | Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); |
||
155 | $returnUrl = Yii::$app->request->get('returnUrl', ['/page/backend/index']); |
||
156 | switch (Yii::$app->request->post('action', 'save')) { |
||
157 | case 'next': |
||
158 | return $this->redirect( |
||
159 | [ |
||
160 | '/page/backend/edit', |
||
161 | 'returnUrl' => $returnUrl, |
||
162 | 'parent_id' => Yii::$app->request->get('parent_id', null) |
||
163 | ] |
||
164 | ); |
||
165 | case 'back': |
||
166 | return $this->redirect($returnUrl); |
||
167 | default: |
||
168 | return $this->redirect( |
||
169 | Url::toRoute( |
||
170 | [ |
||
171 | '/page/backend/edit', |
||
172 | 'id' => $model->id, |
||
173 | 'returnUrl' => $returnUrl, |
||
174 | 'parent_id' => $model->parent_id |
||
175 | ] |
||
176 | ) |
||
177 | ); |
||
178 | } |
||
179 | } else { |
||
180 | \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return $this->render( |
||
186 | 'page-form', |
||
187 | [ |
||
188 | 'model' => $model, |
||
189 | 'object' => $object, |
||
190 | ] |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | /* |
||
195 | * |
||
196 | */ |
||
197 | public function actionDelete($id = null) |
||
216 | |||
217 | public function actionRemoveAll($parent_id) |
||
218 | { |
||
219 | $items = Yii::$app->request->post('items', []); |
||
229 | |||
230 | /* |
||
231 | * |
||
232 | */ |
||
233 | public function actionRestore($id = null, $parent_id = null) |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Clone page action. |
||
258 | * @param integer $id |
||
259 | * @param array|string $returnUrl |
||
260 | * @throws \yii\web\NotFoundHttpException |
||
261 | */ |
||
262 | public function actionClone($id, $returnUrl = ['index']) |
||
358 | } |
||
359 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.