| Conditions | 17 |
| Paths | 57 |
| Total Lines | 96 |
| Code Lines | 61 |
| Lines | 9 |
| Ratio | 9.38 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 115 | public function actionEdit($parent_id = null, $id = null) |
||
| 116 | { |
||
| 117 | if (null === $parent_id) { |
||
| 118 | throw new NotFoundHttpException; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (null === $object = Object::getForClass(Category::className())) { |
||
| 122 | throw new ServerErrorHttpException; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** @var null|Category|HasProperties $model */ |
||
| 126 | $model = null; |
||
| 127 | if (null !== $id) { |
||
| 128 | $model = Category::findById($id, null, null); |
||
| 129 | } else { |
||
| 130 | $parent = Category::findById($parent_id, null, null); |
||
| 131 | if ($parent_id === '0' || !is_null($parent)) { |
||
| 132 | $model = new Category; |
||
| 133 | $model->loadDefaultValues(); |
||
| 134 | $model->parent_id = $parent_id; |
||
| 135 | if ($parent_id !== '0') { |
||
| 136 | $model->category_group_id = $parent->category_group_id; |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | throw new ServerErrorHttpException; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | if (null === $model) { |
||
| 144 | throw new ServerErrorHttpException; |
||
| 145 | } |
||
| 146 | |||
| 147 | $event = new BackendEntityEditEvent($model); |
||
| 148 | $this->trigger(self::BACKEND_CATEGORY_EDIT, $event); |
||
| 149 | |||
| 150 | $post = \Yii::$app->request->post(); |
||
| 151 | if ($event->isValid && $model->load($post) && $model->validate()) { |
||
| 152 | $saveStateEvent = new BackendEntityEditEvent($model); |
||
| 153 | $this->trigger(self::BACKEND_CATEGORY_EDIT_SAVE, $saveStateEvent); |
||
| 154 | |||
| 155 | $save_result = $model->save(); |
||
| 156 | $model->saveProperties($post); |
||
| 157 | |||
| 158 | View Code Duplication | if (null !== $view_object = ViewObject::getByModel($model, true)) { |
|
| 159 | if ($view_object->load($post, 'ViewObject')) { |
||
| 160 | if ($view_object->view_id <= 0) { |
||
| 161 | $view_object->delete(); |
||
| 162 | } else { |
||
| 163 | $view_object->save(); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($save_result) { |
||
| 169 | $modelAfterSaveEvent = new BackendEntityEditEvent($model); |
||
| 170 | $this->trigger(self::BACKEND_CATEGORY_AFTER_SAVE, $modelAfterSaveEvent); |
||
| 171 | |||
| 172 | $this->runAction('save-info', ['model_id'=>$model->id]); |
||
| 173 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
| 174 | $returnUrl = Yii::$app->request->get('returnUrl', ['index']); |
||
| 175 | switch (Yii::$app->request->post('action', 'save')) { |
||
| 176 | case 'next': |
||
| 177 | return $this->redirect( |
||
| 178 | [ |
||
| 179 | 'edit', |
||
| 180 | 'returnUrl' => $returnUrl, |
||
| 181 | 'parent_id' => Yii::$app->request->get('parent_id', null) |
||
| 182 | ] |
||
| 183 | ); |
||
| 184 | case 'back': |
||
| 185 | return $this->redirect($returnUrl); |
||
| 186 | default: |
||
| 187 | return $this->redirect( |
||
| 188 | Url::toRoute( |
||
| 189 | [ |
||
| 190 | 'edit', |
||
| 191 | 'id' => $model->id, |
||
| 192 | 'returnUrl' => $returnUrl, |
||
| 193 | 'parent_id' => $model->parent_id |
||
| 194 | ] |
||
| 195 | ) |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | } else { |
||
| 199 | throw new ServerErrorHttpException; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | return $this->render( |
||
| 204 | 'category-form', |
||
| 205 | [ |
||
| 206 | 'model' => $model, |
||
| 207 | 'object' => $object, |
||
| 208 | ] |
||
| 209 | ); |
||
| 210 | } |
||
| 211 | |||
| 272 |
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.