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 | public function calculate( |
||
84 | |||
85 | /** |
||
86 | * @param Pokemon $pokemon |
||
87 | * @param int $hp |
||
88 | * |
||
89 | * @param bool $upgraded |
||
90 | * @return $this |
||
91 | */ |
||
92 | private function findPotentialStamina( |
||
115 | |||
116 | /** |
||
117 | * @param Pokemon $pokemon |
||
118 | * @param int $cp |
||
119 | * @return $this |
||
120 | */ |
||
121 | private function findPotentialCombinaisons(Pokemon $pokemon, int $cp) |
||
149 | |||
150 | private function cleanImpossibleCombinaisons(array $bestStats, int $global) |
||
172 | |||
173 | private function setRanges(array $bestStats, int $maxStat) |
||
179 | |||
180 | private function setRange($bestStats, $maxStat, $option, &$property) |
||
188 | |||
189 | /** |
||
190 | * @param Pokemon $pokemon |
||
191 | * @param Level $level |
||
192 | * @param int $cp |
||
193 | * @param int $attackIV |
||
194 | * @param int $defenseIV |
||
195 | * @param int $staminaIV |
||
196 | * @return bool |
||
197 | */ |
||
198 | private function testCP( |
||
213 | |||
214 | /** |
||
215 | * @param Pokemon $pokemon |
||
216 | * @param Level $level |
||
217 | * @param int $hp |
||
218 | * @param int $staminaIV |
||
219 | * @return bool |
||
220 | */ |
||
221 | private function testHP( |
||
229 | |||
230 | /** |
||
231 | * Return the range of stats given by the coach |
||
232 | * @param int $maxStat |
||
233 | * @return array |
||
234 | */ |
||
235 | private function getMaxRange(int $maxStat) |
||
249 | |||
250 | /** |
||
251 | * Return the range of stats non cited by the coach |
||
252 | * @param int $maxStat |
||
253 | * @return array |
||
254 | */ |
||
255 | private function getLowerRange(int $maxStat) |
||
269 | |||
270 | /** |
||
271 | * Get the threshold of the global evaluation given by the coach |
||
272 | * @param $global |
||
273 | * @return int |
||
274 | */ |
||
275 | private function getMaxGlobalEvaluation($global) |
||
291 | |||
292 | /** |
||
293 | * Get attack_range |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | public function getAttackRange(): array |
||
301 | |||
302 | /** |
||
303 | * Get defense_range |
||
304 | * |
||
305 | * @return array |
||
306 | */ |
||
307 | public function getDefenseRange(): array |
||
311 | |||
312 | /** |
||
313 | * Get stamina_range |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | public function getStaminaRange(): array |
||
321 | |||
322 | /** |
||
323 | * Get potentialLevels |
||
324 | * |
||
325 | * @return Collection |
||
326 | */ |
||
327 | public function getPotentialLevels(): Collection |
||
331 | |||
332 | /** |
||
333 | * Get potentialStamina |
||
334 | * |
||
335 | * @return Collection |
||
336 | */ |
||
337 | public function getPotentialStamina(): Collection |
||
341 | |||
342 | /** |
||
343 | * Get potentialCombinaisons |
||
344 | * |
||
345 | * @return Collection |
||
346 | */ |
||
347 | public function getPotentialCombinaisons(): Collection |
||
351 | } |
||
352 |