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 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 | 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 | * @version 41a6.79 |
||
183 | */ |
||
184 | // TODO - Factory |
||
185 | public function _createElement() { |
||
191 | |||
192 | /** |
||
193 | * Set unit count of $unit_id to $unit_count |
||
194 | * If there is no $unit_id - it will be created and saved to DB on dbSave |
||
195 | * |
||
196 | * @param int $unit_id |
||
197 | * @param int $unit_count |
||
198 | */ |
||
199 | public function unitSetCount($unit_id, $unit_count = 0) { |
||
202 | |||
203 | 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 | * Get unit list in array as $unit_id => $unit_count |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function unitsGetArray() { |
||
246 | |||
247 | public function unitsCountApplyLossMultiplier($ships_lost_multiplier) { |
||
252 | |||
253 | public function unitsCount() { |
||
256 | |||
257 | /** |
||
258 | * Get count of units in UnitList by unit_id (or all units if unit_id == 0) |
||
259 | * |
||
260 | * @param int $unit_id - 0 - all units |
||
261 | * |
||
262 | * @return int |
||
263 | */ |
||
264 | public function unitsCountById($unit_id = 0) { |
||
267 | |||
268 | View Code Duplication | public function unitsPropertySumById($unit_id = 0, $propertyName = 'count') { |
|
278 | |||
279 | // TODO - WRONG FOR STRUCTURES |
||
280 | public function shipsCapacity() { |
||
283 | |||
284 | // TODO - WRONG FOR STRUCTURES |
||
285 | View Code Duplication | public function shipsPoolPropertySumById($unit_id = 0, $propertyName = 'count') { |
|
295 | |||
296 | public function shipsIsEnoughOnPlanet($dbPlanetRow) { |
||
306 | |||
307 | /** |
||
308 | * @return array |
||
309 | * @throws Exception |
||
310 | */ |
||
311 | public function unitsRender() { |
||
343 | |||
344 | /** |
||
345 | * @param $user |
||
346 | * |
||
347 | * @return int|mixed |
||
348 | */ |
||
349 | // TODO - REDO!!!! |
||
350 | public function shipsSpeedMin($user) { |
||
363 | |||
364 | |||
365 | // TODO - REDO!!!! |
||
366 | public function travelData($speed_percent = 10, $distance, $dbOwnerRow) { |
||
405 | |||
406 | /** |
||
407 | * @param $group |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function unitsInGroup($group) { |
||
420 | |||
421 | public function unitsIsAllMovable($dbOwnerRow) { |
||
431 | |||
432 | |||
433 | |||
434 | // TODO - DEBUG - REMOVE ============================================================================================= |
||
435 | public function _dump() { |
||
542 | |||
543 | } |
||
544 |
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.