Complex classes like MyObject 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 MyObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class MyObject extends CommonObject |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var string ID to identify managed object |
||
| 40 | */ |
||
| 41 | public $element = 'myobject'; |
||
| 42 | /** |
||
| 43 | * @var string Name of table without prefix where object is stored |
||
| 44 | */ |
||
| 45 | public $table_element = 'myobject'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array Does this field is linked to a thirdparty ? |
||
| 49 | */ |
||
| 50 | protected $isnolinkedbythird = 1; |
||
| 51 | /** |
||
| 52 | * @var array Does myobject support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
| 53 | */ |
||
| 54 | protected $ismultientitymanaged = 1; |
||
| 55 | /** |
||
| 56 | * @var string String with name of icon for myobject |
||
| 57 | */ |
||
| 58 | public $picto = 'myobject@mymodule'; |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * 'type' if the field format, 'label' the translation key, 'enabled' is a condition when the filed must be managed, |
||
| 63 | * 'visible' says if field is visible in list (-1 means not shown by default but can be aded into list to be viewed) |
||
| 64 | * 'notnull' if not null in database |
||
| 65 | * 'index' if we want an index in database |
||
| 66 | * 'position' is the sort order of field |
||
| 67 | * 'searchall' is 1 if we want to search in this field when making a search from the quick search button |
||
| 68 | * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8). |
||
| 69 | * 'comment' is not used. You can store here any text of your choice. |
||
| 70 | */ |
||
| 71 | |||
| 72 | // BEGIN MODULEBUILDER PROPERTIES |
||
| 73 | /** |
||
| 74 | * @var array Array with all fields and their property |
||
| 75 | */ |
||
| 76 | public $fields=array( |
||
| 77 | 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'index'=>true, 'position'=>1, 'comment'=>'Id'), |
||
| 78 | 'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>true, 'index'=>true, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'), |
||
| 79 | 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>true, 'index'=>true, 'position'=>20), |
||
| 80 | 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1), |
||
| 81 | 'amount' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>1, 'position'=>40, 'searchall'=>0, 'isameasure'=>1), |
||
| 82 | 'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'index'=>true, 'position'=>1000), |
||
| 83 | 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'position'=>500), |
||
| 84 | 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'position'=>500), |
||
| 85 | //'date_valid' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>500), |
||
| 86 | 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'position'=>500), |
||
| 87 | 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-1, 'position'=>500), |
||
| 88 | //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValid', 'enabled'=>1, 'visible'=>-1, 'position'=>500), |
||
| 89 | 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>true, 'position'=>500), |
||
| 90 | 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'index'=>true, 'position'=>1000, 'nullifempty'=>1), |
||
| 91 | ); |
||
| 92 | // END MODULEBUILDER PROPERTIES |
||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | // If this object has a subtable with lines |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int Name of subtable line |
||
| 100 | */ |
||
| 101 | //public $table_element_line = 'myobjectdet'; |
||
| 102 | /** |
||
| 103 | * @var int Field with ID of parent key if this field has a parent |
||
| 104 | */ |
||
| 105 | //public $fk_element = 'fk_myobject'; |
||
| 106 | /** |
||
| 107 | * @var int Name of subtable class that manage subtable lines |
||
| 108 | */ |
||
| 109 | //public $class_element_line = 'MyObjectline'; |
||
| 110 | /** |
||
| 111 | * @var array Array of child tables (child tables to delete before deleting a record) |
||
| 112 | */ |
||
| 113 | //protected $childtables=array('myobjectdet'); |
||
| 114 | /** |
||
| 115 | * @var MyObjectLine[] Array of subtable lines |
||
| 116 | */ |
||
| 117 | //public $lines = array(); |
||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Constructor |
||
| 123 | * |
||
| 124 | * @param DoliDb $db Database handler |
||
| 125 | */ |
||
| 126 | public function __construct(DoliDB $db) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Create object into database |
||
| 133 | * |
||
| 134 | * @param User $user User that creates |
||
| 135 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 136 | * @return int <0 if KO, Id of created object if OK |
||
| 137 | */ |
||
| 138 | public function create(User $user, $notrigger = false) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Load object in memory from the database |
||
| 145 | * |
||
| 146 | * @param int $id Id object |
||
| 147 | * @param string $ref Ref |
||
| 148 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 149 | */ |
||
| 150 | public function fetch($id, $ref = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Update object into database |
||
| 157 | * |
||
| 158 | * @param User $user User that modifies |
||
| 159 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 160 | * @return int <0 if KO, >0 if OK |
||
| 161 | */ |
||
| 162 | public function update(User $user, $notrigger = false) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Delete object in database |
||
| 169 | * |
||
| 170 | * @param User $user User that deletes |
||
| 171 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 172 | * @return int <0 if KO, >0 if OK |
||
| 173 | */ |
||
| 174 | public function delete(User $user, $notrigger = false) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Return a link to the object card (with optionaly the picto) |
||
| 181 | * |
||
| 182 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
| 183 | * @param string $option On what the link point to |
||
| 184 | * @param int $notooltip 1=Disable tooltip |
||
| 185 | * @param string $morecss Add more css on link |
||
| 186 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 187 | * @return string String with URL |
||
| 188 | */ |
||
| 189 | function getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Retourne le libelle du status d'un user (actif, inactif) |
||
| 243 | * |
||
| 244 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
| 245 | * @return string Label of status |
||
| 246 | */ |
||
| 247 | function getLibStatut($mode=0) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return the status |
||
| 254 | * |
||
| 255 | * @param int $status Id status |
||
| 256 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 5=Long label + Picto |
||
| 257 | * @return string Label of status |
||
| 258 | */ |
||
| 259 | static function LibStatut($status,$mode=0) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Charge les informations d'ordre info dans l'objet commande |
||
| 303 | * |
||
| 304 | * @param int $id Id of order |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | function info($id) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Initialise object with example values |
||
| 357 | * Id must be 0 if object instance is a specimen |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function initAsSpecimen() |
||
| 365 | |||
| 366 | } |
||
| 367 | |||
| 386 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.