Complex classes like Main 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 Main, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Main |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Config class instance |
||
26 | * @var \gplcart\core\Config $config |
||
27 | */ |
||
28 | protected $config; |
||
29 | |||
30 | /** |
||
31 | * @param Config $config |
||
32 | */ |
||
33 | public function __construct(Config $config) |
||
37 | |||
38 | /** |
||
39 | * Implements hook "module.install.before" |
||
40 | * @param null|string $result |
||
41 | */ |
||
42 | public function hookModuleInstallBefore(&$result) |
||
52 | |||
53 | /** |
||
54 | * Implements hook "module.uninstall.before" |
||
55 | */ |
||
56 | public function hookModuleUninstallAfter() |
||
60 | |||
61 | /** |
||
62 | * Implements hook "module.api.process" |
||
63 | * @param array $params |
||
64 | * @param array $user |
||
65 | * @param mixed $response |
||
66 | */ |
||
67 | public function hookModuleApiProcess(array $params, array $user, &$response) |
||
79 | |||
80 | /** |
||
81 | * Executes a command using an array of API request parameters |
||
82 | * @param array $params |
||
83 | * @param array $user |
||
84 | * @param bool $return_string |
||
85 | * @return string|array |
||
86 | */ |
||
87 | public function exec(array $params, array $user, $return_string = true) |
||
112 | |||
113 | /** |
||
114 | * Returns a saved path to PHP executable file |
||
115 | * @return string |
||
116 | */ |
||
117 | protected function getPhpExe() |
||
128 | |||
129 | /** |
||
130 | * Sets the path to PHP executable file |
||
131 | * @param string $file |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function setPhpExe($file) |
||
138 | |||
139 | /** |
||
140 | * Returns the path to PHP executable file |
||
141 | * @return string |
||
142 | * @throws RuntimeException |
||
143 | */ |
||
144 | public function findPhpExe() |
||
227 | |||
228 | /** |
||
229 | * Convert an array of params into a CLI command |
||
230 | * @param array $params |
||
231 | * @param array $user |
||
232 | * @throws RuntimeException |
||
233 | * @throws UnexpectedValueException |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getCommand(array $params, array $user) |
||
271 | |||
272 | /** |
||
273 | * Returns CLI router instance |
||
274 | * @return \gplcart\core\CliRoute |
||
275 | */ |
||
276 | protected function getCliRouteInstance() |
||
282 | } |
||
283 |