Conditions | 17 |
Paths | 57 |
Total Lines | 96 |
Code Lines | 61 |
Lines | 9 |
Ratio | 9.38 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
111 | public function actionEdit($parent_id = null, $id = null) |
||
112 | { |
||
113 | if (null === $parent_id) { |
||
114 | throw new NotFoundHttpException; |
||
115 | } |
||
116 | |||
117 | if (null === $object = Object::getForClass(Category::className())) { |
||
118 | throw new ServerErrorHttpException; |
||
119 | } |
||
120 | |||
121 | /** @var null|Category|HasProperties $model */ |
||
122 | $model = null; |
||
123 | if (null !== $id) { |
||
124 | $model = Category::findById($id, null, null); |
||
125 | } else { |
||
126 | $parent = Category::findById($parent_id, null, null); |
||
127 | if ($parent_id === '0' || !is_null($parent)) { |
||
128 | $model = new Category; |
||
129 | $model->loadDefaultValues(); |
||
130 | $model->parent_id = $parent_id; |
||
131 | if ($parent_id !== '0') { |
||
132 | $model->category_group_id = $parent->category_group_id; |
||
133 | } |
||
134 | } else { |
||
135 | throw new ServerErrorHttpException; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if (null === $model) { |
||
140 | throw new ServerErrorHttpException; |
||
141 | } |
||
142 | |||
143 | $event = new BackendEntityEditEvent($model); |
||
144 | $this->trigger(self::BACKEND_CATEGORY_EDIT, $event); |
||
145 | |||
146 | $post = \Yii::$app->request->post(); |
||
147 | if ($event->isValid && $model->load($post) && $model->validate()) { |
||
148 | $saveStateEvent = new BackendEntityEditEvent($model); |
||
149 | $this->trigger(self::BACKEND_CATEGORY_EDIT_SAVE, $saveStateEvent); |
||
150 | |||
151 | $save_result = $model->save(); |
||
152 | $model->saveProperties($post); |
||
153 | |||
154 | View Code Duplication | if (null !== $view_object = ViewObject::getByModel($model, true)) { |
|
155 | if ($view_object->load($post, 'ViewObject')) { |
||
156 | if ($view_object->view_id <= 0) { |
||
157 | $view_object->delete(); |
||
158 | } else { |
||
159 | $view_object->save(); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if ($save_result) { |
||
165 | $modelAfterSaveEvent = new BackendEntityEditEvent($model); |
||
166 | $this->trigger(self::BACKEND_CATEGORY_AFTER_SAVE, $modelAfterSaveEvent); |
||
167 | |||
168 | $this->runAction('save-info', ['model_id'=>$model->id]); |
||
169 | Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
||
170 | $returnUrl = Yii::$app->request->get('returnUrl', ['index']); |
||
171 | switch (Yii::$app->request->post('action', 'save')) { |
||
172 | case 'next': |
||
173 | return $this->redirect( |
||
174 | [ |
||
175 | 'edit', |
||
176 | 'returnUrl' => $returnUrl, |
||
177 | 'parent_id' => Yii::$app->request->get('parent_id', null) |
||
178 | ] |
||
179 | ); |
||
180 | case 'back': |
||
181 | return $this->redirect($returnUrl); |
||
182 | default: |
||
183 | return $this->redirect( |
||
184 | Url::toRoute( |
||
185 | [ |
||
186 | 'edit', |
||
187 | 'id' => $model->id, |
||
188 | 'returnUrl' => $returnUrl, |
||
189 | 'parent_id' => $model->parent_id |
||
190 | ] |
||
191 | ) |
||
192 | ); |
||
193 | } |
||
194 | } else { |
||
195 | throw new ServerErrorHttpException; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return $this->render( |
||
200 | 'category-form', |
||
201 | [ |
||
202 | 'model' => $model, |
||
203 | 'object' => $object, |
||
204 | ] |
||
205 | ); |
||
206 | } |
||
207 | |||
268 |
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.