Complex classes like UnitList 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 UnitList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class UnitList extends ArrayAccessV2 implements IDbRow, ILocation { |
||
| 16 | |||
| 17 | |||
| 18 | // Properties ******************************************************************************************************** |
||
| 19 | |||
| 20 | // ILocation implementation ========================================================================================== |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Type of this location |
||
| 24 | * |
||
| 25 | * @var int $locationType |
||
| 26 | */ |
||
| 27 | protected static $locationType = LOC_UNIT_LIST; |
||
| 28 | /** |
||
| 29 | * @var ILocation $locatedAt |
||
| 30 | */ |
||
| 31 | protected $locatedAt = null; |
||
| 32 | |||
| 33 | |||
| 34 | // New properties ==================================================================================================== |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Unit[] $mapUnitIdToDb |
||
| 38 | */ |
||
| 39 | // Нужно для корректного сохранения новых юнитов. Их db_id = 0, поэтому при добавлении в контейнер они будут перезаписывать друг друга |
||
| 40 | // Соответственно - при сохраненнии флота надо проходить dbSave именно по $mapUnitIdToDb |
||
| 41 | protected $mapUnitIdToDb = array(); |
||
| 42 | |||
| 43 | |||
| 44 | // Methods *********************************************************************************************************** |
||
| 45 | |||
| 46 | // ILocation implementation ========================================================================================== |
||
| 47 | |||
| 48 | public function getPlayerOwnerId() { |
||
| 51 | |||
| 52 | public function getLocationType() { |
||
| 55 | |||
| 56 | public function getLocationDbId() { |
||
| 59 | |||
| 60 | // TODO - достаточно установить один раз Unit::LocatedAt на UnitList, что бы затем все юниты автоматически брали наиболее актуальный locatedAt |
||
| 61 | public function setLocatedAt($location) { |
||
| 68 | |||
| 69 | public function getLocatedAt() { |
||
| 72 | |||
| 73 | public function getLocatedAtType() { |
||
| 76 | |||
| 77 | public function getLocatedAtDbId() { |
||
| 80 | |||
| 81 | |||
| 82 | // ArrayAccessV2 inheritance ========================================================================================= |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Adds link to unit object also to mapUnitIdToDb |
||
| 86 | * |
||
| 87 | * @param mixed $offset |
||
| 88 | * @param Unit $value |
||
| 89 | */ |
||
| 90 | public function offsetSet($offset, $value) { |
||
| 97 | |||
| 98 | public function offsetUnset($offset) { |
||
| 107 | |||
| 108 | |||
| 109 | // IDbRow implementation ============================================================================================= |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Loading object from DB by primary ID |
||
| 113 | * Real location should be set before calling this method |
||
| 114 | * |
||
| 115 | * @param int $dbId - dbId is generally unused here. However it works as flag: 0 - just reset; (negative) - just reset; (positive) - proceed with loading |
||
| 116 | */ |
||
| 117 | // TODO: Implement dbLoad() method. |
||
| 118 | public function dbLoad($dbId) { |
||
| 148 | |||
| 149 | public function dbSave() { |
||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | // Other ============================================================================================================= |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return Unit |
||
| 183 | * |
||
| 184 | * @version 41a6.30 |
||
| 185 | */ |
||
| 186 | // TODO - Factory |
||
| 187 | public function _createElement() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Set unit count of $unit_id to $unit_count |
||
| 193 | * If there is no $unit_id - it will be created and saved to DB on dbSave |
||
| 194 | * |
||
| 195 | * @param int $unit_id |
||
| 196 | * @param int $unit_count |
||
| 197 | */ |
||
| 198 | public function unitSetCount($unit_id, $unit_count = 0) { |
||
| 201 | |||
| 202 | public function unitGetCount($unit_id) { |
||
| 208 | |||
| 209 | public function unitsCountApplyLossMultiplier($ships_lost_multiplier) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Adjust unit count of $unit_id by $unit_count - or just replace value |
||
| 217 | * If there is no $unit_id - it will be created and saved to DB on dbSave |
||
| 218 | * |
||
| 219 | * @param int $unit_id |
||
| 220 | * @param int $unit_count |
||
| 221 | * @param bool $replace_value |
||
| 222 | */ |
||
| 223 | public function unitAdjustCount($unit_id, $unit_count = 0, $replace_value = false) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get unit list in array as $unit_id => $unit_count |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public function unitsGetArray() { |
||
| 251 | |||
| 252 | public function unitsCount() { |
||
| 255 | |||
| 256 | public function unitsCapacity() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get count of units in UnitList by unit_id (or all units if unit_id == 0) |
||
| 262 | * |
||
| 263 | * @param int $unit_id - 0 - all units |
||
| 264 | * |
||
| 265 | * @return int |
||
| 266 | */ |
||
| 267 | public function unitsCountById($unit_id = 0) { |
||
| 277 | |||
| 278 | |||
| 279 | // // TODO - revise it later |
||
| 280 | // public function _reset() { |
||
| 281 | // //if(!empty($this->mapUnitIdToDb)) { |
||
| 282 | // // foreach($this->mapUnitIdToDb as $unit_id => $object) { |
||
| 283 | // // unset($this->mapUnitIdToDb[$unit_id]); |
||
| 284 | // // } |
||
| 285 | // //} |
||
| 286 | // unset($this->mapUnitIdToDb); |
||
| 287 | // $this->mapUnitIdToDb = array(); |
||
| 288 | // |
||
| 289 | // //if(!empty($this->_container)) { |
||
| 290 | // // foreach($this->_container as $unit_db_id => $object) { |
||
| 291 | // // unset($this->_container[$unit_db_id]); |
||
| 292 | // // } |
||
| 293 | // //} |
||
| 294 | // unset($this->_container); |
||
| 295 | // $this->_container = array(); |
||
| 296 | // } |
||
| 297 | |||
| 298 | |||
| 299 | // TODO - DEBUG - REMOVE ============================================================================================= |
||
| 300 | public function _dump() { |
||
| 399 | |||
| 400 | |||
| 401 | public function unitZeroDbId() { |
||
| 406 | |||
| 407 | |||
| 408 | public function unitZeroCount() { |
||
| 413 | |||
| 414 | } |
||
| 415 |
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.