Complex classes like Calculator 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 Calculator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Calculator |
||
| 20 | { |
||
| 21 | private const AVAILABLE_OPTIONS = ['atk', 'def', 'hp']; |
||
| 22 | |||
| 23 | private $attack_range; |
||
| 24 | private $defense_range; |
||
| 25 | private $stamina_range; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Collection |
||
| 29 | */ |
||
| 30 | private $potentialLevels; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Collection |
||
| 34 | */ |
||
| 35 | private $potentialStamina; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Collection |
||
| 39 | */ |
||
| 40 | private $potentialCombinaisons; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Calculator constructor. |
||
| 44 | */ |
||
| 45 | public function __construct() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Process all operations to retrieve differents IV combinaisons |
||
| 58 | * @param string $pokemonName |
||
| 59 | * @param int $cp |
||
| 60 | * @param int $hp |
||
| 61 | * @param int $dusts |
||
| 62 | * @param int $global |
||
| 63 | * @param int $maxStat |
||
| 64 | * @param array $bestStats |
||
| 65 | * @param bool $upgraded |
||
| 66 | * @return Pokemon |
||
| 67 | */ |
||
| 68 | public function calculate( |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Retrieve the possible level and stamina IV to match HP |
||
| 101 | * @param Pokemon $pokemon |
||
| 102 | * @param int $hp |
||
| 103 | * @param bool $upgraded |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | private function findPotentialStamina( |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Test remaining combinaisons which match CP |
||
| 132 | * @param Pokemon $pokemon |
||
| 133 | * @param int $cp |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | private function findPotentialCombinaisons(Pokemon $pokemon, int $cp) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Remove impossible combinaisons with coach indications |
||
| 167 | * @param array $bestStats |
||
| 168 | * @param int $global |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | private function cleanImpossibleCombinaisons(array $bestStats, int $global) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Remove unavailable options give by user |
||
| 196 | * @param $bestStats |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | private function cleanBestStats($bestStats): array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Remove impossible values for differents IV with coach indications |
||
| 206 | * @param array $bestStats |
||
| 207 | * @param int $maxStat |
||
| 208 | */ |
||
| 209 | private function setRanges(array $bestStats, int $maxStat) |
||
| 215 | |||
| 216 | private function setRange($bestStats, $maxStat, $option, &$property) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Calculate if a combinaison of value match the given CP |
||
| 227 | * @param Pokemon $pokemon |
||
| 228 | * @param Level $level |
||
| 229 | * @param int $cp |
||
| 230 | * @param int $attackIV |
||
| 231 | * @param int $defenseIV |
||
| 232 | * @param int $staminaIV |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | private function testCP( |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Calculate if a combinaison of value match the given HP |
||
| 253 | * @param Pokemon $pokemon |
||
| 254 | * @param Level $level |
||
| 255 | * @param int $hp |
||
| 256 | * @param int $staminaIV |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | private function testHP( |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return the range of stats given by the coach |
||
| 270 | * @param int $maxStat |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | private function getMaxRange(int $maxStat) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Return the range of stats non cited by the coach |
||
| 290 | * @param int $maxStat |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | private function getLowerRange(int $maxStat) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the threshold of the global evaluation given by the coach |
||
| 310 | * @param $global |
||
| 311 | * @return int |
||
| 312 | */ |
||
| 313 | private function getMaxGlobalEvaluation($global) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get attack_range |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function getAttackRange(): array |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get defense_range |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function getDefenseRange(): array |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get stamina_range |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public function getStaminaRange(): array |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get potentialLevels |
||
| 362 | * |
||
| 363 | * @return Collection |
||
| 364 | */ |
||
| 365 | public function getPotentialLevels(): Collection |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get potentialStamina |
||
| 372 | * |
||
| 373 | * @return Collection |
||
| 374 | */ |
||
| 375 | public function getPotentialStamina(): Collection |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get potentialCombinaisons |
||
| 382 | * |
||
| 383 | * @return Collection |
||
| 384 | */ |
||
| 385 | public function getPotentialCombinaisons(): Collection |
||
| 389 | } |
||
| 390 |