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 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 | public $manifest = array( |
||
| 5 | 'package' => 'core', |
||
| 6 | 'name' => 'sn_module', |
||
| 7 | 'version' => '1c0', |
||
| 8 | 'copyright' => 'Project "SuperNova.WS" #41a6.35# copyright © 2009-2014 Gorlum', |
||
| 9 | |||
| 10 | // 'require' => null, |
||
|
|
|||
| 11 | 'root_relative' => '', |
||
| 12 | |||
| 13 | 'installed' => true, |
||
| 14 | 'active' => true, |
||
| 15 | |||
| 16 | // 'constants' array - contents of this array would be instaled into engine |
||
| 17 | 'constants' => array( |
||
| 18 | // 'UNIT_STRUCTURE_NEW' => 999999, |
||
| 19 | ), |
||
| 20 | |||
| 21 | '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 |
||
| 22 | |||
| 23 | // 'functions' array - this functions would be installed as hooks |
||
| 24 | // Key: overwritable function name to replace |
||
| 25 | // Value: which method to use. Format: [*][<object_name>][.]<method> |
||
| 26 | // '*' means that new function would replace old one |
||
| 27 | // If object_name is ommited but "." is present - hook linked to global function |
||
| 28 | // If only "method" present - overwritable linked to appropriate method of current object |
||
| 29 | // Function/Method should be accessible on module init |
||
| 30 | 'functions' => array( |
||
| 31 | // 'test_object_test_method' => 'test_object.test_method', |
||
| 32 | // 'test_function' => '.my_test_function', |
||
| 33 | // 'this_object_test_method' => 'test_method', |
||
| 34 | ), |
||
| 35 | |||
| 36 | // 'menu' array - this menu items would be merged into main game menu |
||
| 37 | // Array element almost identical to $sn_menu with additional param 'LOCATION'. |
||
| 38 | // 'LOCATION' => '-news', // Special atrtribute for modules |
||
| 39 | // [-|+][<menu_item_id>] |
||
| 40 | // <menu_item_id> identifies menu item aginst new menu item would be placed. When ommited new item placed against whole menu |
||
| 41 | // -/+ 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 |
||
| 42 | // Empty or non-existent LOCATION equivalent to '+' - place item at end of menu |
||
| 43 | // Non-existent menu_item_id treated as ommited |
||
| 44 | 'menu' => array(), |
||
| 45 | |||
| 46 | // 'page' array - defines pages which will handle this module and appropriate handlers |
||
| 47 | 'page' => array(), |
||
| 48 | ); |
||
| 49 | |||
| 50 | protected $config = array(); |
||
| 51 | |||
| 52 | protected $module_full_class_path = __FILE__; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Динамическое назначение переменных |
||
| 56 | * |
||
| 57 | * Актуально, когда записываемые данные зависят от статуса игры |
||
| 58 | * Например - назначаются константы внутри модуля |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | function __assign_vars() { |
||
| 87 | |||
| 88 | function loadModuleRootConfig() { |
||
| 101 | |||
| 102 | function __construct($filename = __FILE__) { |
||
| 131 | |||
| 132 | protected function __patch_menu(&$sn_menu_extra, &$menu_patch) { |
||
| 139 | |||
| 140 | |||
| 141 | public function initialize() { |
||
| 246 | |||
| 247 | function check_status() { |
||
| 249 | } |
||
| 250 | |||
| 251 |
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.