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 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 ContainerArrayOfObject 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() { |
||
| 49 | return is_object($this->locatedAt) ? $this->locatedAt->getPlayerOwnerId() : null; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function getLocationType() { |
||
| 53 | return is_object($this->locatedAt) ? $this->locatedAt->getLocationType() : LOC_NONE; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function getLocationDbId() { |
||
| 57 | return is_object($this->locatedAt) ? $this->locatedAt->getLocationDbId() : null; |
||
| 58 | } |
||
| 59 | |||
| 60 | // TODO - достаточно установить один раз Unit::LocatedAt на UnitList, что бы затем все юниты автоматически брали наиболее актуальный locatedAt |
||
| 61 | public function setLocatedAt($location) { |
||
| 62 | $this->locatedAt = $location; |
||
| 63 | // TODO - по факту не нужно - достточно один раз поставить на $this |
||
| 64 | // foreach($this->_container as $unit) { |
||
| 65 | // $unit->setLocatedAt($this->locatedAt); |
||
| 66 | // } |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getLocatedAt() { |
||
| 72 | |||
| 73 | public function getLocatedAtType() { |
||
| 74 | return is_object($this->locatedAt) ? $this->locatedAt->getLocationType() : LOC_NONE; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function getLocatedAtDbId() { |
||
| 78 | return is_object($this->locatedAt) ? $this->locatedAt->getLocationDbId() : 0; |
||
| 79 | } |
||
| 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) { |
||
| 99 | if (!empty($this[$offset]->unitId)) { |
||
| 100 | // $unit_id = $this[$offset]->unitId; |
||
|
|
|||
| 101 | // $this->mapUnitIdToDb[$unit_id] = null; |
||
| 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 | public function dbLoad($dbId, $lockSkip = false) { |
||
| 146 | |||
| 147 | public function dbSave() { |
||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | // Other ============================================================================================================= |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return Unit |
||
| 181 | * |
||
| 182 | */ |
||
| 183 | // TODO - Factory |
||
| 184 | 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) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Adjust unit count of $unit_id by $unit_count - or just replace value |
||
| 212 | * If there is no $unit_id - it will be created and saved to DB on dbSave |
||
| 213 | * |
||
| 214 | * @param int $unit_id |
||
| 215 | * @param int $unit_count |
||
| 216 | * @param bool $replace_value |
||
| 217 | */ |
||
| 218 | public function unitAdjustCount($unit_id, $unit_count = 0, $replace_value = false) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return UnitIterator |
||
| 235 | */ |
||
| 236 | public function getUnitIterator() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param array $shipCostInMetalPerPiece - cost in metal by unitId |
||
| 244 | * |
||
| 245 | * @return float[] |
||
| 246 | */ |
||
| 247 | public function unitsCostInMetal($shipCostInMetalPerPiece) { |
||
| 255 | |||
| 256 | public function unitsCountApplyLossMultiplier($ships_lost_multiplier) { |
||
| 261 | |||
| 262 | public function unitsCount() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get count of units in UnitList by unit_id (or all units if unit_id == 0) |
||
| 268 | * |
||
| 269 | * @param int $unit_id - 0 - all units |
||
| 270 | * |
||
| 271 | * @return int |
||
| 272 | */ |
||
| 273 | public function unitsCountById($unit_id = 0) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param int $unit_id |
||
| 279 | * @param string $propertyName |
||
| 280 | * |
||
| 281 | * @return int |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function unitsPropertySumById($unit_id = 0, $propertyName = 'count') { |
|
| 293 | |||
| 294 | // TODO - WRONG FOR STRUCTURES |
||
| 295 | public function shipsCapacity() { |
||
| 298 | |||
| 299 | // TODO - WRONG FOR STRUCTURES |
||
| 300 | View Code Duplication | public function shipsPoolPropertySumById($unit_id = 0, $propertyName = 'count') { |
|
| 310 | |||
| 311 | View Code Duplication | public function shipsIsEnoughOnPlanet($dbOwnerRow, $dbPlanetRow) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @return array |
||
| 323 | * @throws Exception |
||
| 324 | */ |
||
| 325 | public function unitsRender() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param $user |
||
| 360 | * |
||
| 361 | * @return int|mixed |
||
| 362 | */ |
||
| 363 | // TODO - REDO!!!! |
||
| 364 | public function shipsSpeedMin($user) { |
||
| 377 | |||
| 378 | |||
| 379 | // TODO - REDO!!!! |
||
| 380 | public function travelData($speed_percent = 10, $distance, $dbOwnerRow) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param $group |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | public function unitsInGroup($group) { |
||
| 434 | |||
| 435 | View Code Duplication | public function unitsIsAllMovable($dbOwnerRow) { |
|
| 445 | |||
| 446 | public function unitsPositive() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param array $dbOwnerRow |
||
| 458 | * @param int $sourcePlanetRowId |
||
| 459 | */ |
||
| 460 | public function dbSubstractUnitsFromPlanet($dbOwnerRow, $sourcePlanetRowId) { |
||
| 465 | |||
| 466 | |||
| 467 | // TODO - DEBUG - REMOVE ============================================================================================= |
||
| 468 | public function _dump() { |
||
| 565 | |||
| 566 | public function unitZeroDbId() { |
||
| 571 | |||
| 572 | public function unitZeroCount() { |
||
| 577 | |||
| 578 | } |
||
| 579 |
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.