Complex classes like FleetValidator 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 FleetValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FleetValidator { |
||
| 7 | /** |
||
| 8 | * @var Fleet $fleet |
||
| 9 | */ |
||
| 10 | protected $fleet; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * FleetValidator constructor. |
||
| 14 | * |
||
| 15 | * @param Fleet $fleet |
||
| 16 | */ |
||
| 17 | public function __construct($fleet) { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * |
||
| 23 | */ |
||
| 24 | public function validate() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param array $checklist |
||
| 61 | * |
||
| 62 | * @throws Exception |
||
| 63 | */ |
||
| 64 | public function checkMissionRestrictions($checklist) { |
||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @throws Exception |
||
| 91 | */ |
||
| 92 | protected function restrict2ToAllowedMissions() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @throws Exception |
||
| 100 | */ |
||
| 101 | protected function restrict2ToAllowedPlanetTypes() { |
||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | protected function checkSpeedPercentOld() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | protected function checkSenderNoVacation() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | protected function checkTargetNoVacation() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | protected function checkMultiAccount() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | protected function checkTargetNotSource() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | protected function checkTargetInUniverse() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | protected function checkUnitsPositive() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | protected function checkOnlyFleetUnits() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | protected function checkOnlyFlyingUnits() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | protected function checkEnoughFleetSlots() { |
||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | protected function checkEnoughCapacity($includeResources = true) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | protected function checkNotTooFar() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | protected function checkDebrisExists() { |
||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | // Resources checks |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | protected function checkResourcesPositive() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | protected function checkCargo() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | protected function checkSourceEnoughFuel() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | protected function checkSourceEnoughResources() { |
||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | |||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | // Target vector checks (????????) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | protected function checkKnownSpace() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | protected function checkTargetExists() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | protected function checkTargetIsPlanet() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | protected function checkTargetIsDebris() { |
||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | // Ships checks |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | protected function checkFleetNotEmpty() { |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | protected function checkSourceEnoughShips() { |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | protected function checkHaveColonizer() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | protected function checkHaveRecyclers() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | protected function checkSpiesOnly() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | protected function checkNotOnlySpies() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | protected function checkNoMissiles() { |
||
| 396 | |||
| 397 | |||
| 398 | |||
| 399 | |||
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | |||
| 404 | |||
| 405 | |||
| 406 | |||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | protected function checkTargetOwn() { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | protected function forceTargetOwn() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | protected function checkTargetOther() { |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | protected function alwaysFalse() { |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | protected function checkTargetAllyDeposit() { |
||
| 466 | |||
| 467 | |||
| 468 | /** |
||
| 469 | * Forces missions that can flight to OWN planets |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | protected function forceMissionsOwn() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Check mission type OR no mission - and limits available missions to this type if positive |
||
| 497 | * |
||
| 498 | * @param int $missionType |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | protected function forceMission($missionType) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | protected function forceMissionExplore() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | protected function forceMissionColonize() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return bool |
||
| 531 | */ |
||
| 532 | protected function forceMissionRecycle() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | protected function forceMissionMissile() { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Just checks mission type |
||
| 545 | * |
||
| 546 | * @param int $missionType |
||
| 547 | * |
||
| 548 | * @return bool |
||
| 549 | */ |
||
| 550 | protected function checkMissionNonRestrict($missionType) { |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * @return bool |
||
| 557 | */ |
||
| 558 | protected function checkNotEmptyMission() { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | protected function checkMissionRelocate() { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | protected function checkMissionHoldNonUnique() { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return bool |
||
| 580 | */ |
||
| 581 | protected function checkMissionTransport() { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @return bool |
||
| 587 | */ |
||
| 588 | protected function checkMissionTransportReal() { |
||
| 594 | |||
| 595 | |||
| 596 | /** |
||
| 597 | * @return bool |
||
| 598 | */ |
||
| 599 | protected function forceMissionSpy() { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @return bool |
||
| 605 | */ |
||
| 606 | protected function checkRealFlight() { |
||
| 609 | |||
| 610 | |||
| 611 | /** |
||
| 612 | * @return bool |
||
| 613 | */ |
||
| 614 | protected function unsetMissionSpyComplex() { |
||
| 625 | |||
| 626 | |||
| 627 | /** |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | protected function checkMissionExists() { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @return bool |
||
| 636 | */ |
||
| 637 | protected function checkPlayerInactiveOrNotNoob() { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @return bool |
||
| 646 | */ |
||
| 647 | protected function checkTargetActive() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @return bool |
||
| 656 | */ |
||
| 657 | // TODO - REDO MAIN FUNCTION |
||
| 658 | protected function checkTargetNotActive() { |
||
| 661 | |||
| 662 | |||
| 663 | /** |
||
| 664 | * @return bool |
||
| 665 | */ |
||
| 666 | protected function checkSameAlly() { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @return bool |
||
| 672 | */ |
||
| 673 | protected function checkTargetNoob() { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @return bool |
||
| 694 | */ |
||
| 695 | // TODO - REDO MAIN FUNCTION |
||
| 696 | protected function checkTargetNotNoob() { |
||
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * @return bool |
||
| 703 | */ |
||
| 704 | protected function checkMissionHoldReal() { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @return bool |
||
| 713 | */ |
||
| 714 | protected function checkMissionHoldOnNotNoob() { |
||
| 722 | |||
| 723 | |||
| 724 | // Missiles |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @return bool |
||
| 728 | */ |
||
| 729 | protected function checkOnlyAttackMissiles() { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @return bool |
||
| 737 | */ |
||
| 738 | protected function checkSiloLevel() { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @return bool |
||
| 746 | */ |
||
| 747 | protected function checkSameGalaxy() { |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @return bool |
||
| 753 | */ |
||
| 754 | protected function checkMissileDistance() { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @return bool |
||
| 760 | */ |
||
| 761 | protected function checkMissileTarget() { |
||
| 764 | |||
| 765 | |||
| 766 | /** |
||
| 767 | * @return int |
||
| 768 | */ |
||
| 769 | protected function checkExpeditionsMax() { |
||
| 772 | |||
| 773 | /** |
||
| 774 | * @return bool |
||
| 775 | */ |
||
| 776 | protected function checkExpeditionsFree() { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * @return bool |
||
| 782 | */ |
||
| 783 | protected function checkCaptainSent() { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * @return bool |
||
| 789 | */ |
||
| 790 | protected function checkCaptainExists() { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @return bool |
||
| 796 | */ |
||
| 797 | protected function checkCaptainOnPlanet() { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @return bool |
||
| 803 | */ |
||
| 804 | protected function checkCaptainNotRelocating() { |
||
| 813 | |||
| 814 | } |
||
| 815 |
This check looks for
tryblocks that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.If there is nothing in the
trythen thecatchblock can never be executed either. Thus, thesetrystatements can be removed completely.