Complex classes like OrbitalBaseHelper 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 OrbitalBaseHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class OrbitalBaseHelper { |
||
10 | /** @var TechnologyHelper **/ |
||
11 | protected $technologyHelper; |
||
12 | |||
13 | /** |
||
14 | * @param TechnologyHelper $technologyHelper |
||
15 | */ |
||
16 | public function __construct(TechnologyHelper $technologyHelper) |
||
20 | |||
21 | public function isABuilding($building) { |
||
24 | |||
25 | public function isAShipFromDock1($ship) { |
||
28 | |||
29 | public function isAShipFromDock2($ship) { |
||
32 | |||
33 | public function isAShipFromDock3($ship) { |
||
36 | |||
37 | public function fleetQuantity($typeOfBase) { |
||
51 | |||
52 | public function getInfo($buildingNumber, $info, $level = 0, $sup = 'default') { |
||
55 | |||
56 | public function getBuildingInfo($buildingNumber, $info, $level = 0, $sup = 'default') { |
||
57 | if($this->isABuilding($buildingNumber)) { |
||
58 | if (in_array($info, ['name', 'column', 'frenchName', 'imageLink', 'description'])) { |
||
59 | return OrbitalBaseResource::$building[$buildingNumber][$info]; |
||
60 | } elseif ($info == 'techno') { |
||
61 | if (in_array($buildingNumber, array(3,4,6,8,9))) { |
||
62 | return OrbitalBaseResource::$building[$buildingNumber][$info]; |
||
63 | } else { |
||
64 | return -1; |
||
65 | } |
||
66 | } elseif ($info == 'maxLevel') { |
||
67 | # $level is the type of the base |
||
68 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level]; |
||
69 | |||
70 | } elseif ($info == 'level') { |
||
71 | if ($level <= 0 OR $level > count(OrbitalBaseResource::$building[$buildingNumber]['level'])) { |
||
72 | return FALSE; |
||
73 | } |
||
74 | if ($sup == 'time') { |
||
75 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][0]; |
||
76 | } elseif($sup == 'resourcePrice') { |
||
77 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][1]; |
||
78 | } elseif($sup == 'points') { |
||
79 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][2]; |
||
80 | } else { |
||
81 | if ($sup == 'nbQueues') { |
||
82 | if ($buildingNumber == 0 OR $buildingNumber == 2 OR $buildingNumber == 3 OR $buildingNumber == 5) { |
||
83 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][3]; |
||
84 | } |
||
85 | } elseif ($sup == 'storageSpace') { |
||
86 | if ($buildingNumber == 7) { |
||
87 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][3]; |
||
88 | } elseif ($buildingNumber == 2 OR $buildingNumber == 3) { |
||
89 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][4]; |
||
90 | } |
||
91 | } elseif ($sup == 'refiningCoefficient' AND $buildingNumber == 1) { |
||
92 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][3]; |
||
93 | } elseif ($sup == 'releasedShip' AND ($buildingNumber == 2 OR $buildingNumber == 3)) { |
||
94 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][5]; |
||
95 | } elseif ($sup == 'releasedShip' AND $buildingNumber == 4) { |
||
96 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][4]; |
||
97 | } elseif ($sup == 'nbCommercialShip' AND $buildingNumber == 6) { |
||
98 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][3]; |
||
99 | } elseif ($sup == 'nbRecyclers' AND $buildingNumber == 8) { |
||
100 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][3]; |
||
101 | } elseif ($sup == 'nbRoutesMax' AND $buildingNumber == 9) { |
||
102 | return OrbitalBaseResource::$building[$buildingNumber][$info][$level-1][3]; |
||
103 | } else { |
||
104 | throw new ErrorException('4e argument invalide dans getBuildingInfo de OrbitalBaseResource'); |
||
105 | } |
||
106 | } |
||
107 | } else { |
||
108 | throw new ErrorException('2e argument invalide dans getBuildingInfo de OrbitalBaseResource'); |
||
109 | } |
||
110 | } else { |
||
111 | throw new ErrorException('1er argument invalide (entre 0 et 7) dans getBuildingInfo de OrbitalBaseResource'); |
||
112 | } |
||
113 | return FALSE; |
||
114 | } |
||
115 | |||
116 | public function haveRights($buildingId, $level, $type, $sup) { |
||
205 | } |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.