| Conditions | 15 |
| Paths | 145 |
| Total Lines | 133 |
| Lines | 24 |
| Ratio | 18.05 % |
| 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 |
||
| 202 | public static function getValuesForFilter($property_id, $category_id, $properties, $multiple = false, $productsFilteringMode = "") |
||
| 203 | { |
||
| 204 | $priceMin = Yii::$app->request->get('price_min'); |
||
| 205 | $priceMax = Yii::$app->request->get('price_max'); |
||
| 206 | $cacheKey = "getValuesForFilter:" . json_encode([$property_id, $category_id, $properties, $priceMin, $priceMax]); |
||
| 207 | if (false === $allSelections = Yii::$app->cache->get($cacheKey)) { |
||
| 208 | |||
| 209 | switch($productsFilteringMode) { |
||
| 210 | case ConfigConfigurationModel::FILTER_PARENTS_ONLY: |
||
| 211 | $joinCondition = 'p.id = {{%product_category}}.object_model_id AND p.active = 1 AND p.parent_id = 0'; |
||
| 212 | break; |
||
| 213 | case ConfigConfigurationModel::FILTER_CHILDREN_ONLY: |
||
| 214 | $joinCondition = 'p.id = {{%product_category}}.object_model_id AND p.active = 1 AND p.parent_id != 0'; |
||
| 215 | break; |
||
| 216 | default: |
||
| 217 | $joinCondition = 'p.id = {{%product_category}}.object_model_id AND p.active = 1'; |
||
| 218 | break; |
||
| 219 | } |
||
| 220 | $product = Yii::$container->get(Product::class); |
||
| 221 | $objectModel = BaseObject::getForClass(get_class($product)); |
||
| 222 | $objectId = $objectModel !== null ? $objectModel->id : 0; |
||
| 223 | $allSelections = static::find() |
||
| 224 | ->asArray(true) |
||
| 225 | ->select([self::tableName() . '.id', self::tableName() . '.name', 'value', self::tableName() . '.slug']) |
||
| 226 | ->innerJoin( |
||
| 227 | ObjectStaticValues::tableName(), |
||
| 228 | ObjectStaticValues::tableName() . '.property_static_value_id=' . self::tableName() . '.id' |
||
| 229 | ) |
||
| 230 | ->innerJoin( |
||
| 231 | '{{%product_category}}', |
||
| 232 | '{{%product_category}}.object_model_id = ' . ObjectStaticValues::tableName() . '.object_model_id' |
||
| 233 | ) |
||
| 234 | ->innerJoin( |
||
| 235 | $product::tableName() . ' p', |
||
| 236 | $joinCondition |
||
| 237 | ) |
||
| 238 | ->where( |
||
| 239 | [ |
||
| 240 | self::tableName() . '.property_id' => $property_id, |
||
| 241 | self::tableName() . '.dont_filter' => 0, |
||
| 242 | '{{%product_category}}.category_id' => $category_id, |
||
| 243 | ] |
||
| 244 | ) |
||
| 245 | ->orderBy( |
||
| 246 | [ |
||
| 247 | self::tableName() . '.sort_order' => SORT_ASC, |
||
| 248 | self::tableName() . '.name' => SORT_ASC, |
||
| 249 | ] |
||
| 250 | ) |
||
| 251 | ->all(); |
||
| 252 | /** @var ActiveQuery $query */ |
||
| 253 | $query = ObjectStaticValues::find() |
||
| 254 | ->distinct(true) |
||
| 255 | ->select(ObjectStaticValues::tableName() . '.object_model_id') |
||
| 256 | ->where(['object_id' => $objectId]); |
||
| 257 | if (false === empty($properties)) { |
||
| 258 | foreach ($properties as $propertyId => $propertyStaticValues) { |
||
| 259 | $subQuery = self::initSubQuery($category_id, $joinCondition); |
||
| 260 | $subQuery->andWhere(['property_static_value_id' => $propertyStaticValues,]); |
||
| 261 | View Code Duplication | $subQueryOptimisation = Yii::$app->db->cache(function($db) use ($subQuery) { |
|
| 262 | $ids = implode(', ', $subQuery->createCommand($db)->queryColumn()); |
||
| 263 | return empty($ids) === true ? '(-1)' : "($ids)"; |
||
| 264 | }, 86400, new TagDependency([ |
||
| 265 | 'tags' => [ |
||
| 266 | ActiveRecordHelper::getCommonTag(ObjectStaticValues::className()), |
||
| 267 | ] |
||
| 268 | ])); |
||
| 269 | $query->andWhere(new Expression('`object_model_id` IN ' . $subQueryOptimisation)); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | if (false === empty($priceMin) && false === empty($priceMax)) { |
||
| 273 | $subQuery = self::initSubQuery($category_id, $joinCondition); |
||
| 274 | $subQuery |
||
| 275 | ->andWhere('p.price >= (:min_price * currency.convert_nominal / currency.convert_rate)', |
||
| 276 | [':min_price' => $priceMin]) |
||
| 277 | ->andWhere('p.price <= (:max_price * currency.convert_nominal / currency.convert_rate)', |
||
| 278 | [':max_price' => $priceMax]) |
||
| 279 | ->leftJoin(Currency::tableName() . ' ON currency.id = p.currency_id'); |
||
| 280 | View Code Duplication | $subQueryOptimisation = Yii::$app->db->cache(function($db) use ($subQuery) { |
|
| 281 | $ids = implode(', ', $subQuery->createCommand($db)->queryColumn()); |
||
| 282 | return empty($ids) === true ? '(-1)' : "($ids)"; |
||
| 283 | }, 86400, new TagDependency([ |
||
| 284 | 'tags' => [ |
||
| 285 | ActiveRecordHelper::getCommonTag(ObjectStaticValues::className()), |
||
| 286 | ] |
||
| 287 | ])); |
||
| 288 | $query->andWhere(new Expression('`object_model_id` IN ' . $subQueryOptimisation)); |
||
| 289 | } |
||
| 290 | $selectedQuery = static::find() |
||
| 291 | ->select(static::tableName() . '.id') |
||
| 292 | ->asArray(true) |
||
| 293 | ->innerJoin( |
||
| 294 | ObjectStaticValues::tableName(), |
||
| 295 | ObjectStaticValues::tableName() . '.property_static_value_id = ' . static::tableName() . '.id' |
||
| 296 | ) |
||
| 297 | ->where([ |
||
| 298 | 'property_id' => $property_id, |
||
| 299 | ]) |
||
| 300 | ->andWhere( |
||
| 301 | new Expression( |
||
| 302 | ObjectStaticValues::tableName() . '.object_model_id IN (' . $query->createCommand()->getRawSql() . ')' |
||
| 303 | ) |
||
| 304 | ); |
||
| 305 | if (false == $multiple) { |
||
| 306 | if (isset($properties[$property_id])) { |
||
| 307 | $selectedQuery->andWhere([self::tableName() . '.id' => $properties[$property_id]]); |
||
| 308 | } |
||
| 309 | } else { |
||
| 310 | unset($properties[$property_id]); |
||
| 311 | } |
||
| 312 | $selected = $selectedQuery->column(); |
||
| 313 | foreach ($allSelections as $index => $selection) { |
||
| 314 | $allSelections[$index]['active'] = in_array($selection['id'], $selected); |
||
| 315 | } |
||
| 316 | View Code Duplication | if (null !== $allSelections) { |
|
| 317 | Yii::$app->cache->set( |
||
| 318 | $cacheKey, |
||
| 319 | $allSelections, |
||
| 320 | 0, |
||
| 321 | new TagDependency( |
||
| 322 | [ |
||
| 323 | 'tags' => [ |
||
| 324 | ActiveRecordHelper::getCommonTag(PropertyStaticValues::className()), |
||
| 325 | ActiveRecordHelper::getCommonTag(Property::className()), |
||
| 326 | ] |
||
| 327 | |||
| 328 | ] |
||
| 329 | ) |
||
| 330 | ); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | return $allSelections; |
||
| 334 | } |
||
| 335 | |||
| 413 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.