| Conditions | 15 |
| Paths | 56 |
| Total Lines | 89 |
| Code Lines | 63 |
| Lines | 14 |
| Ratio | 15.73 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 118 | protected function editPrices($data, $context) { |
||
| 119 | $calculator = $this->getCalculator(); |
||
| 120 | $selectedField = Yii::$app->request->post('apply_for'); |
||
| 121 | $value = Yii::$app->request->post('value'); |
||
| 122 | $type = Yii::$app->request->post('type'); |
||
| 123 | $currencyId = Yii::$app->request->post('currency_id'); |
||
| 124 | $this->pRound = [ |
||
| 125 | 'is_round' => Yii::$app->request->post('is_round'), |
||
| 126 | 'round_val' => Yii::$app->request->post('round_val'), |
||
| 127 | ]; |
||
| 128 | |||
| 129 | $report = [ |
||
| 130 | 'all' => 0, |
||
| 131 | 'success' => 0, |
||
| 132 | 'error' => 0, |
||
| 133 | 'skipped' => 0, |
||
| 134 | 'errors' => [] |
||
| 135 | ]; |
||
| 136 | |||
| 137 | if ($context == 'backend-product') |
||
| 138 | $sql = ['in', 'id', $data]; |
||
| 139 | else |
||
| 140 | $sql = ['in', 'main_category_id', $this->getParentCategories($data)]; |
||
| 141 | |||
| 142 | $items = Product::find() |
||
| 143 | ->select(['id', 'name', 'currency_id', 'price', 'old_price']) |
||
| 144 | ->where($sql) |
||
| 145 | ->asArray() |
||
| 146 | ->all(); |
||
| 147 | |||
| 148 | foreach ($items as $item){ |
||
| 149 | if ($item['currency_id'] != $currencyId) { |
||
| 150 | $report['skipped']++; |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | |||
| 154 | // change prices |
||
| 155 | $fError = false; |
||
| 156 | $errorKey = '[' . $item['id'] . '] ' . $item['name']; |
||
| 157 | $calcPrice = $item['price']; |
||
| 158 | $calcOldPrice = $item['old_price']; |
||
| 159 | |||
| 160 | if ($type == 'normal') { |
||
| 161 | // price |
||
| 162 | View Code Duplication | if ($selectedField == 'price' || $selectedField == 'all') { |
|
| 163 | $calcPrice = $calculator($calcPrice, $value); |
||
| 164 | if (!$this->checkAndRound($calcPrice)) { |
||
| 165 | $fError = true; |
||
| 166 | $report['errors'][$errorKey][Yii::t('app', 'Price')] = Yii::t('app', 'Сalculated value is less than zero'); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | // old price |
||
| 170 | View Code Duplication | if ($selectedField == 'old_price' || $selectedField == 'all') { |
|
| 171 | $calcOldPrice = $calculator($calcOldPrice, $value); |
||
| 172 | if (!$this->checkAndRound($calcOldPrice)) { |
||
| 173 | $fError = true; |
||
| 174 | $report['errors'][$errorKey][Yii::t('app', 'Old Price')] = Yii::t('app', 'Сalculated value is less than zero'); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } else { // type == relative |
||
| 178 | if ($selectedField == 'price') { |
||
| 179 | $calcOldPrice = $calculator($calcPrice, $value); |
||
| 180 | if (!$this->checkAndRound($calcOldPrice)) { |
||
| 181 | $fError = true; |
||
| 182 | $report['errors'][$errorKey][Yii::t('app', 'Old Price')] = Yii::t('app', 'Сalculated value is less than zero'); |
||
| 183 | } |
||
| 184 | } else { |
||
| 185 | $calcPrice = $calculator($calcOldPrice, $value); |
||
| 186 | if (!$this->checkAndRound($calcPrice)) { |
||
| 187 | $fError = true; |
||
| 188 | $report['errors'][$errorKey][Yii::t('app', 'Price')] = Yii::t('app', 'Сalculated value is less than zero'); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | $report['all']++; |
||
| 194 | if ($fError) { |
||
| 195 | $report['error']++; |
||
| 196 | } else { |
||
| 197 | $report['success']++; |
||
| 198 | Product::updateAll( |
||
| 199 | ['price' => $calcPrice, 'old_price' => $calcOldPrice], |
||
| 200 | ['id' => $item['id']] |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | return $report; |
||
| 206 | } |
||
| 207 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.