| Conditions | 52 |
| Paths | > 20000 |
| Total Lines | 187 |
| Code Lines | 129 |
| Lines | 60 |
| Ratio | 32.09 % |
| 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 |
||
| 178 | public function parseOptions($options = []) { |
||
| 179 | $selectOptions = [ |
||
| 180 | 'where' => !empty($options['where']) ? $options['where'] : [], |
||
| 181 | 'distinct' => false, |
||
| 182 | 'join' => [], |
||
| 183 | 'order' => [], |
||
| 184 | 'start' => isset($options['start']) ? (int) $options['start'] : 0, |
||
| 185 | 'key' => isset($options['key']) ? $options['key'] : null, |
||
| 186 | 'limit' => !empty($options['count']) ? (int) $options['count'] : 0, |
||
| 187 | ]; |
||
| 188 | if (!empty($options['sort']) && is_array($options['sort'])) { |
||
| 189 | foreach ($options['sort'] as $col => $direction) { |
||
| 190 | switch ($col) { |
||
| 191 | case 'price': |
||
| 192 | $selectOptions['order'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 193 | break; |
||
| 194 | View Code Duplication | case 'name': |
|
| 195 | $selectOptions['order'][] = ['name', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 196 | break; |
||
| 197 | View Code Duplication | case 'sales': |
|
| 198 | $selectOptions['order'][] = ['sales', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 199 | break; |
||
| 200 | View Code Duplication | case 'weight': |
|
| 201 | $selectOptions['order'][] = ['weight', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | $selectOptions['where'][] = ['deleted', 0]; |
||
| 207 | if (empty($this->config['view_empty_image'])) { |
||
| 208 | $selectOptions['where'][] = ['image_file_id', 0, '!=']; |
||
| 209 | } |
||
| 210 | |||
| 211 | $selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = ' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'inner']; |
||
| 212 | |||
| 213 | $selectOptions['join'][] = [Ecommerce\Item\Offer\Price::table(), |
||
| 214 | Ecommerce\Item\Offer::index() . ' = ' . Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer::index() . |
||
| 215 | (empty($this->config['show_zero_price']) ? ' and ' . Ecommerce\Item\Offer\Price::colPrefix() . 'price>0' : ''), |
||
| 216 | empty($this->config['show_without_price']) ? 'inner' : 'left']; |
||
| 217 | |||
| 218 | $selectOptions['join'][] = [ |
||
| 219 | Ecommerce\Item\Offer\Price\Type::table(), Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index() . ' = ' . Ecommerce\Item\Offer\Price\Type::index() |
||
| 220 | ]; |
||
| 221 | |||
| 222 | $selectOptions['where'][] = [ |
||
| 223 | [Ecommerce\Item\Offer\Price\Type::index(), NULL, 'is'], |
||
| 224 | [ |
||
| 225 | [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'], |
||
| 226 | [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'], |
||
| 227 | ], |
||
| 228 | ]; |
||
| 229 | |||
| 230 | |||
| 231 | if (!empty($this->config['view_filter'])) { |
||
| 232 | if (!empty($this->config['view_filter']['options'])) { |
||
| 233 | foreach ($this->config['view_filter']['options'] as $optionId => $optionValue) { |
||
| 234 | $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' . |
||
| 235 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 236 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"', |
||
| 237 | 'inner', 'option' . $optionId]; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | //filters |
||
| 242 | if (!empty($options['filters'])) { |
||
| 243 | foreach ($options['filters'] as $col => $filter) { |
||
| 244 | switch ($col) { |
||
| 245 | case 'price': |
||
| 246 | View Code Duplication | if (!empty($filter['min'])) { |
|
| 247 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['min'], '>=']; |
||
| 248 | } |
||
| 249 | View Code Duplication | if (!empty($filter['max'])) { |
|
| 250 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['max'], '<=']; |
||
| 251 | } |
||
| 252 | break; |
||
| 253 | View Code Duplication | case 'options': |
|
| 254 | foreach ($filter as $optionId => $optionValue) { |
||
| 255 | $optionId = (int) $optionId; |
||
| 256 | if (is_array($optionValue)) { |
||
| 257 | $optionValueArr = []; |
||
| 258 | foreach ($optionValue as $val) { |
||
| 259 | $optionValueArr[] = \App::$cur->db->connection->pdo->quote($val); |
||
| 260 | } |
||
| 261 | $qstr = 'IN (' . implode(',', $optionValueArr) . ')'; |
||
| 262 | } else { |
||
| 263 | $qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue); |
||
| 264 | } |
||
| 265 | $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' . |
||
| 266 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 267 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value ' . $qstr . '', |
||
| 268 | 'inner', 'option' . $optionId]; |
||
| 269 | } |
||
| 270 | break; |
||
| 271 | View Code Duplication | case 'offerOptions': |
|
| 272 | //$selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = offer.' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'left', 'offer']; |
||
| 273 | foreach ($filter as $optionId => $optionValue) { |
||
| 274 | $optionId = (int) $optionId; |
||
| 275 | if (is_array($optionValue)) { |
||
| 276 | $optionValueArr = []; |
||
| 277 | foreach ($optionValue as $val) { |
||
| 278 | $optionValueArr[] = \App::$cur->db->connection->pdo->quote($val); |
||
| 279 | } |
||
| 280 | $qstr = 'IN (' . implode(',', $optionValueArr) . ')'; |
||
| 281 | } else { |
||
| 282 | $qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue); |
||
| 283 | } |
||
| 284 | $selectOptions['join'][] = [Ecommerce\Item\Offer\Param::table(), Ecommerce\Item\Offer::index() . ' = ' . 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer::index() . ' AND ' . |
||
| 285 | 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 286 | 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . 'value ' . $qstr, |
||
| 287 | 'inner', 'offerOption' . $optionId]; |
||
| 288 | } |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | //parents |
||
| 294 | if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) { |
||
| 295 | $first = true; |
||
| 296 | $where = []; |
||
| 297 | foreach (explode(',', $options['parent']) as $categoryId) { |
||
| 298 | if (!$categoryId) { |
||
| 299 | continue; |
||
| 300 | } |
||
| 301 | $category = \Ecommerce\Category::get($categoryId); |
||
| 302 | $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR']; |
||
| 303 | $first = false; |
||
| 304 | } |
||
| 305 | $selectOptions['where'][] = $where; |
||
| 306 | } elseif (!empty($options['parent'])) { |
||
| 307 | $category = \Ecommerce\Category::get($options['parent']); |
||
| 308 | $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE']; |
||
| 309 | } |
||
| 310 | |||
| 311 | //search |
||
| 312 | if (!empty($options['search'])) { |
||
| 313 | $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']); |
||
| 314 | $searchArr = []; |
||
| 315 | foreach (explode(' ', $searchStr) as $part) { |
||
| 316 | $part = trim($part); |
||
| 317 | if ($part && strlen($part) > 2) { |
||
| 318 | $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE']; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | if (!empty($searchArr)) { |
||
| 322 | $selectOptions['where'][] = $searchArr; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | if (empty($this->config['view_empty_warehouse'])) { |
||
| 326 | $warehouseIds = []; |
||
| 327 | View Code Duplication | if (class_exists('Geography\City\Data')) { |
|
| 328 | $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]); |
||
| 329 | if ($warehouses && $warehouses->data) { |
||
| 330 | foreach (explode(',', $warehouses->data) as $id) { |
||
| 331 | $warehouseIds[$id] = $id; |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } |
||
| 335 | $selectOptions['where'][] = [ |
||
| 336 | '( |
||
| 337 | (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0) |
||
| 338 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw |
||
| 339 | WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ' |
||
| 340 | ' . ($warehouseIds ? ' AND iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . ' |
||
| 341 | ) |
||
| 342 | - |
||
| 343 | (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0) |
||
| 344 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb |
||
| 345 | inner JOIN ' . \App::$cur->db->table_prefix . \Ecommerce\Cart::table() . ' icc ON icc.' . \Ecommerce\Cart::index() . ' = iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Cart::index() . ' AND ( |
||
| 346 | (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
||
| 347 | (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
||
| 348 | ) |
||
| 349 | WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ') |
||
| 350 | )', |
||
| 351 | 0, |
||
| 352 | '>' |
||
| 353 | ]; |
||
| 354 | } |
||
| 355 | |||
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | |||
| 361 | $selectOptions['group'] = Ecommerce\Item::index(); |
||
| 362 | |||
| 363 | return $selectOptions; |
||
| 364 | } |
||
| 365 | |||
| 545 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: