| Conditions | 39 |
| Paths | 68 |
| Total Lines | 216 |
| Code Lines | 158 |
| Lines | 44 |
| Ratio | 20.37 % |
| 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 |
||
| 142 | public function widgetRun() |
||
| 143 | { |
||
| 144 | $categoryId = Yii::$app->request->get('last_category_id'); |
||
| 145 | if ($categoryId === null) { |
||
| 146 | return '<!-- no category_id specified for FilterSet widget in request -->'; |
||
| 147 | } |
||
| 148 | $filterSets = FilterSets::getForCategoryId($categoryId); |
||
| 149 | if (count($filterSets) === 0) { |
||
| 150 | return '<!-- no filter sets for current category -->'; |
||
| 151 | } |
||
| 152 | if ($this->header === '') { |
||
| 153 | $this->header = Yii::t('app', 'Filters'); |
||
| 154 | } |
||
| 155 | |||
| 156 | // Begin of a new filter sets implementation |
||
| 157 | if ($this->useNewFilter) { |
||
| 158 | $urlParams = [ |
||
| 159 | '@category', |
||
| 160 | 'last_category_id' => $categoryId, |
||
| 161 | 'properties' => Yii::$app->request->get('properties', []), |
||
| 162 | 'category_group_id' => Yii::$app->request->get('category_group_id', 0), |
||
| 163 | ]; |
||
| 164 | $priceMin = empty(Yii::$app->request->post('price_min')) ? Yii::$app->request->get('price_min') : Yii::$app->request->post('price_min'); |
||
| 165 | $priceMax = empty(Yii::$app->request->post('price_max')) ? Yii::$app->request->get('price_max') : Yii::$app->request->post('price_max'); |
||
| 166 | if (false === empty($priceMin)) { |
||
| 167 | $urlParams['price_min'] = $priceMin; |
||
| 168 | } |
||
| 169 | if (false === empty($priceMax)) { |
||
| 170 | $urlParams['price_max'] = $priceMax; |
||
| 171 | } |
||
| 172 | $urlParams = $this->mergeUrlProperties($urlParams, ['properties' => Yii::$app->request->post('properties', [])]); |
||
| 173 | $urlParams = $this->removeLostDependencies($filterSets, $urlParams); |
||
| 174 | $properties = $urlParams['properties']; |
||
| 175 | $newGet = Yii::$app->request->get(); |
||
| 176 | $newGet['properties'] = $properties; |
||
| 177 | Yii::$app->request->setQueryParams($newGet); |
||
| 178 | ksort($properties); |
||
| 179 | $cacheKey = 'FilterSets:' . $categoryId . ':' . json_encode($urlParams); |
||
| 180 | unset($properties); |
||
| 181 | if (false === $filtersArray = Yii::$app->cache->get($cacheKey)) { |
||
| 182 | $filtersArray = []; |
||
| 183 | foreach ($filterSets as $filterSet) { |
||
| 184 | /** Если eav и слайдер, то фильтр формируется по особым правилам */ |
||
| 185 | if ($filterSet->is_range_slider && $filterSet->property->is_eav) { |
||
| 186 | $filter = $this->getEavSliderFilter($filterSet->property, $urlParams); |
||
| 187 | if($filter) { |
||
| 188 | $filtersArray[] = $filter; |
||
| 189 | } |
||
| 190 | continue; |
||
| 191 | } elseif ($filterSet->property->has_static_values === 0) { |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | if (!empty($filterSet->property->depends_on_property_id) |
||
| 195 | && !empty($filterSet->property->depended_property_values) |
||
| 196 | ) { |
||
| 197 | if ( |
||
| 198 | $this->getSelectedPropertyIndex( |
||
| 199 | $urlParams['properties'], |
||
| 200 | $filterSet->property->depends_on_property_id, |
||
| 201 | $filterSet->property->depended_property_values |
||
| 202 | ) === false |
||
| 203 | ) { |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | $selections = PropertyStaticValues::getValuesForFilter( |
||
| 208 | $filterSet->property->id, |
||
| 209 | $urlParams['last_category_id'], |
||
| 210 | $urlParams['properties'], |
||
| 211 | $filterSet->multiple, |
||
| 212 | Yii::$app->getModule('shop')->filterOnlyByParentProduct |
||
| 213 | ); |
||
| 214 | if (count($selections) === 0) { |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | $item = [ |
||
| 218 | 'id' => $filterSet->property->id, |
||
| 219 | 'name' => $filterSet->property->name, |
||
| 220 | 'sort_order' => $filterSet->property->sort_order, |
||
| 221 | 'isRange' => $filterSet->is_range_slider, |
||
| 222 | 'selections' => [], |
||
| 223 | 'multiple' => $filterSet->multiple, |
||
| 224 | ]; |
||
| 225 | if ($filterSet->is_range_slider) { |
||
| 226 | $item['max'] = PHP_INT_MIN; |
||
| 227 | $item['min'] = PHP_INT_MAX; |
||
| 228 | $item['property'] = $filterSet->property; |
||
| 229 | } |
||
| 230 | foreach ($selections as $selection) { |
||
| 231 | if ($filterSet->is_range_slider) { |
||
| 232 | View Code Duplication | if ((int)$selection['value'] > $item['max']) { |
|
| 233 | $item['max'] = (int)$selection['value']; |
||
| 234 | } |
||
| 235 | View Code Duplication | if ((int)$selection['value'] < $item['min']) { |
|
| 236 | $item['min'] = (int)$selection['value']; |
||
| 237 | } |
||
| 238 | } elseif($item['multiple']) { |
||
| 239 | $selectedPropertyIndex = $this->getSelectedPropertyIndex( |
||
| 240 | $urlParams['properties'], |
||
| 241 | $filterSet->property_id, |
||
| 242 | $selection['id'] |
||
| 243 | ); |
||
| 244 | View Code Duplication | if ($selectedPropertyIndex !== false) { |
|
| 245 | if (count($urlParams['properties'][$filterSet->property_id]) > 1) { |
||
| 246 | $routeParams = $urlParams; |
||
| 247 | unset($routeParams['properties'][$filterSet->property_id][$selectedPropertyIndex]); |
||
| 248 | } else { |
||
| 249 | $routeParams = $urlParams; |
||
| 250 | unset($routeParams['properties'][$filterSet->property_id]); |
||
| 251 | } |
||
| 252 | if (isset($this->toUnset[$filterSet->property_id])) { |
||
| 253 | foreach ($this->toUnset[$filterSet->property_id] as $id) { |
||
| 254 | unset($routeParams['properties'][$id]); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } else { |
||
| 258 | $routeParams = $this->mergeUrlProperties( |
||
| 259 | $urlParams, |
||
| 260 | ['properties' => [$filterSet->property_id => [$selection['id']]]] |
||
| 261 | ); |
||
| 262 | } |
||
| 263 | $item['selections'][] = [ |
||
| 264 | 'id' => $selection['id'], |
||
| 265 | 'checked' => $selectedPropertyIndex !== false, |
||
| 266 | 'label' => $selection['name'], |
||
| 267 | 'url' => Url::toRoute($routeParams), |
||
| 268 | 'active' => $selection['active'], |
||
| 269 | ]; |
||
| 270 | } else { |
||
| 271 | $selectedPropertyIndex = $this->getSelectedPropertyIndex( |
||
| 272 | $urlParams['properties'], |
||
| 273 | $filterSet->property_id, |
||
| 274 | $selection['id'] |
||
| 275 | ); |
||
| 276 | View Code Duplication | if ($selectedPropertyIndex !== false) { |
|
| 277 | if (count($urlParams['properties'][$filterSet->property_id]) > 1) { |
||
| 278 | $routeParams = $urlParams; |
||
| 279 | unset($routeParams['properties'][$filterSet->property_id][$selectedPropertyIndex]); |
||
| 280 | } else { |
||
| 281 | $routeParams = $urlParams; |
||
| 282 | unset($routeParams['properties'][$filterSet->property_id]); |
||
| 283 | } |
||
| 284 | if (isset($this->toUnset[$filterSet->property_id])) { |
||
| 285 | foreach ($this->toUnset[$filterSet->property_id] as $id) { |
||
| 286 | unset($routeParams['properties'][$id]); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } else { |
||
| 290 | $routeParams = $this->mergeUrlProperties( |
||
| 291 | $urlParams, |
||
| 292 | ['properties' => [$filterSet->property_id => [$selection['id']]]] |
||
| 293 | ); |
||
| 294 | } |
||
| 295 | $item['selections'][] = [ |
||
| 296 | 'id' => $selection['id'], |
||
| 297 | 'checked' => $selectedPropertyIndex !== false, |
||
| 298 | 'label' => $selection['name'], |
||
| 299 | 'url' => $selection['active'] === true || $selectedPropertyIndex !== false |
||
| 300 | ? Url::toRoute($routeParams) |
||
| 301 | : null, |
||
| 302 | 'active' => $selection['active'], |
||
| 303 | ]; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | if ($filterSet->is_range_slider) { |
||
| 307 | if ($item['min'] === $item['max']) { |
||
| 308 | continue; |
||
| 309 | } |
||
| 310 | $i = 1; |
||
| 311 | $n = $item['max'] - $item['min']; |
||
| 312 | while ($n >= 10) { |
||
| 313 | $n = $n / 10; |
||
| 314 | $i++; |
||
| 315 | } |
||
| 316 | $item['step'] = $i > 3 ? (int)pow(10, $i - 3) : 1; |
||
| 317 | unset($i, $n); |
||
| 318 | } |
||
| 319 | $filtersArray[] = $item; |
||
| 320 | unset($item); |
||
| 321 | } |
||
| 322 | Yii::$app->cache->set( |
||
| 323 | $cacheKey, |
||
| 324 | $filtersArray, |
||
| 325 | 86400, |
||
| 326 | new TagDependency( |
||
| 327 | [ |
||
| 328 | 'tags' => [ |
||
| 329 | ActiveRecordHelper::getCommonTag(FilterSets::className()), |
||
| 330 | ActiveRecordHelper::getCommonTag(Product::className()), |
||
| 331 | ActiveRecordHelper::getCommonTag(Property::className()), |
||
| 332 | ], |
||
| 333 | ] |
||
| 334 | ) |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | return $this->render( |
||
| 338 | $this->viewFile, |
||
| 339 | [ |
||
| 340 | 'filtersArray' => $filtersArray, |
||
| 341 | 'id' => 'filter-set-' . $this->id, |
||
| 342 | 'urlParams' => $urlParams, |
||
| 343 | 'usePjax' => $this->usePjax, |
||
| 344 | ] |
||
| 345 | ); |
||
| 346 | } |
||
| 347 | // End of a new filter sets implementation |
||
| 348 | |||
| 349 | return $this->render( |
||
| 350 | $this->viewFile, |
||
| 351 | [ |
||
| 352 | 'filterSets' => $filterSets, |
||
| 353 | 'id' => 'filter-set-' . $this->id, |
||
| 354 | 'hideEmpty' => $this->hideEmpty, |
||
| 355 | ] |
||
| 356 | ); |
||
| 357 | } |
||
| 358 | } |
||
| 359 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.