Complex classes like sn_module 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 sn_module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class sn_module { |
||
4 | /** |
||
5 | * @var sn_module[] |
||
6 | */ |
||
7 | protected static $sn_module = array(); |
||
8 | |||
9 | public static $sn_module_list = array(); |
||
10 | |||
11 | public $manifest = array( |
||
12 | 'package' => 'core', |
||
13 | 'name' => 'sn_module', |
||
14 | 'version' => '1c0', |
||
15 | 'copyright' => 'Project "SuperNova.WS" #41a52.93# copyright © 2009-2014 Gorlum', |
||
16 | |||
17 | 'require' => array(), |
||
18 | 'root_relative' => '', |
||
19 | |||
20 | 'installed' => true, |
||
21 | 'active' => true, |
||
22 | |||
23 | // 'constants' array - contents of this array would be instaled into engine. 'UNIT_STRUCTURE_NEW' => 999999, |
||
24 | 'constants' => array(), |
||
25 | |||
26 | 'vars' => array(), // Just a placeholder. vars assigned via special method __assign_vars(). Need 'cause of new constants that can be defined within module. See below |
||
27 | |||
28 | /** |
||
29 | * 'functions' array - this functions would be installed as hooks |
||
30 | * Key: overwritable function name to replace |
||
31 | * Value: which method to use. Format: [*][<object_name>][.]<method> |
||
32 | * '*' means that new function would replace old one |
||
33 | * If object_name is ommited but "." is present - hook linked to global function |
||
34 | * If only "method" present - overwritable linked to appropriate method of current object |
||
35 | * Examples: |
||
36 | * 'test_object.test_method' - will |
||
37 | * Function/Method should be accessible on module init |
||
38 | * // 'test_object_test_method' => 'test_object.test_method', |
||
39 | * // 'test_function' => '.my_test_function', |
||
40 | * // 'this_object_test_method' => 'test_method', |
||
41 | */ |
||
42 | 'functions' => array(), |
||
43 | |||
44 | // 'menu' array - this menu items would be merged into main game menu |
||
45 | // Array element almost identical to $sn_menu with additional param 'LOCATION'. |
||
46 | // 'LOCATION' => '-news', // Special atrtribute for modules |
||
|
|||
47 | // [-|+][<menu_item_id>] |
||
48 | // <menu_item_id> identifies menu item aginst new menu item would be placed. When ommited new item placed against whole menu |
||
49 | // -/+ indicates that new item should be placed before/after identified menu item (or whole menu). If ommited and menu item exists - new item will replace previous one |
||
50 | // Empty or non-existent LOCATION equivalent to '+' - place item at end of menu |
||
51 | // Non-existent menu_item_id treated as ommited |
||
52 | 'menu' => array(), |
||
53 | |||
54 | // 'page' array - defines pages which will handle this module and appropriate handlers |
||
55 | 'page' => array(), |
||
56 | ); |
||
57 | |||
58 | protected $config = array(); |
||
59 | |||
60 | protected $module_full_class_path = __FILE__; |
||
61 | |||
62 | /** |
||
63 | * Динамическое назначение переменных |
||
64 | * |
||
65 | * Актуально, когда записываемые данные зависят от статуса игры |
||
66 | * Например - назначаются константы внутри модуля |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function __assign_vars() { |
||
73 | |||
74 | public function loadModuleRootConfig() { |
||
86 | |||
87 | public function __construct($filename = __FILE__) { |
||
114 | |||
115 | |||
116 | public function initialize() { |
||
138 | |||
139 | protected function setSystemConstants() { |
||
149 | |||
150 | protected function setSystemVariables() { |
||
192 | |||
193 | protected function mergeMenu(&$sn_menu_extra, &$menu_patch) { |
||
202 | |||
203 | protected function addSystemHandlers() { |
||
211 | |||
212 | protected function mergeNavbarButton() { |
||
221 | |||
222 | protected function mergeI18N() { |
||
240 | |||
241 | /** |
||
242 | * @param string $arrayName - name of manifest fields to get data from |
||
243 | */ |
||
244 | protected function mergeArraySpecial($arrayName) { |
||
247 | |||
248 | /** |
||
249 | * @param array $arrayFrom |
||
250 | * @param array $arrayMergeTo |
||
251 | */ |
||
252 | protected function mergeArrayParam(&$arrayFrom, &$arrayMergeTo) { |
||
264 | |||
265 | protected function mergeCss() { $this->mergeArrayParam($this->manifest['css'], classSupernova::$css); } |
||
266 | |||
267 | protected function mergeJavascript() { $this->mergeArraySpecial('javascript'); } |
||
268 | |||
269 | public function check_status() { } |
||
270 | |||
271 | public static function orderModules() { |
||
339 | |||
340 | /** |
||
341 | * @param string $moduleName |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | public static function isModuleActive($moduleName) { |
||
353 | |||
354 | /** |
||
355 | * @param string $moduleName |
||
356 | * |
||
357 | * @return sn_module |
||
358 | */ |
||
359 | public static function getModule($moduleName) { |
||
362 | |||
363 | } |
||
364 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.