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) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Remove unavailable options give by user |
||
| 198 | * @param $bestStats |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | private function cleanBestStats($bestStats): array |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Remove impossible values for differents IV with coach indications |
||
| 208 | * @param array $bestStats |
||
| 209 | * @param int $maxStat |
||
| 210 | */ |
||
| 211 | private function setRanges(array $bestStats, int $maxStat) |
||
| 217 | |||
| 218 | private function setRange($bestStats, $maxStat, $option, &$property) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Calculate if a combinaison of value match the given CP |
||
| 229 | * @param Pokemon $pokemon |
||
| 230 | * @param Level $level |
||
| 231 | * @param int $cp |
||
| 232 | * @param int $attackIV |
||
| 233 | * @param int $defenseIV |
||
| 234 | * @param int $staminaIV |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | private function testCP( |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Calculate if a combinaison of value match the given HP |
||
| 255 | * @param Pokemon $pokemon |
||
| 256 | * @param Level $level |
||
| 257 | * @param int $hp |
||
| 258 | * @param int $staminaIV |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | private function testHP( |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return the range of stats given by the coach |
||
| 272 | * @param int $maxStat |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | private function getMaxRange(int $maxStat) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Return the range of stats non cited by the coach |
||
| 292 | * @param int $maxStat |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | private function getLowerRange(int $maxStat) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get the threshold of the global evaluation given by the coach |
||
| 312 | * @param $global |
||
| 313 | * @return int |
||
| 314 | */ |
||
| 315 | private function getMaxGlobalEvaluation($global) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get attack_range |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function getAttackRange(): array |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get defense_range |
||
| 345 | * |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | public function getDefenseRange(): array |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get stamina_range |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function getStaminaRange(): array |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get potentialLevels |
||
| 365 | * |
||
| 366 | * @return Collection |
||
| 367 | */ |
||
| 368 | public function getPotentialLevels(): Collection |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get potentialStamina |
||
| 375 | * |
||
| 376 | * @return Collection |
||
| 377 | */ |
||
| 378 | public function getPotentialStamina(): Collection |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get potentialCombinaisons |
||
| 385 | * |
||
| 386 | * @return Collection |
||
| 387 | */ |
||
| 388 | public function getPotentialCombinaisons(): Collection |
||
| 392 | } |
||
| 393 |