Complex classes like EcommerceController 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 EcommerceController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class EcommerceController extends adminController |
||
12 | { |
||
13 | public function dashboardAction() |
||
18 | |||
19 | public function configureAction() |
||
1 ignored issue
–
show
|
|||
20 | { |
||
21 | if (!empty($_POST['config'])) { |
||
22 | $config = App::$cur->ecommerce->config; |
||
23 | $config['view_empty_warehouse'] = empty($_POST['config']['view_empty_warehouse']) ? false : true; |
||
24 | $config['view_empty_image'] = empty($_POST['config']['view_empty_image']) ? false : true; |
||
25 | $config['sell_empty_warehouse'] = empty($_POST['config']['sell_empty_warehouse']) ? false : true; |
||
26 | $config['sell_over_warehouse'] = empty($_POST['config']['sell_over_warehouse']) ? false : true; |
||
27 | $config['notify_mail'] = $_POST['config']['notify_mail']; |
||
28 | $config['defaultCategoryView'] = $_POST['config']['defaultCategoryView']; |
||
29 | $config['defaultCurrency'] = $_POST['config']['defaultCurrency']; |
||
30 | $config['orderPrefix'] = $_POST['config']['orderPrefix']; |
||
31 | $config['show_zero_price'] = empty($_POST['config']['show_zero_price']) ? false : true; |
||
32 | $config['show_without_price'] = empty($_POST['config']['show_without_price']) ? false : true; |
||
33 | Config::save('module', $config, 'Ecommerce'); |
||
34 | Tools::redirect('/admin/ecommerce/configure', 'Настройки были изменены', 'success'); |
||
35 | } |
||
36 | $managers = [ |
||
37 | 'Ecommerce\Delivery', |
||
38 | 'Ecommerce\PayType', |
||
39 | 'Ecommerce\Warehouse', |
||
40 | 'Ecommerce\Unit', |
||
41 | 'Ecommerce\Card', |
||
42 | 'Ecommerce\Discount', |
||
43 | 'Ecommerce\Cart\Stage', |
||
44 | 'Ecommerce\Item\Type', |
||
45 | 'Ecommerce\Item\Option', |
||
46 | 'Ecommerce\Item\Offer\Option', |
||
47 | 'Ecommerce\Item\Offer\Price\Type', |
||
48 | 'Ecommerce\UserAdds\Field', |
||
49 | 'Ecommerce\Cart\Status', |
||
50 | 'Ecommerce\Delivery\Field', |
||
51 | ]; |
||
52 | $this->view->setTitle('Настройки магазина'); |
||
53 | $this->view->page(['data' => compact('managers')]); |
||
54 | } |
||
55 | |||
56 | public function reBlockIndexAction() |
||
65 | |||
66 | public function reSearchIndexAction($i = 0) |
||
84 | |||
85 | public function newOrdersSubscribeAction() |
||
89 | |||
90 | public function closeCartAction($cartId = 0) |
||
104 | |||
105 | } |
||
106 |
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: