| Conditions | 23 |
| Paths | 140 |
| Total Lines | 98 |
| Code Lines | 60 |
| Lines | 16 |
| Ratio | 16.33 % |
| 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 |
||
| 36 | protected function _run() { |
||
| 37 | $actionParams = $this->actionParams; |
||
| 38 | if ($this->actionParams && (!empty($actionParams->get['id']) || !empty($actionParams->post['id']))) { |
||
| 39 | $mode = $this->mode; |
||
| 40 | } else { |
||
| 41 | $mode = 'edit'; |
||
| 42 | } |
||
| 43 | |||
| 44 | if (!empty($this->getData()['view'])) { |
||
| 45 | $mode = 'view'; |
||
| 46 | } |
||
| 47 | |||
| 48 | $model = $this->getModel(); |
||
| 49 | if (!$model) { |
||
| 50 | throw new NotFoundHttpException('Record not founded'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->trigger(self::EVENT_AFTER_FIND); |
||
| 54 | if ($this->scenario !== null) { |
||
| 55 | $model->setScenario($this->scenario); |
||
| 56 | } |
||
| 57 | |||
| 58 | $isNewRecord = $model->isNewRecord; |
||
| 59 | $result = parent::loadAndValidateForm(); |
||
|
|
|||
| 60 | if (is_array($result)) { |
||
| 61 | $response = $this->getResponse([ |
||
| 62 | 'content' => $result |
||
| 63 | ]); |
||
| 64 | if ($this->actionParams->isAjax && !$this->actionParams->isPjax) { |
||
| 65 | $response->format = \yii\web\Response::FORMAT_JSON; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $response; |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($result === true && $this->isSave() && $this->isSubmitted()) { |
||
| 72 | $model->save(); |
||
| 73 | $this->trigger('afterSave'); |
||
| 74 | if ($isNewRecord) { |
||
| 75 | $this->trigger('afterCreate'); |
||
| 76 | } else { |
||
| 77 | $this->trigger('afterUpdate'); |
||
| 78 | } |
||
| 79 | |||
| 80 | $successMessage = $this->getSuccessMessage($isNewRecord); |
||
| 81 | $this->setFlash($successMessage); |
||
| 82 | View Code Duplication | if ($this->actionParams->isAjax && !$this->actionParams->isPjax) { |
|
| 83 | return $this->getResponse([ |
||
| 84 | 'format' => \yii\web\Response::FORMAT_JSON, |
||
| 85 | 'content' => [ |
||
| 86 | 'message' => $this->getSuccessMessage($isNewRecord), |
||
| 87 | ], |
||
| 88 | ]); |
||
| 89 | } |
||
| 90 | |||
| 91 | $result = $this->redirectAfterSave(); |
||
| 92 | if ($result === false) { |
||
| 93 | $result = [ |
||
| 94 | 'mode' => $mode, |
||
| 95 | 'model' => $model |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | } else { |
||
| 99 | View Code Duplication | if ($this->actionParams->isAjax && !$this->actionParams->isPjax) { |
|
| 100 | return $this->getResponse([ |
||
| 101 | 'format' => \yii\web\Response::FORMAT_JSON, |
||
| 102 | 'content' => [ |
||
| 103 | 'success' => true, |
||
| 104 | ], |
||
| 105 | ]); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!empty($model->errors)) { |
||
| 109 | $flash = Html::errorSummary($model, [ |
||
| 110 | 'encode' => false, |
||
| 111 | ]); |
||
| 112 | |||
| 113 | $this->setFlash($flash, 'danger'); |
||
| 114 | } |
||
| 115 | |||
| 116 | $result = [ |
||
| 117 | 'mode' => $mode, |
||
| 118 | 'model' => $model |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | if (\yii::$app->has('db') && $t = \yii::$app->db->transaction) { |
||
| 123 | while ($t->getIsActive()) { |
||
| 124 | $t->commit(); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $response = $this->getResponse([ |
||
| 129 | 'content' => $result, |
||
| 130 | ]); |
||
| 131 | |||
| 132 | return $response; |
||
| 133 | } |
||
| 134 | |||
| 352 | } |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.