Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Ecommerce 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Ecommerce, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Ecommerce extends Module |
||
| 12 | { |
||
| 13 | public function init() |
||
| 14 | { |
||
| 15 | App::$primary->view->customAsset('js', '/moduleAsset/Ecommerce/js/cart.js'); |
||
| 16 | } |
||
| 17 | |||
| 18 | public function getPayTypeHandlers($forSelect = false) |
||
| 19 | { |
||
| 20 | if (!$forSelect) { |
||
| 21 | return $this->getSnippets('payTypeHandler'); |
||
| 22 | } |
||
| 23 | $handlers = ['' => 'Не выбрано']; |
||
| 24 | foreach ($this->getSnippets('payTypeHandler') as $key => $handler) { |
||
| 25 | if (empty($handler)) { |
||
| 26 | continue; |
||
| 27 | } |
||
| 28 | $handlers[$key] = $handler['name']; |
||
| 29 | } |
||
| 30 | return $handlers; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function cartPayRecive($data) |
||
| 34 | { |
||
| 35 | $cart = Ecommerce\Cart::get($data['pay']->data); |
||
| 36 | if ($cart) { |
||
| 37 | $payed = true; |
||
| 38 | foreach ($cart->pays as $pay) { |
||
| 39 | if ($pay->pay_status_id != 2) { |
||
| 40 | $payed = false; |
||
| 41 | break; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | $cart->payed = $payed; |
||
| 45 | $cart->save(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | View Code Duplication | public function parseFields($data, $cart) |
|
| 50 | { |
||
| 51 | $fields = \Ecommerce\UserAdds\Field::getList(); |
||
| 52 | $name = ''; |
||
| 53 | foreach ($fields as $field) { |
||
| 54 | if ($field->save && !empty($data[$field->id])) { |
||
| 55 | $name .= htmlspecialchars($data[$field->id]) . ' '; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | $name = trim($name); |
||
| 59 | |||
| 60 | $userAdds = Ecommerce\UserAdds::get([['user_id', $cart->user->id], ['name', $name]]); |
||
| 61 | if (!$userAdds) { |
||
| 62 | $userAdds = new Ecommerce\UserAdds(); |
||
| 63 | $userAdds->user_id = $cart->user->id; |
||
| 64 | $userAdds->name = $name; |
||
| 65 | $userAdds->save(); |
||
| 66 | foreach ($fields as $field) { |
||
| 67 | if (!$field->save) { |
||
| 68 | continue; |
||
| 69 | } |
||
| 70 | $userAddsValue = new Ecommerce\UserAdds\Value(); |
||
| 71 | $userAddsValue->value = htmlspecialchars($data[$field->id]); |
||
| 72 | $userAddsValue->useradds_field_id = $field->id; |
||
| 73 | $userAddsValue->useradds_id = $userAdds->id; |
||
| 74 | $userAddsValue->save(); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | $user = \Users\User::get($cart->user_id); |
||
| 78 | foreach ($fields as $field) { |
||
| 79 | $info = new \Ecommerce\Cart\Info(); |
||
| 80 | $info->name = $field->name; |
||
| 81 | $info->value = htmlspecialchars($data[$field->id]); |
||
| 82 | $info->useradds_field_id = $field->id; |
||
| 83 | $info->cart_id = $cart->id; |
||
| 84 | $info->save(); |
||
| 85 | $relations = []; |
||
| 86 | if ($field->userfield) { |
||
| 87 | if (strpos($field->userfield, ':')) { |
||
| 88 | $path = explode(':', $field->userfield); |
||
| 89 | if (!$user->{$path[0]}->{$path[1]}) { |
||
| 90 | $user->{$path[0]}->{$path[1]} = $info->value; |
||
| 91 | $relations[$path[0]] = $path[0]; |
||
| 92 | } |
||
| 93 | } else { |
||
| 94 | if (!$user->{$field->userfield}) { |
||
| 95 | $user->{$field->userfield} = $info->value; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | foreach ($relations as $rel) { |
||
| 100 | $user->$rel->save(); |
||
| 101 | } |
||
| 102 | $user->save(); |
||
| 103 | } |
||
| 104 | return $userAdds; |
||
| 105 | } |
||
| 106 | |||
| 107 | View Code Duplication | public function parseDeliveryFields($data, $cart, $fields) |
|
| 163 | |||
| 164 | public function getCurCart($create = true) |
||
|
1 ignored issue
–
show
|
|||
| 165 | { |
||
| 166 | $cart = false; |
||
| 167 | if (!empty($_SESSION['cart']['cart_id'])) { |
||
| 183 | |||
| 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 | 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 | 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 | if (is_array($optionValue)) { |
||
| 274 | $optionValueArr = []; |
||
| 275 | foreach ($optionValue as $val) { |
||
| 276 | $optionValueArr[] = \App::$cur->db->connection->pdo->quote($val); |
||
| 277 | } |
||
| 278 | $qstr = 'IN (' . implode(',', $optionValueArr) . ')'; |
||
| 279 | } else { |
||
| 280 | $qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue); |
||
| 281 | } |
||
| 282 | $selectOptions['join'][] = [Ecommerce\Item\Offer\Param::table(), Ecommerce\Item\Offer::index() . ' = ' . 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer::index() . ' AND ' . |
||
| 283 | 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer\Option::index() . ' = "' . (int) $optionId . '" AND ' . |
||
| 284 | 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . 'value ' . $qstr, |
||
| 285 | 'inner', 'offerOption' . $optionId]; |
||
| 286 | } |
||
| 287 | break; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | //parents |
||
| 292 | if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) { |
||
| 293 | $first = true; |
||
| 294 | $where = []; |
||
| 295 | foreach (explode(',', $options['parent']) as $categoryId) { |
||
| 296 | if (!$categoryId) { |
||
| 297 | continue; |
||
| 298 | } |
||
| 299 | $category = \Ecommerce\Category::get($categoryId); |
||
| 300 | $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR']; |
||
| 301 | $first = false; |
||
| 302 | } |
||
| 303 | $selectOptions['where'][] = $where; |
||
| 304 | } elseif (!empty($options['parent'])) { |
||
| 305 | $category = \Ecommerce\Category::get($options['parent']); |
||
| 306 | $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE']; |
||
| 307 | } |
||
| 308 | |||
| 309 | //search |
||
| 310 | if (!empty($options['search'])) { |
||
| 311 | $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']); |
||
| 312 | $searchArr = []; |
||
| 313 | foreach (explode(' ', $searchStr) as $part) { |
||
| 314 | $part = trim($part); |
||
| 315 | if ($part && strlen($part) > 2) { |
||
| 316 | $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE']; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | if (!empty($searchArr)) { |
||
| 320 | $selectOptions['where'][] = $searchArr; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | if (empty($this->config['view_empty_warehouse'])) { |
||
| 324 | $warehouseIds = []; |
||
| 325 | View Code Duplication | if (class_exists('Geography\City\Data')) { |
|
| 326 | $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]); |
||
| 327 | if ($warehouses && $warehouses->data) { |
||
| 328 | foreach (explode(',', $warehouses->data) as $id) { |
||
| 329 | $warehouseIds[$id] = $id; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | $selectOptions['where'][] = [ |
||
| 334 | '( |
||
| 335 | (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0) |
||
| 336 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw |
||
| 337 | WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ' |
||
| 338 | ' . ($warehouseIds ? ' AND iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . ' |
||
| 339 | ) |
||
| 340 | - |
||
| 341 | (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0) |
||
| 342 | FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb |
||
| 343 | 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 ( |
||
| 344 | (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || |
||
| 345 | (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE)) |
||
| 346 | ) |
||
| 347 | WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ') |
||
| 348 | )', |
||
| 349 | 0, |
||
| 350 | '>' |
||
| 351 | ]; |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | $selectOptions['group'] = Ecommerce\Item::index(); |
||
| 360 | |||
| 361 | return $selectOptions; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Getting items params with params |
||
| 366 | * |
||
| 367 | * @param array $params |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public function getItemsParams($params = []) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Getting items with params |
||
| 384 | * |
||
| 385 | * @param array $params |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | public function getItems($params = []) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Return count of items with params |
||
| 397 | * |
||
| 398 | * @param array $params |
||
| 399 | * @return int |
||
| 400 | */ |
||
| 401 | public function getItemsCount($params = []) |
||
| 415 | |||
| 416 | View Code Duplication | public function viewsCategoryList($inherit = true) |
|
| 433 | |||
| 434 | View Code Duplication | public function templatesCategoryList() |
|
| 450 | |||
| 451 | public function cartStatusDetector($event) |
||
| 473 | |||
| 474 | public function cardTrigger($event) |
||
| 491 | |||
| 492 | public function bonusTrigger($event) |
||
| 514 | |||
| 515 | View Code Duplication | function sitemap() |
|
| 550 | |||
| 551 | } |
||
| 552 |
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: