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 |
||
| 29 | class PageController extends \app\backend\components\BackendController |
||
| 30 | { |
||
| 31 | |||
| 32 | const BACKEND_PAGE_EDIT = 'backend-page-edit'; |
||
| 33 | const BACKEND_PAGE_EDIT_SAVE = 'backend-page-edit-save'; |
||
| 34 | const BACKEND_PAGE_EDIT_FORM = 'backend-page-edit-form'; |
||
| 35 | const BACKEND_PAGE_AFTER_SAVE = 'backend-page-after-save'; |
||
| 36 | |||
| 37 | View Code Duplication | public function behaviors() |
|
| 38 | { |
||
| 39 | return [ |
||
| 40 | 'access' => [ |
||
| 41 | 'class' => AccessControl::className(), |
||
| 42 | 'rules' => [ |
||
| 43 | [ |
||
| 44 | 'allow' => true, |
||
| 45 | 'roles' => ['content manage'], |
||
| 46 | ], |
||
| 47 | ], |
||
| 48 | ], |
||
| 49 | ]; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function actions() |
||
| 53 | { |
||
| 54 | return [ |
||
| 55 | 'getTree' => [ |
||
| 56 | 'class' => AdjacencyFullTreeDataAction::className(), |
||
| 57 | 'class_name' => Page::className(), |
||
| 58 | 'model_label_attribute' => 'name', |
||
| 59 | ], |
||
| 60 | 'addImage' => [ |
||
| 61 | 'class' => AddImageAction::className(), |
||
| 62 | ], |
||
| 63 | 'upload' => [ |
||
| 64 | 'class' => UploadAction::className(), |
||
| 65 | 'upload' => 'theme/resources/product-images', |
||
| 66 | ], |
||
| 67 | 'remove' => [ |
||
| 68 | 'class' => RemoveAction::className(), |
||
| 69 | 'uploadDir' => 'theme/resources/product-images', |
||
| 70 | ], |
||
| 71 | 'save-info' => [ |
||
| 72 | 'class' => SaveInfoAction::className(), |
||
| 73 | ], |
||
| 74 | 'property-handler' => [ |
||
| 75 | 'class' => PropertyHandler::className(), |
||
| 76 | 'modelName' => Page::className() |
||
| 77 | ], |
||
| 78 | 'move' => [ |
||
| 79 | 'class' => TreeNodeMoveAction::className(), |
||
| 80 | 'className' => Page::className(), |
||
| 81 | 'saveAttributes' => ['slug_compiled'], |
||
| 82 | ], |
||
| 83 | 'reorder' => [ |
||
| 84 | 'class' => TreeNodesReorderAction::className(), |
||
| 85 | 'className' => Page::className(), |
||
| 86 | ], |
||
| 87 | 'publish-switch' => [ |
||
| 88 | 'class' => MassPublishAction::className(), |
||
| 89 | 'modelName' => Page::className(), |
||
| 90 | 'attribute' => 'published', |
||
| 91 | ] |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | View Code Duplication | public function actionIndex($parent_id = 1) |
|
| 117 | |||
| 118 | public function actionEdit($parent_id, $id = null) |
||
| 119 | { |
||
| 120 | $object = Object::getForClass(Page::className()); |
||
| 121 | |||
| 122 | /** @var null|Page|HasProperties $model */ |
||
| 123 | $model = new Page; |
||
| 124 | $model->published = 1; |
||
| 125 | if ($id !== null) { |
||
| 126 | $model = Page::findOne($id); |
||
| 127 | if ($model === null) { |
||
| 128 | throw new NotFoundHttpException; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $model->parent_id = $parent_id; |
||
| 132 | |||
| 133 | $event = new BackendEntityEditEvent($model); |
||
| 134 | $this->trigger(self::BACKEND_PAGE_EDIT, $event); |
||
| 135 | |||
| 136 | $post = \Yii::$app->request->post(); |
||
| 137 | if ($event->isValid && $model->load($post)) { |
||
| 138 | $saveStateEvent = new BackendEntityEditEvent($model); |
||
| 139 | $this->trigger(self::BACKEND_PAGE_EDIT_SAVE, $saveStateEvent); |
||
| 140 | |||
| 141 | if ($saveStateEvent->isValid && $model->validate()) { |
||
| 142 | $save_result = $model->save(); |
||
| 143 | $model->saveProperties($post); |
||
| 144 | |||
| 145 | View Code Duplication | if (null !== $view_object = ViewObject::getByModel($model, true)) { |
|
| 146 | if ($view_object->load($post, 'ViewObject')) { |
||
| 147 | if ($view_object->view_id <= 0) { |
||
| 148 | $view_object->delete(); |
||
| 149 | } else { |
||
| 150 | $view_object->save(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($save_result) { |
||
| 156 | $modelAfterSaveEvent = new BackendEntityEditEvent($model); |
||
| 157 | $this->trigger(self::BACKEND_PAGE_AFTER_SAVE, $modelAfterSaveEvent); |
||
| 158 | |||
| 159 | $this->runAction('save-info', ['model_id' => $model->id]); |
||
| 160 | Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); |
||
| 161 | $returnUrl = Yii::$app->request->get('returnUrl', ['/page/backend/index']); |
||
| 162 | switch (Yii::$app->request->post('action', 'save')) { |
||
| 163 | case 'next': |
||
| 164 | return $this->redirect( |
||
| 165 | [ |
||
| 166 | '/page/backend/edit', |
||
| 167 | 'returnUrl' => $returnUrl, |
||
| 168 | 'parent_id' => Yii::$app->request->get('parent_id', null) |
||
| 169 | ] |
||
| 170 | ); |
||
| 171 | case 'back': |
||
| 172 | return $this->redirect($returnUrl); |
||
| 173 | default: |
||
| 174 | return $this->redirect( |
||
| 175 | Url::toRoute( |
||
| 176 | [ |
||
| 177 | '/page/backend/edit', |
||
| 178 | 'id' => $model->id, |
||
| 179 | 'returnUrl' => $returnUrl, |
||
| 180 | 'parent_id' => $model->parent_id |
||
| 181 | ] |
||
| 182 | ) |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | } else { |
||
| 186 | \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this->render( |
||
| 192 | 'page-form', |
||
| 193 | [ |
||
| 194 | 'model' => $model, |
||
| 195 | 'object' => $object, |
||
| 196 | ] |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | /* |
||
| 201 | * |
||
| 202 | */ |
||
| 203 | public function actionDelete($id = null) |
||
| 222 | |||
| 223 | public function actionRemoveAll($parent_id) |
||
| 224 | { |
||
| 235 | |||
| 236 | /* |
||
| 237 | * |
||
| 238 | */ |
||
| 239 | public function actionRestore($id = null, $parent_id = null) |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Clone page action. |
||
| 264 | * @param integer $id |
||
| 265 | * @param array|string $returnUrl |
||
| 266 | * @throws \yii\web\NotFoundHttpException |
||
| 267 | */ |
||
| 268 | public function actionClone($id, $returnUrl = ['index']) |
||
| 364 | } |
||
| 365 |
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
@returnannotation as described here.