| Total Complexity | 69 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OptionsParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OptionsParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class OptionsParser extends \InjiObject { |
||
|
|
|||
| 15 | |||
| 16 | public static function parse($options = [], $forCount = false) { |
||
| 17 | $selectOptions = self::getDefault($options); |
||
| 18 | |||
| 19 | //parse order options |
||
| 20 | self::order($options, $selectOptions); |
||
| 21 | |||
| 22 | //joined offers |
||
| 23 | $selectOptions['join'][] = [Item\Offer::table(), Item::index() . ' = ' . Item\Offer::colPrefix() . Item::index(), 'inner']; |
||
| 24 | |||
| 25 | //joined prices, check price configs |
||
| 26 | $comparision = Item\Offer::index() . ' = ' . Item\Offer\Price::colPrefix() . Item\Offer::index(); |
||
| 27 | if (empty(\App::$cur->Ecommerce->config['show_zero_price'])) { |
||
| 28 | $comparision .= ' and ' . Item\Offer\Price::colPrefix() . 'price>0'; |
||
| 29 | } |
||
| 30 | if (!empty(\App::$cur->Ecommerce->config['available_price_types'])) { |
||
| 31 | $comparision .= ' and ' . Item\Offer\Price::colPrefix() . 'item_offer_price_type_id in (' . implode(',', \App::$cur->Ecommerce->config['available_price_types']) . ')'; |
||
| 32 | } |
||
| 33 | $selectOptions['join'][] = [Item\Offer\Price::table(), $comparision, |
||
| 34 | empty(\App::$cur->Ecommerce->config['show_without_price']) ? 'inner' : 'left']; |
||
| 35 | |||
| 36 | //join item types |
||
| 37 | $selectOptions['join'][] = [ |
||
| 38 | Item\Offer\Price\Type::table(), Item\Offer\Price::colPrefix() . Item\Offer\Price\Type::index() . ' = ' . Item\Offer\Price\Type::index() |
||
| 39 | ]; |
||
| 40 | |||
| 41 | //add filter for item type user roles by current user role |
||
| 42 | $selectOptions['where'][] = [ |
||
| 43 | [Item\Offer\Price\Type::index(), null, 'is'], |
||
| 44 | [ |
||
| 45 | [Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'], |
||
| 46 | [Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'], |
||
| 47 | ], |
||
| 48 | ]; |
||
| 49 | |||
| 50 | //add custom preset filters from config on where |
||
| 51 | self::presetFilters($selectOptions); |
||
| 52 | |||
| 53 | //parse filters |
||
| 54 | self::filters($options, $selectOptions); |
||
| 55 | |||
| 56 | //categories paths |
||
| 57 | self::categories($options, $selectOptions); |
||
| 58 | |||
| 59 | //search |
||
| 60 | self::search($options, $selectOptions); |
||
| 61 | |||
| 62 | self::warehouse($selectOptions); |
||
| 63 | if ($forCount) { |
||
| 64 | $selectOptions['distinct'] = \Ecommerce\Item::index(); |
||
| 65 | } else { |
||
| 66 | $selectOptions['group'] = Item::index(); |
||
| 67 | } |
||
| 68 | if (isset($options['array'])) { |
||
| 69 | $selectOptions['array'] = $options['array']; |
||
| 70 | } |
||
| 71 | |||
| 72 | return $selectOptions; |
||
| 73 | } |
||
| 74 | |||
| 75 | public static function getDefault(&$options) { |
||
| 76 | $selectOptions = [ |
||
| 77 | 'where' => !empty($options['where']) ? $options['where'] : [], |
||
| 78 | 'distinct' => false, |
||
| 79 | 'join' => [], |
||
| 80 | 'order' => [], |
||
| 81 | 'start' => isset($options['start']) ? (int) $options['start'] : 0, |
||
| 82 | 'key' => isset($options['key']) ? $options['key'] : null, |
||
| 83 | 'limit' => !empty($options['count']) ? (int) $options['count'] : 0, |
||
| 84 | ]; |
||
| 85 | |||
| 86 | //only not deleted items |
||
| 87 | $selectOptions['where'][] = ['deleted', 0]; |
||
| 88 | |||
| 89 | //check config of view items without images |
||
| 90 | if (empty(\App::$cur->Ecommerce->config['view_empty_image'])) { |
||
| 91 | $selectOptions['where'][] = ['image_file_id', 0, '!=']; |
||
| 92 | } |
||
| 93 | |||
| 94 | return $selectOptions; |
||
| 95 | } |
||
| 96 | |||
| 97 | public static function order(&$options, &$selectOptions) { |
||
| 98 | if (empty($options['sort']) || !is_array($options['sort'])) { |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | foreach ($options['sort'] as $col => $direction) { |
||
| 102 | $direction = strtolower($direction) == 'desc' ? 'desc' : 'asc'; |
||
| 103 | switch ($col) { |
||
| 104 | case 'price': |
||
| 105 | $selectOptions['order'][] = [Item\Offer\Price::colPrefix() . 'price', $direction]; |
||
| 106 | break; |
||
| 107 | case 'new': |
||
| 108 | $selectOptions['order'][] = ['date_create', $direction]; |
||
| 109 | break; |
||
| 110 | case 'isset': |
||
| 111 | $warehouseIds = self::getWarehouses('order'); |
||
| 112 | $selectOptions['order'][] = [ |
||
| 113 | '( |
||
| 114 | (SELECT COALESCE(sum(`' . Item\Offer\Warehouse::colPrefix() . 'count`),0) |
||
| 115 | FROM ' . \App::$cur->db->table_prefix . Item\Offer\Warehouse::table() . ' iciw |
||
| 116 | WHERE iciw.' . Item\Offer\Warehouse::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ' |
||
| 117 | ' . ($warehouseIds ? ' AND iciw.' . Item\Offer\Warehouse::colPrefix() . Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . ' |
||
| 118 | ) |
||
| 119 | - |
||
| 120 | (SELECT COALESCE(sum(' . Warehouse\Block::colPrefix() . 'count) ,0) |
||
| 121 | FROM ' . \App::$cur->db->table_prefix . Warehouse\Block::table() . ' iewb |
||
| 122 | inner JOIN ' . \App::$cur->db->table_prefix . Cart::table() . ' icc ON icc.' . Cart::index() . ' = iewb.' . Warehouse\Block::colPrefix() . Cart::index() . ' AND ( |
||
| 123 | (`' . Cart::colPrefix() . 'warehouse_block` = 1 and `' . Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
||
| 124 | (`' . Cart::colPrefix() . Cart\Status::index() . '` in(0,1) and `' . Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
||
| 125 | ) |
||
| 126 | WHERE iewb.' . Warehouse\Block::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ') |
||
| 127 | )', $direction |
||
| 128 | ]; |
||
| 129 | break; |
||
| 130 | case 'name': |
||
| 131 | case 'sales': |
||
| 132 | case 'weight': |
||
| 133 | $selectOptions['order'][] = [$col, $direction]; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | $selectOptions['order'][] = ['offers:prices:type:weight', 'ASC']; |
||
| 137 | } |
||
| 138 | |||
| 139 | public static function presetFilters(&$selectOptions) { |
||
| 140 | if (empty(\App::$cur->Ecommerce->config['view_filter'])) { |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | if (!empty(\App::$cur->Ecommerce->config['view_filter']['options'])) { |
||
| 144 | foreach (\App::$cur->Ecommerce->config['view_filter']['options'] as $optionId => $optionValue) { |
||
| 145 | $selectOptions['join'][] = [Item\Param::table(), Item::index() . ' = ' . 'option' . $optionId . '.' . Item\Param::colPrefix() . Item::index() . ' AND ' . |
||
| 146 | 'option' . $optionId . '.' . Item\Param::colPrefix() . Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 147 | 'option' . $optionId . '.' . Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"', |
||
| 148 | 'inner', 'option' . $optionId]; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function filters(&$options, &$selectOptions) { |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | public static function categories(&$options, &$selectOptions) { |
||
| 212 | if (empty($options['parent'])) { |
||
| 213 | return; |
||
| 214 | } |
||
| 215 | if (is_array($options['parent']) || strpos($options['parent'], ',') !== false) { |
||
| 216 | $list = is_array($options['parent']) ? $options['parent'] : explode(',', $options['parent']); |
||
| 217 | $first = true; |
||
| 218 | $where = []; |
||
| 219 | foreach ($list as $categoryId) { |
||
| 220 | if (!$categoryId) { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | $category = Category::get($categoryId); |
||
| 224 | $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR']; |
||
| 225 | $first = false; |
||
| 226 | } |
||
| 227 | $selectOptions['where'][] = $where; |
||
| 228 | } else { |
||
| 229 | $category = Category::get($options['parent']); |
||
| 230 | if ($category) { |
||
| 231 | $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE']; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | public static function search(&$options, &$selectOptions) { |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | public static function warehouse(&$selectOptions) { |
||
| 277 | ]; |
||
| 278 | } |
||
| 279 | |||
| 280 | public static function getWarehouses($type = 'all') { |
||
| 296 | } |
||
| 297 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths