| Conditions | 42 |
| Paths | 68 |
| Total Lines | 227 |
| Lines | 58 |
| Ratio | 25.55 % |
| 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 |
||
| 192 | public function widgetRun() |
||
| 193 | { |
||
| 194 | $categoryId = Yii::$app->request->get('last_category_id'); |
||
| 195 | if ($categoryId === null) { |
||
| 196 | return '<!-- no category_id specified for FilterSet widget in request -->'; |
||
| 197 | } |
||
| 198 | $filterSets = FilterSets::getForCategoryId($categoryId); |
||
| 199 | if (count($filterSets) === 0) { |
||
| 200 | return '<!-- no filter sets for current category -->'; |
||
| 201 | } |
||
| 202 | if ($this->header === '') { |
||
| 203 | $this->header = Yii::t('app', 'Filters'); |
||
| 204 | } |
||
| 205 | |||
| 206 | // Begin of a new filter sets implementation |
||
| 207 | if ($this->useNewFilter) { |
||
| 208 | $urlParams = [ |
||
| 209 | '@category', |
||
| 210 | 'last_category_id' => $categoryId, |
||
| 211 | 'properties' => Yii::$app->request->get('properties', []), |
||
| 212 | 'category_group_id' => Yii::$app->request->get('category_group_id', 0), |
||
| 213 | ]; |
||
| 214 | $priceMin = empty(Yii::$app->request->post('price_min')) ? Yii::$app->request->get('price_min') : Yii::$app->request->post('price_min'); |
||
| 215 | $priceMax = empty(Yii::$app->request->post('price_max')) ? Yii::$app->request->get('price_max') : Yii::$app->request->post('price_max'); |
||
| 216 | if (false === empty($priceMin)) { |
||
| 217 | $urlParams['price_min'] = $priceMin; |
||
| 218 | } |
||
| 219 | if (false === empty($priceMax)) { |
||
| 220 | $urlParams['price_max'] = $priceMax; |
||
| 221 | } |
||
| 222 | $urlParams = $this->mergeUrlProperties($urlParams, ['properties' => Yii::$app->request->post('properties', [])]); |
||
| 223 | $urlParams = $this->removeLostDependencies($filterSets, $urlParams); |
||
| 224 | $properties = $urlParams['properties']; |
||
| 225 | $newGet = Yii::$app->request->get(); |
||
| 226 | $newGet['properties'] = $properties; |
||
| 227 | Yii::$app->request->setQueryParams($newGet); |
||
| 228 | ksort($properties); |
||
| 229 | $cacheKey = 'FilterSets:' . $categoryId . ':' . Json::encode($urlParams); |
||
| 230 | unset($properties); |
||
| 231 | if (false === $filtersArray = Yii::$app->cache->get($cacheKey)) { |
||
| 232 | $filtersArray = []; |
||
| 233 | foreach ($filterSets as $filterSet) { |
||
| 234 | /** Если eav и слайдер, то фильтр формируется по особым правилам */ |
||
| 235 | View Code Duplication | if ($filterSet->is_range_slider && $filterSet->property->is_eav) { |
|
| 236 | $filter = $this->getEavSliderFilter($filterSet->property, $urlParams); |
||
| 237 | if($filter) { |
||
| 238 | $filtersArray[] = $filter; |
||
| 239 | } |
||
| 240 | continue; |
||
| 241 | } |
||
| 242 | |||
| 243 | View Code Duplication | if ($filterSet->is_range_slider && $filterSet->property->is_column_type_stored) { |
|
| 244 | $filter = $this->getTableInheritanceFilter($filterSet->property, $urlParams); |
||
| 245 | if ($filter) { |
||
| 246 | $filtersArray[] = $filter; |
||
| 247 | } |
||
| 248 | continue; |
||
| 249 | } |
||
| 250 | |||
| 251 | if ($filterSet->property->has_static_values === 0) { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ( |
||
| 256 | !empty($filterSet->property->depends_on_property_id) |
||
| 257 | && !empty($filterSet->property->depended_property_values) |
||
| 258 | && $this->getSelectedPropertyIndex( |
||
| 259 | $urlParams['properties'], |
||
| 260 | $filterSet->property->depends_on_property_id, |
||
| 261 | $filterSet->property->depended_property_values |
||
| 262 | ) === false |
||
| 263 | ) { |
||
| 264 | continue; |
||
| 265 | } |
||
| 266 | |||
| 267 | $selections = PropertyStaticValues::getValuesForFilter( |
||
| 268 | $filterSet->property->id, |
||
| 269 | $urlParams['last_category_id'], |
||
| 270 | $urlParams['properties'], |
||
| 271 | $filterSet->multiple, |
||
| 272 | Yii::$app->getModule('shop')->productsFilteringMode |
||
| 273 | ); |
||
| 274 | if (count($selections) === 0) { |
||
| 275 | continue; |
||
| 276 | } |
||
| 277 | $item = [ |
||
| 278 | 'id' => $filterSet->property->id, |
||
| 279 | 'name' => $filterSet->property->name, |
||
| 280 | 'sort_order' => $filterSet->property->sort_order, |
||
| 281 | 'isRange' => $filterSet->is_range_slider, |
||
| 282 | 'selections' => [], |
||
| 283 | 'multiple' => $filterSet->multiple, |
||
| 284 | ]; |
||
| 285 | if ($filterSet->is_range_slider) { |
||
| 286 | $item['max'] = PHP_INT_MIN; |
||
| 287 | $item['min'] = PHP_INT_MAX; |
||
| 288 | $item['property'] = $filterSet->property; |
||
| 289 | } |
||
| 290 | foreach ($selections as $selection) { |
||
| 291 | if ($filterSet->is_range_slider) { |
||
| 292 | View Code Duplication | if ((int)$selection['value'] > $item['max']) { |
|
| 293 | $item['max'] = (int)$selection['value']; |
||
| 294 | } |
||
| 295 | View Code Duplication | if ((int)$selection['value'] < $item['min']) { |
|
| 296 | $item['min'] = (int)$selection['value']; |
||
| 297 | } |
||
| 298 | } elseif($item['multiple']) { |
||
| 299 | $selectedPropertyIndex = $this->getSelectedPropertyIndex( |
||
| 300 | $urlParams['properties'], |
||
| 301 | $filterSet->property_id, |
||
| 302 | $selection['id'] |
||
| 303 | ); |
||
| 304 | View Code Duplication | if ($selectedPropertyIndex !== false) { |
|
| 305 | if (count($urlParams['properties'][$filterSet->property_id]) > 1) { |
||
| 306 | $routeParams = $urlParams; |
||
| 307 | unset($routeParams['properties'][$filterSet->property_id][$selectedPropertyIndex]); |
||
| 308 | } else { |
||
| 309 | $routeParams = $urlParams; |
||
| 310 | unset($routeParams['properties'][$filterSet->property_id]); |
||
| 311 | } |
||
| 312 | if (isset($this->toUnset[$filterSet->property_id])) { |
||
| 313 | foreach ($this->toUnset[$filterSet->property_id] as $id) { |
||
| 314 | unset($routeParams['properties'][$id]); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } else { |
||
| 318 | $routeParams = $this->mergeUrlProperties( |
||
| 319 | $urlParams, |
||
| 320 | ['properties' => [$filterSet->property_id => [$selection['id']]]] |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | $item['selections'][] = [ |
||
| 324 | 'id' => $selection['id'], |
||
| 325 | 'checked' => $selectedPropertyIndex !== false, |
||
| 326 | 'label' => $selection['name'], |
||
| 327 | 'url' => Url::toRoute($routeParams), |
||
| 328 | 'active' => $selection['active'], |
||
| 329 | ]; |
||
| 330 | } else { |
||
| 331 | $selectedPropertyIndex = $this->getSelectedPropertyIndex( |
||
| 332 | $urlParams['properties'], |
||
| 333 | $filterSet->property_id, |
||
| 334 | $selection['id'] |
||
| 335 | ); |
||
| 336 | View Code Duplication | if ($selectedPropertyIndex !== false) { |
|
| 337 | if (count($urlParams['properties'][$filterSet->property_id]) > 1) { |
||
| 338 | $routeParams = $urlParams; |
||
| 339 | unset($routeParams['properties'][$filterSet->property_id][$selectedPropertyIndex]); |
||
| 340 | } else { |
||
| 341 | $routeParams = $urlParams; |
||
| 342 | unset($routeParams['properties'][$filterSet->property_id]); |
||
| 343 | } |
||
| 344 | if (isset($this->toUnset[$filterSet->property_id])) { |
||
| 345 | foreach ($this->toUnset[$filterSet->property_id] as $id) { |
||
| 346 | unset($routeParams['properties'][$id]); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } else { |
||
| 350 | $routeParams = $this->mergeUrlProperties( |
||
| 351 | $urlParams, |
||
| 352 | ['properties' => [$filterSet->property_id => [$selection['id']]]] |
||
| 353 | ); |
||
| 354 | } |
||
| 355 | $item['selections'][] = [ |
||
| 356 | 'id' => $selection['id'], |
||
| 357 | 'checked' => $selectedPropertyIndex !== false, |
||
| 358 | 'label' => $selection['name'], |
||
| 359 | 'url' => $selection['active'] === true || $selectedPropertyIndex !== false |
||
| 360 | ? Url::toRoute($routeParams) |
||
| 361 | : null, |
||
| 362 | 'active' => $selection['active'], |
||
| 363 | ]; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | if ($filterSet->is_range_slider) { |
||
| 367 | if ($item['min'] === $item['max']) { |
||
| 368 | continue; |
||
| 369 | } |
||
| 370 | $i = 1; |
||
| 371 | $n = $item['max'] - $item['min']; |
||
| 372 | while ($n >= 10) { |
||
| 373 | $n /= 10; |
||
| 374 | $i++; |
||
| 375 | } |
||
| 376 | $item['step'] = $i > 3 ? 10 ** ($i - 3) : 1; |
||
| 377 | unset($i, $n); |
||
| 378 | } |
||
| 379 | $filtersArray[] = $item; |
||
| 380 | unset($item); |
||
| 381 | } |
||
| 382 | $product = Yii::$container->get(Product::class); |
||
| 383 | Yii::$app->cache->set( |
||
| 384 | $cacheKey, |
||
| 385 | $filtersArray, |
||
| 386 | 86400, |
||
| 387 | new TagDependency( |
||
| 388 | [ |
||
| 389 | 'tags' => [ |
||
| 390 | ActiveRecordHelper::getCommonTag(FilterSets::class), |
||
| 391 | ActiveRecordHelper::getCommonTag(get_class($product)), |
||
| 392 | ActiveRecordHelper::getCommonTag(Property::class), |
||
| 393 | ], |
||
| 394 | ] |
||
| 395 | ) |
||
| 396 | ); |
||
| 397 | } |
||
| 398 | return $this->render( |
||
| 399 | 'filter-sets-new', |
||
| 400 | [ |
||
| 401 | 'filtersArray' => $filtersArray, |
||
| 402 | 'id' => 'filter-set-' . $this->id, |
||
| 403 | 'urlParams' => $urlParams, |
||
| 404 | 'usePjax' => $this->usePjax, |
||
| 405 | ] |
||
| 406 | ); |
||
| 407 | } |
||
| 408 | // End of a new filter sets implementation |
||
| 409 | |||
| 410 | return $this->render( |
||
| 411 | $this->viewFile, |
||
| 412 | [ |
||
| 413 | 'filterSets' => $filterSets, |
||
| 414 | 'id' => 'filter-set-' . $this->id, |
||
| 415 | 'hideEmpty' => $this->hideEmpty, |
||
| 416 | ] |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | } |
||
| 420 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.