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) |
||
204 | |||
205 | /** |
||
206 | * Remove unavailable options give by user |
||
207 | * @param $bestStats |
||
208 | * @return array |
||
209 | */ |
||
210 | private function cleanBestStats($bestStats): array |
||
214 | |||
215 | /** |
||
216 | * Remove impossible values for differents IV with coach indications |
||
217 | * @param array $bestStats |
||
218 | * @param int $maxStat |
||
219 | */ |
||
220 | private function setRanges(array $bestStats, int $maxStat) |
||
226 | |||
227 | private function setRange($bestStats, $maxStat, $option, &$property) |
||
235 | |||
236 | /** |
||
237 | * Calculate if a combinaison of value match the given CP |
||
238 | * @param Pokemon $pokemon |
||
239 | * @param Level $level |
||
240 | * @param int $cp |
||
241 | * @param int $attackIV |
||
242 | * @param int $defenseIV |
||
243 | * @param int $staminaIV |
||
244 | * @return bool |
||
245 | */ |
||
246 | private function testCP( |
||
261 | |||
262 | /** |
||
263 | * Calculate if a combinaison of value match the given HP |
||
264 | * @param Pokemon $pokemon |
||
265 | * @param Level $level |
||
266 | * @param int $hp |
||
267 | * @param int $staminaIV |
||
268 | * @return bool |
||
269 | */ |
||
270 | private function testHP( |
||
278 | |||
279 | /** |
||
280 | * Return the range of stats given by the coach |
||
281 | * @param int $maxStat |
||
282 | * @return array |
||
283 | */ |
||
284 | private function getMaxRange(int $maxStat) |
||
298 | |||
299 | /** |
||
300 | * Return the range of stats non cited by the coach |
||
301 | * @param int $maxStat |
||
302 | * @return array |
||
303 | */ |
||
304 | private function getLowerRange(int $maxStat) |
||
318 | |||
319 | /** |
||
320 | * Get the threshold of the global evaluation given by the coach |
||
321 | * @param $global |
||
322 | * @return int |
||
323 | */ |
||
324 | private function getMaxGlobalEvaluation($global) |
||
341 | |||
342 | /** |
||
343 | * Get attack_range |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | public function getAttackRange(): array |
||
351 | |||
352 | /** |
||
353 | * Get defense_range |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | public function getDefenseRange(): array |
||
361 | |||
362 | /** |
||
363 | * Get stamina_range |
||
364 | * |
||
365 | * @return array |
||
366 | */ |
||
367 | public function getStaminaRange(): array |
||
371 | |||
372 | /** |
||
373 | * Get potentialLevels |
||
374 | * |
||
375 | * @return Collection |
||
376 | */ |
||
377 | public function getPotentialLevels(): Collection |
||
381 | |||
382 | /** |
||
383 | * Get potentialStamina |
||
384 | * |
||
385 | * @return Collection |
||
386 | */ |
||
387 | public function getPotentialStamina(): Collection |
||
391 | |||
392 | /** |
||
393 | * Get potentialCombinaisons |
||
394 | * |
||
395 | * @return Collection |
||
396 | */ |
||
397 | public function getPotentialCombinaisons(): Collection |
||
401 | } |
||
402 |