| Conditions | 39 |
| Paths | 6912 |
| Total Lines | 138 |
| Code Lines | 96 |
| Lines | 15 |
| Ratio | 10.87 % |
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 |
||
| 127 | public function parseOptions($options = []) |
||
| 128 | { |
||
| 129 | $selectOptions = [ |
||
| 130 | 'where' => !empty($options['where']) ? $options['where'] : [], |
||
| 131 | 'distinct' => false, |
||
| 132 | 'join' => [], |
||
| 133 | 'order' => [], |
||
| 134 | 'start' => isset($options['start']) ? (int) $options['start'] : 0, |
||
| 135 | 'key' => isset($options['key']) ? $options['key'] : null, |
||
| 136 | 'limit' => !empty($options['count']) ? (int) $options['count'] : 0, |
||
| 137 | ]; |
||
| 138 | if (!empty($options['sort']) && is_array($options['sort'])) { |
||
| 139 | foreach ($options['sort'] as $col => $direction) { |
||
| 140 | switch ($col) { |
||
| 141 | case 'price': |
||
| 142 | $selectOptions['order'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 143 | break; |
||
| 144 | View Code Duplication | case 'name': |
|
| 145 | $selectOptions['order'][] = ['name', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 146 | break; |
||
| 147 | View Code Duplication | case 'sales': |
|
| 148 | $selectOptions['order'][] = ['sales', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 149 | break; |
||
| 150 | View Code Duplication | case 'weight': |
|
| 151 | $selectOptions['order'][] = ['weight', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | if (empty($this->config['view_empty_image'])) { |
||
| 158 | $selectOptions['where'][] = ['image_file_id', 0, '!=']; |
||
| 159 | } |
||
| 160 | if (!empty($this->config['view_filter'])) { |
||
| 161 | if (!empty($this->config['view_filter']['options'])) { |
||
| 162 | foreach ($this->config['view_filter']['options'] as $optionId => $optionValue) { |
||
| 163 | $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' . |
||
| 164 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 165 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"', |
||
| 166 | 'inner', 'option' . $optionId]; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | //filters |
||
| 171 | if (!empty($options['filters'])) { |
||
| 172 | foreach ($options['filters'] as $col => $filter) { |
||
| 173 | switch ($col) { |
||
| 174 | case 'price': |
||
| 175 | View Code Duplication | if (!empty($filter['min'])) { |
|
| 176 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['min'], '>=']; |
||
| 177 | } |
||
| 178 | View Code Duplication | if (!empty($filter['max'])) { |
|
| 179 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['max'], '<=']; |
||
| 180 | } |
||
| 181 | break; |
||
| 182 | case 'options': |
||
| 183 | foreach ($filter as $optionId => $optionValue) { |
||
| 184 | $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' . |
||
| 185 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 186 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"', |
||
| 187 | 'inner', 'option' . $optionId]; |
||
| 188 | } |
||
| 189 | break; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | //parents |
||
| 194 | if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) { |
||
| 195 | $first = true; |
||
| 196 | $where = []; |
||
| 197 | foreach (explode(',', $options['parent']) as $categoryId) { |
||
| 198 | if (!$categoryId) { |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | $category = \Ecommerce\Category::get($categoryId); |
||
| 202 | $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR']; |
||
| 203 | $first = false; |
||
| 204 | } |
||
| 205 | $selectOptions['where'][] = $where; |
||
| 206 | } elseif (!empty($options['parent'])) { |
||
| 207 | $category = \Ecommerce\Category::get($options['parent']); |
||
| 208 | $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE']; |
||
| 209 | } |
||
| 210 | |||
| 211 | //search |
||
| 212 | if (!empty($options['search'])) { |
||
| 213 | $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']); |
||
| 214 | $searchArr = []; |
||
| 215 | foreach (explode(' ', $searchStr) as $part) { |
||
| 216 | $part = trim($part); |
||
| 217 | if ($part && strlen($part) > 2) { |
||
| 218 | $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE']; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | if (!empty($searchArr)) { |
||
| 222 | $selectOptions['where'][] = $searchArr; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | if (empty($this->config['view_empty_warehouse'])) { |
||
| 226 | $selectOptions['where'][] = [ |
||
| 227 | '( |
||
| 228 | (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0) |
||
| 229 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw |
||
| 230 | WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ' |
||
| 231 | ) |
||
| 232 | - |
||
| 233 | (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0) |
||
| 234 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb |
||
| 235 | 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 ( |
||
| 236 | (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
||
| 237 | (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
||
| 238 | ) |
||
| 239 | WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ') |
||
| 240 | )', |
||
| 241 | 0, |
||
| 242 | '>' |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | $selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = ' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'inner']; |
||
| 248 | $selectOptions['join'][] = [Ecommerce\Item\Offer\Price::table(), |
||
| 249 | Ecommerce\Item\Offer::index() . ' = ' . Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer::index() . ' and ' . Ecommerce\Item\Offer\Price::colPrefix() . 'price>0', 'inner']; |
||
| 250 | $selectOptions['join'][] = [ |
||
| 251 | Ecommerce\Item\Offer\Price\Type::table(), Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index() . ' = ' . Ecommerce\Item\Offer\Price\Type::index() . |
||
| 252 | ' and (' . Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles="" || ' . Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles LIKE "%|' . \Users\User::$cur->role_id . '|%")' |
||
| 253 | ]; |
||
| 254 | $selectOptions['where'][] = [ |
||
| 255 | [Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index(), 0], |
||
| 256 | [Ecommerce\Item\Offer\Price\Type::index(), 0, '>', 'OR'] |
||
| 257 | ]; |
||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | $selectOptions['group'] = Ecommerce\Item::index(); |
||
| 262 | |||
| 263 | return $selectOptions; |
||
| 264 | } |
||
| 265 | |||
| 389 |
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: