| Conditions | 10 |
| Paths | 15 |
| Total Lines | 61 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 2 | 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 |
||
| 21 | public function init() |
||
|
|
|||
| 22 | { |
||
| 23 | $cacheKey = 'priceRangeCategory' . $this->categoryId; |
||
| 24 | |||
| 25 | if (!$data = Yii::$app->cache->get($cacheKey)) { |
||
| 26 | $dataMin = (new Query())->select([ |
||
| 27 | 'MIN(product.price / currency.convert_nominal * currency.convert_rate) AS min_price', |
||
| 28 | 'product.currency_id AS min_currency', |
||
| 29 | ]) |
||
| 30 | ->from(['product', 'product_category', 'currency']) |
||
| 31 | ->where('product.id = product_category.object_model_id AND (product.currency_id = currency.id)') |
||
| 32 | ->andWhere( |
||
| 33 | [ |
||
| 34 | 'product.active' => 1, |
||
| 35 | 'product_category.category_id' => $this->categoryId |
||
| 36 | ] |
||
| 37 | )->one(); |
||
| 38 | |||
| 39 | $dataMax = (new Query())->select([ |
||
| 40 | 'MAX(product.price / currency.convert_nominal * currency.convert_rate) AS max_price', |
||
| 41 | 'product.currency_id AS max_currency', |
||
| 42 | ]) |
||
| 43 | ->from(['product', 'product_category', 'currency']) |
||
| 44 | ->where('product.id = product_category.object_model_id AND (product.currency_id = currency.id)') |
||
| 45 | ->andWhere( |
||
| 46 | [ |
||
| 47 | 'product.active' => 1, |
||
| 48 | 'product_category.category_id' => $this->categoryId |
||
| 49 | ] |
||
| 50 | )->one(); |
||
| 51 | |||
| 52 | $data = ArrayHelper::merge($dataMax, $dataMin); |
||
| 53 | if ($data) { |
||
| 54 | $data['min_price'] = (int) $data['min_price']; |
||
| 55 | $data['max_price'] = (int) $data['max_price']; |
||
| 56 | Yii::$app->cache->set($cacheKey, $data, 86400); |
||
| 57 | } |
||
| 58 | |||
| 59 | } |
||
| 60 | if ($data && isset($data['min_price']) && isset($data['max_price'])) { |
||
| 61 | $this->minValue = $data['min_price']; |
||
| 62 | $this->maxValue = $data['max_price']; |
||
| 63 | $get = ArrayHelper::merge(Yii::$app->request->get(), Yii::$app->request->post()); |
||
| 64 | |||
| 65 | if (isset($get[$this->minAttribute]) && is_numeric($get[$this->minAttribute])) { |
||
| 66 | $this->changeFlagDefaultValue = 1; |
||
| 67 | $this->minValueNow = $get[$this->minAttribute]; |
||
| 68 | } else { |
||
| 69 | $this->minValueNow = $this->minValue; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (isset($get[$this->maxAttribute]) && is_numeric($get[$this->maxAttribute])) { |
||
| 73 | $this->changeFlagDefaultValue = 1; |
||
| 74 | $this->maxValueNow = $get[$this->maxAttribute]; |
||
| 75 | } else { |
||
| 76 | $this->maxValueNow = $this->maxValue; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | return parent::init(); |
||
| 80 | |||
| 81 | } |
||
| 82 | |||
| 84 | } |
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.