| Conditions | 39 |
| Paths | 6912 |
| Total Lines | 138 |
| Code Lines | 97 |
| 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 |
||
| 184 | public function parseOptions($options = []) |
||
| 185 | { |
||
| 186 | $selectOptions = [ |
||
| 187 | 'where' => !empty($options['where']) ? $options['where'] : [], |
||
| 188 | 'distinct' => false, |
||
| 189 | 'join' => [], |
||
| 190 | 'order' => [], |
||
| 191 | 'start' => isset($options['start']) ? (int) $options['start'] : 0, |
||
| 192 | 'key' => isset($options['key']) ? $options['key'] : null, |
||
| 193 | 'limit' => !empty($options['count']) ? (int) $options['count'] : 0, |
||
| 194 | ]; |
||
| 195 | if (!empty($options['sort']) && is_array($options['sort'])) { |
||
| 196 | foreach ($options['sort'] as $col => $direction) { |
||
| 197 | switch ($col) { |
||
| 198 | case 'price': |
||
| 199 | $selectOptions['order'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 200 | break; |
||
| 201 | View Code Duplication | case 'name': |
|
| 202 | $selectOptions['order'][] = ['name', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 203 | break; |
||
| 204 | View Code Duplication | case 'sales': |
|
| 205 | $selectOptions['order'][] = ['sales', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 206 | break; |
||
| 207 | View Code Duplication | case 'weight': |
|
| 208 | $selectOptions['order'][] = ['weight', strtolower($direction) == 'desc' ? 'desc' : 'asc']; |
||
| 209 | break; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | $selectOptions['where'][] = ['deleted', 0]; |
||
| 214 | if (empty($this->config['view_empty_image'])) { |
||
| 215 | $selectOptions['where'][] = ['image_file_id', 0, '!=']; |
||
| 216 | } |
||
| 217 | if (!empty($this->config['view_filter'])) { |
||
| 218 | if (!empty($this->config['view_filter']['options'])) { |
||
| 219 | foreach ($this->config['view_filter']['options'] as $optionId => $optionValue) { |
||
| 220 | $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' . |
||
| 221 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 222 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"', |
||
| 223 | 'inner', 'option' . $optionId]; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | //filters |
||
| 228 | if (!empty($options['filters'])) { |
||
| 229 | foreach ($options['filters'] as $col => $filter) { |
||
| 230 | switch ($col) { |
||
| 231 | case 'price': |
||
| 232 | View Code Duplication | if (!empty($filter['min'])) { |
|
| 233 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['min'], '>=']; |
||
| 234 | } |
||
| 235 | View Code Duplication | if (!empty($filter['max'])) { |
|
| 236 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['max'], '<=']; |
||
| 237 | } |
||
| 238 | break; |
||
| 239 | case 'options': |
||
| 240 | foreach ($filter as $optionId => $optionValue) { |
||
| 241 | $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' . |
||
| 242 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 243 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"', |
||
| 244 | 'inner', 'option' . $optionId]; |
||
| 245 | } |
||
| 246 | break; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | //parents |
||
| 251 | if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) { |
||
| 252 | $first = true; |
||
| 253 | $where = []; |
||
| 254 | foreach (explode(',', $options['parent']) as $categoryId) { |
||
| 255 | if (!$categoryId) { |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | $category = \Ecommerce\Category::get($categoryId); |
||
| 259 | $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR']; |
||
| 260 | $first = false; |
||
| 261 | } |
||
| 262 | $selectOptions['where'][] = $where; |
||
| 263 | } elseif (!empty($options['parent'])) { |
||
| 264 | $category = \Ecommerce\Category::get($options['parent']); |
||
| 265 | $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE']; |
||
| 266 | } |
||
| 267 | |||
| 268 | //search |
||
| 269 | if (!empty($options['search'])) { |
||
| 270 | $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']); |
||
| 271 | $searchArr = []; |
||
| 272 | foreach (explode(' ', $searchStr) as $part) { |
||
| 273 | $part = trim($part); |
||
| 274 | if ($part && strlen($part) > 2) { |
||
| 275 | $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE']; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | if (!empty($searchArr)) { |
||
| 279 | $selectOptions['where'][] = $searchArr; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | if (empty($this->config['view_empty_warehouse'])) { |
||
| 283 | $selectOptions['where'][] = [ |
||
| 284 | '( |
||
| 285 | (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0) |
||
| 286 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw |
||
| 287 | WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ' |
||
| 288 | ) |
||
| 289 | - |
||
| 290 | (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0) |
||
| 291 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb |
||
| 292 | 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 ( |
||
| 293 | (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
||
| 294 | (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
||
| 295 | ) |
||
| 296 | WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ') |
||
| 297 | )', |
||
| 298 | 0, |
||
| 299 | '>' |
||
| 300 | ]; |
||
| 301 | } |
||
| 302 | |||
| 303 | |||
| 304 | $selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = ' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'inner']; |
||
| 305 | $selectOptions['join'][] = [Ecommerce\Item\Offer\Price::table(), |
||
| 306 | Ecommerce\Item\Offer::index() . ' = ' . Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer::index() . ' and ' . Ecommerce\Item\Offer\Price::colPrefix() . 'price>0', 'inner']; |
||
| 307 | $selectOptions['join'][] = [ |
||
| 308 | Ecommerce\Item\Offer\Price\Type::table(), Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index() . ' = ' . Ecommerce\Item\Offer\Price\Type::index() . |
||
| 309 | ' and (' . Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles="" || ' . Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles LIKE "%|' . \Users\User::$cur->role_id . '|%")' |
||
| 310 | ]; |
||
| 311 | $selectOptions['where'][] = [ |
||
| 312 | [Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index(), 0], |
||
| 313 | [Ecommerce\Item\Offer\Price\Type::index(), 0, '>', 'OR'] |
||
| 314 | ]; |
||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | $selectOptions['group'] = Ecommerce\Item::index(); |
||
| 319 | |||
| 320 | return $selectOptions; |
||
| 321 | } |
||
| 322 | |||
| 472 |
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: