Complex classes like ArmyAttack 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 ArmyAttack, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Xaoc303\BattleCalc; |
||
| 7 | class ArmyAttack |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $unitsAtt; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $unitsDef; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * run |
||
| 21 | * |
||
| 22 | * @param Army $armyAtt |
||
| 23 | * @param Army $armyDef |
||
| 24 | * @param int $Inic |
||
| 25 | */ |
||
| 26 | public static function run(Army &$armyAtt, Army &$armyDef, $Inic) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * attack |
||
| 41 | * |
||
| 42 | * @param int $Inic |
||
| 43 | */ |
||
| 44 | private function attack($Inic) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * attackAirOn |
||
| 65 | * |
||
| 66 | * @param int $i |
||
| 67 | */ |
||
| 68 | private function attackAirOn(&$i) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * attackTerOn |
||
| 75 | * |
||
| 76 | * @param int $i |
||
| 77 | */ |
||
| 78 | private function attackTerOn(&$i) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * attackMagicOn |
||
| 85 | * |
||
| 86 | * @param int $i |
||
| 87 | */ |
||
| 88 | private function attackMagicOn(&$i) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * unitMagicRound |
||
| 102 | * |
||
| 103 | * @param integer $id |
||
| 104 | * @param integer $round |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | private function unitMagicRound($id, $round) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * attackMagic |
||
| 134 | * |
||
| 135 | * @param integer $i |
||
| 136 | * @param string $k |
||
| 137 | */ |
||
| 138 | private function attackMagic($i, $k) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * attackMagicStorm |
||
| 204 | * |
||
| 205 | * @param int $i |
||
| 206 | */ |
||
| 207 | private function attackMagicStorm($i) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * attackMagicLockP |
||
| 214 | * |
||
| 215 | * @param int $i |
||
| 216 | */ |
||
| 217 | private function attackMagicLockP($i) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * attackMagicPhantom |
||
| 241 | * |
||
| 242 | * @param int $i |
||
| 243 | */ |
||
| 244 | private function attackMagicPhantom($i) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * attackMagicScarab |
||
| 255 | * |
||
| 256 | * @param int $i |
||
| 257 | */ |
||
| 258 | private function attackMagicScarab($i) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * attackMagicRemont |
||
| 265 | * |
||
| 266 | * @param int $i |
||
| 267 | */ |
||
| 268 | private function attackMagicRemont($i) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * attackMagicMedicine |
||
| 275 | * |
||
| 276 | * @param int $i |
||
| 277 | */ |
||
| 278 | private function attackMagicMedicine($i) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * attackMagicBlind |
||
| 285 | * |
||
| 286 | * @param int $i |
||
| 287 | */ |
||
| 288 | private function attackMagicBlind($i) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * attackMagicSteam |
||
| 314 | * |
||
| 315 | * @param int $i |
||
| 316 | */ |
||
| 317 | private function attackMagicSteam($i) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * attackMagicYamato |
||
| 328 | * |
||
| 329 | * @param int $i |
||
| 330 | */ |
||
| 331 | private function attackMagicYamato($i) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * attackMagicNuclear |
||
| 338 | * |
||
| 339 | * @param int $i |
||
| 340 | */ |
||
| 341 | private function attackMagicNuclear($i) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * attackMagicEMI |
||
| 348 | * |
||
| 349 | * @param int $i |
||
| 350 | */ |
||
| 351 | private function attackMagicEMI($i) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * attackMagicRadiation |
||
| 358 | * |
||
| 359 | * @param int $i |
||
| 360 | */ |
||
| 361 | private function attackMagicRadiation($i) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * attackMagicMines |
||
| 368 | * |
||
| 369 | * @param int $i |
||
| 370 | */ |
||
| 371 | private function attackMagicMines($i) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * attackMagicPlague |
||
| 378 | * |
||
| 379 | * @param int $i |
||
| 380 | */ |
||
| 381 | private function attackMagicPlague($i) |
||
| 385 | } |
||
| 386 |