| Conditions | 48 | 
| Paths | > 20000 | 
| Total Lines | 170 | 
| Code Lines | 115 | 
| Lines | 42 | 
| Ratio | 24.71 % | 
| Changes | 3 | ||
| Bugs | 0 | Features | 2 | 
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 | |||
| 218 | $selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = ' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'inner'];  | 
            ||
| 219 | |||
| 220 | $selectOptions['join'][] = [Ecommerce\Item\Offer\Price::table(),  | 
            ||
| 221 | Ecommerce\Item\Offer::index() . ' = ' . Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer::index() .  | 
            ||
| 222 | (empty($this->config['show_zero_price']) ? ' and ' . Ecommerce\Item\Offer\Price::colPrefix() . 'price>0' : ''),  | 
            ||
| 223 | empty($this->config['show_without_price']) ? 'inner' : 'left'];  | 
            ||
| 224 | |||
| 225 | $selectOptions['join'][] = [  | 
            ||
| 226 | Ecommerce\Item\Offer\Price\Type::table(), Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index() . ' = ' . Ecommerce\Item\Offer\Price\Type::index()  | 
            ||
| 227 | ];  | 
            ||
| 228 | |||
| 229 | $selectOptions['where'][] = [  | 
            ||
| 230 | [Ecommerce\Item\Offer\Price\Type::index(), NULL, 'is'],  | 
            ||
| 231 | [  | 
            ||
| 232 | [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'],  | 
            ||
| 233 | [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'],  | 
            ||
| 234 | ],  | 
            ||
| 235 | ];  | 
            ||
| 236 | |||
| 237 | |||
| 238 |         if (!empty($this->config['view_filter'])) { | 
            ||
| 239 |             if (!empty($this->config['view_filter']['options'])) { | 
            ||
| 240 |                 foreach ($this->config['view_filter']['options'] 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 | }  | 
            ||
| 247 | }  | 
            ||
| 248 | //filters  | 
            ||
| 249 |         if (!empty($options['filters'])) { | 
            ||
| 250 |             foreach ($options['filters'] as $col => $filter) { | 
            ||
| 251 |                 switch ($col) { | 
            ||
| 252 | case 'price':  | 
            ||
| 253 | View Code Duplication |                         if (!empty($filter['min'])) { | 
            |
| 254 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['min'], '>='];  | 
            ||
| 255 | }  | 
            ||
| 256 | View Code Duplication |                         if (!empty($filter['max'])) { | 
            |
| 257 | $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['max'], '<='];  | 
            ||
| 258 | }  | 
            ||
| 259 | break;  | 
            ||
| 260 | View Code Duplication | case 'options':  | 
            |
| 261 |                         foreach ($filter as $optionId => $optionValue) { | 
            ||
| 262 | $optionId = (int) $optionId;  | 
            ||
| 263 | $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' .  | 
            ||
| 264 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .  | 
            ||
| 265 | 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = ' . \App::$cur->db->connection->pdo->quote($optionValue) . '',  | 
            ||
| 266 | 'inner', 'option' . $optionId];  | 
            ||
| 267 | }  | 
            ||
| 268 | break;  | 
            ||
| 269 | View Code Duplication | case 'offerOptions':  | 
            |
| 270 | //$selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = offer.' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'left', 'offer'];  | 
            ||
| 271 |                         foreach ($filter as $optionId => $optionValue) { | 
            ||
| 272 | $optionId = (int) $optionId;  | 
            ||
| 273 | $selectOptions['join'][] = [Ecommerce\Item\Offer\Param::table(), Ecommerce\Item\Offer::index() . ' = ' . 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer::index() . ' AND ' .  | 
            ||
| 274 | 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer\Option::index() . ' = "' . (int) $optionId . '" AND ' .  | 
            ||
| 275 | 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . 'value = ' . \App::$cur->db->connection->pdo->quote($optionValue) . '',  | 
            ||
| 276 | 'inner', 'offerOption' . $optionId];  | 
            ||
| 277 | }  | 
            ||
| 278 | break;  | 
            ||
| 279 | }  | 
            ||
| 280 | }  | 
            ||
| 281 | }  | 
            ||
| 282 | //parents  | 
            ||
| 283 |         if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) { | 
            ||
| 284 | $first = true;  | 
            ||
| 285 | $where = [];  | 
            ||
| 286 |             foreach (explode(',', $options['parent']) as $categoryId) { | 
            ||
| 287 |                 if (!$categoryId) { | 
            ||
| 288 | continue;  | 
            ||
| 289 | }  | 
            ||
| 290 | $category = \Ecommerce\Category::get($categoryId);  | 
            ||
| 291 | $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR'];  | 
            ||
| 292 | $first = false;  | 
            ||
| 293 | }  | 
            ||
| 294 | $selectOptions['where'][] = $where;  | 
            ||
| 295 |         } elseif (!empty($options['parent'])) { | 
            ||
| 296 | $category = \Ecommerce\Category::get($options['parent']);  | 
            ||
| 297 | $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE'];  | 
            ||
| 298 | }  | 
            ||
| 299 | |||
| 300 | //search  | 
            ||
| 301 |         if (!empty($options['search'])) { | 
            ||
| 302 |             $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']); | 
            ||
| 303 | $searchArr = [];  | 
            ||
| 304 |             foreach (explode(' ', $searchStr) as $part) { | 
            ||
| 305 | $part = trim($part);  | 
            ||
| 306 |                 if ($part && strlen($part) > 2) { | 
            ||
| 307 | $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE'];  | 
            ||
| 308 | }  | 
            ||
| 309 | }  | 
            ||
| 310 |             if (!empty($searchArr)) { | 
            ||
| 311 | $selectOptions['where'][] = $searchArr;  | 
            ||
| 312 | }  | 
            ||
| 313 | }  | 
            ||
| 314 |         if (empty($this->config['view_empty_warehouse'])) { | 
            ||
| 315 | $warehouseIds = [];  | 
            ||
| 316 | View Code Duplication |             if (class_exists('Geography\City\Data')) { | 
            |
| 317 | $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]);  | 
            ||
| 318 |                 if ($warehouses && $warehouses->data) { | 
            ||
| 319 |                     foreach (explode(',', $warehouses->data) as $id) { | 
            ||
| 320 | $warehouseIds[$id] = $id;  | 
            ||
| 321 | }  | 
            ||
| 322 | }  | 
            ||
| 323 | }  | 
            ||
| 324 | $selectOptions['where'][] = [  | 
            ||
| 325 | '(  | 
            ||
| 326 | (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0)  | 
            ||
| 327 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw  | 
            ||
| 328 | WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . '  | 
            ||
| 329 |                 ' . ($warehouseIds ? ' AND iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . ' | 
            ||
| 330 | )  | 
            ||
| 331 | -  | 
            ||
| 332 |           (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0) | 
            ||
| 333 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb  | 
            ||
| 334 | 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 (  | 
            ||
| 335 | (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) ||  | 
            ||
| 336 | (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE))  | 
            ||
| 337 | )  | 
            ||
| 338 | WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ')  | 
            ||
| 339 | )',  | 
            ||
| 340 | 0,  | 
            ||
| 341 | '>'  | 
            ||
| 342 | ];  | 
            ||
| 343 | }  | 
            ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | |||
| 348 | |||
| 349 | |||
| 350 | $selectOptions['group'] = Ecommerce\Item::index();  | 
            ||
| 351 | |||
| 352 | return $selectOptions;  | 
            ||
| 353 | }  | 
            ||
| 354 | |||
| 507 | 
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: