Complex classes like PlayerRoutine 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 PlayerRoutine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class PlayerRoutine |
||
| 20 | { |
||
| 21 | /** @var array **/ |
||
| 22 | protected $results; |
||
| 23 | |||
| 24 | const COEF_RESOURCE = 0.001; |
||
| 25 | |||
| 26 | public function execute( |
||
| 27 | $players, |
||
| 28 | \PDOStatement $resourcesStatement, |
||
| 29 | \PDOStatement $resourcesDataStatement, |
||
| 30 | \PDOStatement $generalStatement, |
||
| 31 | \PDOStatement $armiesStatement, |
||
| 32 | \PDOStatement $planetStatement, |
||
| 33 | \PDOStatement $tradeRoutesStatement, |
||
| 34 | \PDOStatement $linkedTradeRoutesStatement, |
||
| 35 | \PDOStatement $attackersStatement, |
||
| 36 | \PDOStatement $defendersStatement, |
||
| 37 | OrbitalBaseHelper $orbitalBaseHelper |
||
| 38 | ) |
||
| 39 | { |
||
|
|
|||
| 40 | $this->results = []; |
||
| 41 | # create an array with all the players |
||
| 42 | foreach ($players as $player) { |
||
| 43 | $this->results[$player->id] = [ |
||
| 44 | 'general' => 0, |
||
| 45 | 'resources' => 0, |
||
| 46 | 'experience' => 0, |
||
| 47 | 'victory' => 0, |
||
| 48 | 'defeat' => 0, |
||
| 49 | 'fight' => 0, |
||
| 50 | 'armies' => 0, |
||
| 51 | 'butcher' => 0, |
||
| 52 | 'butcherDestroyedPEV' => 0, |
||
| 53 | 'butcherLostPEV' => 0, |
||
| 54 | 'trader' => 0, |
||
| 55 | |||
| 56 | 'DA_Resources' => 0, |
||
| 57 | 'DA_PlanetNumber' => 0 |
||
| 58 | ]; |
||
| 59 | } |
||
| 60 | $this->calculateResources($resourcesStatement, $orbitalBaseHelper); |
||
| 61 | $this->calculateDataResources($resourcesDataStatement); |
||
| 62 | $this->calculatePlanetRanking($planetStatement); |
||
| 63 | $this->calculateGeneralRanking($generalStatement); |
||
| 64 | $this->calculateArmiesRanking($armiesStatement); |
||
| 65 | $this->calculateTradeRanking($tradeRoutesStatement, $linkedTradeRoutesStatement); |
||
| 66 | $this->calculateButcherRanking($attackersStatement, $defendersStatement); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function processResults(Ranking $ranking, $players, PlayerRankingManager $playerRankingManager, PlayerRankingRepository $playerRankingRepository) |
||
| 70 | { |
||
| 71 | foreach ($players as $playerId) { |
||
| 72 | if (isset($this->results[$playerId->id])) { |
||
| 73 | # add the points to the list |
||
| 74 | $this->results[$playerId->id]['experience'] += $playerId->experience; |
||
| 75 | $this->results[$playerId->id]['victory'] += $playerId->victory; |
||
| 76 | $this->results[$playerId->id]['defeat'] += $playerId->defeat; |
||
| 77 | $this->results[$playerId->id]['fight'] += $playerId->victory - $playerId->defeat; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | # copy the arrays |
||
| 82 | $listG = $this->results; |
||
| 83 | $listR = $this->results; |
||
| 84 | $listE = $this->results; |
||
| 85 | $listF = $this->results; |
||
| 86 | $listA = $this->results; |
||
| 87 | $listB = $this->results; |
||
| 88 | $listT = $this->results; |
||
| 89 | |||
| 90 | # sort all the copies |
||
| 91 | uasort($listG, [$this, 'cmpGeneral']); |
||
| 92 | uasort($listR, [$this, 'cmpResources']); |
||
| 93 | uasort($listE, [$this, 'cmpExperience']); |
||
| 94 | uasort($listF, [$this, 'cmpFight']); |
||
| 95 | uasort($listA, [$this, 'cmpArmies']); |
||
| 96 | uasort($listB, [$this, 'cmpButcher']); |
||
| 97 | uasort($listT, [$this, 'cmpTrader']); |
||
| 98 | |||
| 99 | /*foreach ($list as $key => $value) { |
||
| 100 | echo $key . ' => ' . $value['general'] . '<br/>'; |
||
| 101 | }*/ |
||
| 102 | |||
| 103 | # put the position in each array |
||
| 104 | $position = 1; |
||
| 105 | foreach ($listG as $key => $value) { $listG[$key]['position'] = $position++;} |
||
| 106 | $position = 1; |
||
| 107 | foreach ($listR as $key => $value) { $listR[$key]['position'] = $position++;} |
||
| 108 | $position = 1; |
||
| 109 | foreach ($listE as $key => $value) { $listE[$key]['position'] = $position++;} |
||
| 110 | $position = 1; |
||
| 111 | foreach ($listF as $key => $value) { $listF[$key]['position'] = $position++;} |
||
| 112 | $position = 1; |
||
| 113 | foreach ($listA as $key => $value) { $listA[$key]['position'] = $position++;} |
||
| 114 | $position = 1; |
||
| 115 | foreach ($listB as $key => $value) { $listB[$key]['position'] = $position++;} |
||
| 116 | $position = 1; |
||
| 117 | foreach ($listT as $key => $value) { $listT[$key]['position'] = $position++;} |
||
| 118 | |||
| 119 | foreach ($players as $player) { |
||
| 120 | $playerId = $player->getId(); |
||
| 121 | |||
| 122 | $pr = new PlayerRanking(); |
||
| 123 | $pr->rRanking = $ranking->getId(); |
||
| 124 | $pr->rPlayer = $playerId; |
||
| 125 | |||
| 126 | # voir s'il faut améliorer (p.ex. : stocker le tableau des objets et supprimer chaque objet utilisé pour que la liste se rapetisse) |
||
| 127 | $firstRanking = true; |
||
| 128 | for ($i = 0; $i < $playerRankingManager->size(); $i++) { |
||
| 129 | if ($playerRankingManager->get($i)->rPlayer == $playerId) { |
||
| 130 | $firstRanking = false; |
||
| 131 | $oldRanking = $playerRankingManager->get($i); |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | $pr->general = $listG[$playerId]['general']; |
||
| 137 | $pr->generalPosition = $listG[$playerId]['position']; |
||
| 138 | $pr->generalVariation = $firstRanking ? 0 : $oldRanking->generalPosition - $pr->generalPosition; |
||
| 139 | $player->factionPoint = $pr->general; |
||
| 140 | |||
| 141 | $pr->resources = $listR[$playerId]['resources']; |
||
| 142 | $pr->resourcesPosition = $listR[$playerId]['position']; |
||
| 143 | $pr->resourcesVariation = $firstRanking ? 0 : $oldRanking->resourcesPosition - $pr->resourcesPosition; |
||
| 144 | |||
| 145 | $pr->experience = $listE[$playerId]['experience']; |
||
| 146 | $pr->experiencePosition = $listE[$playerId]['position']; |
||
| 147 | $pr->experienceVariation = $firstRanking ? 0 : $oldRanking->experiencePosition - $pr->experiencePosition; |
||
| 148 | |||
| 149 | $pr->fight = ($listF[$playerId]['fight'] >= 0) ? $listF[$playerId]['fight'] : 0; |
||
| 150 | $pr->victories = $listF[$playerId]['victory']; |
||
| 151 | $pr->defeat = $listF[$playerId]['defeat']; |
||
| 152 | $pr->fightPosition = $listF[$playerId]['position']; |
||
| 153 | $pr->fightVariation = $firstRanking ? 0 : $oldRanking->fightPosition - $pr->fightPosition; |
||
| 154 | |||
| 155 | $pr->armies = $listA[$playerId]['armies']; |
||
| 156 | $pr->armiesPosition = $listA[$playerId]['position']; |
||
| 157 | $pr->armiesVariation = $firstRanking ? 0 : $oldRanking->armiesPosition - $pr->armiesPosition; |
||
| 158 | |||
| 159 | $pr->butcher = ($listB[$playerId]['butcher'] >= 0) ? $listB[$playerId]['butcher'] : 0; |
||
| 160 | $pr->butcherDestroyedPEV = $listB[$playerId]['butcherDestroyedPEV']; |
||
| 161 | $pr->butcherLostPEV = $listB[$playerId]['butcherLostPEV']; |
||
| 162 | $pr->butcherPosition = $listB[$playerId]['position']; |
||
| 163 | $pr->butcherVariation = $firstRanking ? 0 : $oldRanking->butcherPosition - $pr->butcherPosition; |
||
| 164 | |||
| 165 | $pr->trader = $listT[$playerId]['trader']; |
||
| 166 | $pr->traderPosition = $listT[$playerId]['position']; |
||
| 167 | $pr->traderVariation = $firstRanking ? 0 : $oldRanking->traderPosition - $pr->traderPosition; |
||
| 168 | |||
| 169 | $playerRankingManager->add($pr); |
||
| 170 | |||
| 171 | if (DATA_ANALYSIS) { |
||
| 172 | $playerRankingRepository->insertDataAnalysis( |
||
| 173 | $player, |
||
| 174 | $pr, |
||
| 175 | $this->results[$player]['DA_Resources'], |
||
| 176 | $this->results[$player]['DA_PlanetNumber'] |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | protected function calculateResources(\PDOStatement $statement, OrbitalBaseHelper $orbitalBaseHelper) |
||
| 183 | { |
||
| 184 | while ($row = $statement->fetch()) { |
||
| 185 | if (isset($this->results[$row['player']])) { |
||
| 186 | $resourcesProd = Game::resourceProduction($orbitalBaseHelper->getBuildingInfo(OrbitalBaseResource::REFINERY, 'level', $row['levelRefinery'], 'refiningCoefficient'), $row['coefResources']); |
||
| 187 | $this->results[$row['player']]['resources'] += $resourcesProd; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | $statement->closeCursor(); |
||
| 191 | } |
||
| 192 | |||
| 193 | protected function calculateDataResources(\PDOStatement $statement) |
||
| 194 | { |
||
| 195 | while ($row = $statement->fetch()) { |
||
| 196 | if (isset($this->results[$row['player']])) { |
||
| 197 | $this->results[$row['player']]['DA_Resources'] += DataAnalysis::resourceToStdUnit($row['sumResources']); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | $statement->closeCursor(); |
||
| 201 | } |
||
| 202 | |||
| 203 | protected function calculatePlanetRanking(\PDOStatement $statement) |
||
| 204 | { |
||
| 205 | while ($row = $statement->fetch()) { |
||
| 206 | if (isset($this->results[$row['player']])) { |
||
| 207 | $this->results[$row['player']]['DA_PlanetNumber'] += $row['sumPlanets']; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | $statement->closeCursor(); |
||
| 211 | } |
||
| 212 | |||
| 213 | protected function calculateGeneralRanking(\PDOStatement $statement) |
||
| 214 | { |
||
| 215 | while ($row = $statement->fetch()) { |
||
| 216 | if (isset($this->results[$row['player']])) { |
||
| 217 | $shipPrice = 0; |
||
| 218 | $shipPrice += ShipResource::getInfo(0, 'resourcePrice') * $row['s0']; |
||
| 219 | $shipPrice += ShipResource::getInfo(1, 'resourcePrice') * $row['s1']; |
||
| 220 | $shipPrice += ShipResource::getInfo(2, 'resourcePrice') * $row['s2']; |
||
| 221 | $shipPrice += ShipResource::getInfo(3, 'resourcePrice') * $row['s3']; |
||
| 222 | $shipPrice += ShipResource::getInfo(4, 'resourcePrice') * $row['s4']; |
||
| 223 | $shipPrice += ShipResource::getInfo(5, 'resourcePrice') * $row['s5']; |
||
| 224 | $shipPrice += ShipResource::getInfo(6, 'resourcePrice') * $row['s6']; |
||
| 225 | $shipPrice += ShipResource::getInfo(7, 'resourcePrice') * $row['s7']; |
||
| 226 | $shipPrice += ShipResource::getInfo(8, 'resourcePrice') * $row['s8']; |
||
| 227 | $shipPrice += ShipResource::getInfo(9, 'resourcePrice') * $row['s9']; |
||
| 228 | $shipPrice += ShipResource::getInfo(10, 'resourcePrice') * $row['s10']; |
||
| 229 | $shipPrice += ShipResource::getInfo(11, 'resourcePrice') * $row['s11']; |
||
| 230 | $points = round($shipPrice * self::COEF_RESOURCE); |
||
| 231 | $points += $row['points']; |
||
| 232 | $points += round($row['resources'] * self::COEF_RESOURCE); |
||
| 233 | $this->results[$row['player']]['general'] += $points; |
||
| 234 | |||
| 235 | $pevQuantity = 0; |
||
| 236 | $pevQuantity += ShipResource::getInfo(0, 'pev') * $row['s0']; |
||
| 237 | $pevQuantity += ShipResource::getInfo(1, 'pev') * $row['s1']; |
||
| 238 | $pevQuantity += ShipResource::getInfo(2, 'pev') * $row['s2']; |
||
| 239 | $pevQuantity += ShipResource::getInfo(3, 'pev') * $row['s3']; |
||
| 240 | $pevQuantity += ShipResource::getInfo(4, 'pev') * $row['s4']; |
||
| 241 | $pevQuantity += ShipResource::getInfo(5, 'pev') * $row['s5']; |
||
| 242 | $pevQuantity += ShipResource::getInfo(6, 'pev') * $row['s6']; |
||
| 243 | $pevQuantity += ShipResource::getInfo(7, 'pev') * $row['s7']; |
||
| 244 | $pevQuantity += ShipResource::getInfo(8, 'pev') * $row['s8']; |
||
| 245 | $pevQuantity += ShipResource::getInfo(9, 'pev') * $row['s9']; |
||
| 246 | $pevQuantity += ShipResource::getInfo(10, 'pev') * $row['s10']; |
||
| 247 | $pevQuantity += ShipResource::getInfo(11, 'pev') * $row['s11']; |
||
| 248 | $this->results[$row['player']]['armies'] += $pevQuantity; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | $statement->closeCursor(); |
||
| 252 | } |
||
| 253 | |||
| 254 | protected function calculateArmiesRanking(\PDOStatement $statement) |
||
| 255 | { |
||
| 256 | while ($row = $statement->fetch()) { |
||
| 257 | if (isset($this->results[$row['player']])) { |
||
| 258 | $shipPrice = 0; |
||
| 259 | $shipPrice += ShipResource::getInfo(0, 'resourcePrice') * $row['s0']; |
||
| 260 | $shipPrice += ShipResource::getInfo(1, 'resourcePrice') * $row['s1']; |
||
| 261 | $shipPrice += ShipResource::getInfo(2, 'resourcePrice') * $row['s2']; |
||
| 262 | $shipPrice += ShipResource::getInfo(3, 'resourcePrice') * $row['s3']; |
||
| 263 | $shipPrice += ShipResource::getInfo(4, 'resourcePrice') * $row['s4']; |
||
| 264 | $shipPrice += ShipResource::getInfo(5, 'resourcePrice') * $row['s5']; |
||
| 265 | $shipPrice += ShipResource::getInfo(6, 'resourcePrice') * $row['s6']; |
||
| 266 | $shipPrice += ShipResource::getInfo(7, 'resourcePrice') * $row['s7']; |
||
| 267 | $shipPrice += ShipResource::getInfo(8, 'resourcePrice') * $row['s8']; |
||
| 268 | $shipPrice += ShipResource::getInfo(9, 'resourcePrice') * $row['s9']; |
||
| 269 | $shipPrice += ShipResource::getInfo(10, 'resourcePrice') * $row['s10']; |
||
| 270 | $shipPrice += ShipResource::getInfo(11, 'resourcePrice') * $row['s11']; |
||
| 271 | $points = round($shipPrice * self::COEF_RESOURCE); |
||
| 272 | $this->results[$row['player']]['general'] += $points; |
||
| 273 | |||
| 274 | $pevQuantity = 0; |
||
| 275 | $pevQuantity += ShipResource::getInfo(0, 'pev') * $row['s0']; |
||
| 276 | $pevQuantity += ShipResource::getInfo(1, 'pev') * $row['s1']; |
||
| 277 | $pevQuantity += ShipResource::getInfo(2, 'pev') * $row['s2']; |
||
| 278 | $pevQuantity += ShipResource::getInfo(3, 'pev') * $row['s3']; |
||
| 279 | $pevQuantity += ShipResource::getInfo(4, 'pev') * $row['s4']; |
||
| 280 | $pevQuantity += ShipResource::getInfo(5, 'pev') * $row['s5']; |
||
| 281 | $pevQuantity += ShipResource::getInfo(6, 'pev') * $row['s6']; |
||
| 282 | $pevQuantity += ShipResource::getInfo(7, 'pev') * $row['s7']; |
||
| 283 | $pevQuantity += ShipResource::getInfo(8, 'pev') * $row['s8']; |
||
| 284 | $pevQuantity += ShipResource::getInfo(9, 'pev') * $row['s9']; |
||
| 285 | $pevQuantity += ShipResource::getInfo(10, 'pev') * $row['s10']; |
||
| 286 | $pevQuantity += ShipResource::getInfo(11, 'pev') * $row['s11']; |
||
| 287 | $this->results[$row['player']]['armies'] += $pevQuantity; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | $statement->closeCursor(); |
||
| 291 | } |
||
| 292 | |||
| 293 | protected function calculateTradeRanking(\PDOStatement $routesStatement, \PDOStatement $linkedRoutesStatement) |
||
| 308 | |||
| 309 | protected function calculateButcherRanking(\PDOStatement $attackerStatement, \PDOStatement $defenderStatement) |
||
| 328 | |||
| 329 | protected function cmpGeneral($a, $b) |
||
| 336 | |||
| 337 | protected function cmpResources($a, $b) { |
||
| 343 | |||
| 344 | protected function cmpExperience($a, $b) { |
||
| 350 | |||
| 351 | protected function cmpFight($a, $b) { |
||
| 357 | |||
| 358 | protected function cmpArmies($a, $b) { |
||
| 364 | |||
| 365 | protected function cmpButcher($a, $b) { |
||
| 371 | |||
| 372 | protected function cmpTrader($a, $b) { |
||
| 378 | } |