Complex classes like Hangman 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 Hangman, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Hangman extends EventSourcedAggregateRoot implements MiniGame |
||
| 38 | { |
||
| 39 | use PlayTrait; |
||
| 40 | |||
| 41 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 42 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 43 | ////////////////////////////////////////////// CONSTANTS /////////////////////////////////////////////////// |
||
| 44 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 45 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 46 | |||
| 47 | const STATE_UNINITIALIZED = 'uninitialized'; |
||
| 48 | const STATE_READY = 'ready'; |
||
| 49 | const STATE_STARTED = 'started'; |
||
| 50 | const STATE_OVER = 'over'; |
||
| 51 | |||
| 52 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 53 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 54 | ///////////////////////////////////////////// PROPERTIES /////////////////////////////////////////////////// |
||
| 55 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 56 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var MiniGameId |
||
| 60 | */ |
||
| 61 | private $id; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $word; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var HangmanPlayer[] |
||
| 70 | **/ |
||
| 71 | private $players; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $gameOrder; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var HangmanPlayer |
||
| 80 | **/ |
||
| 81 | private $currentPlayer; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $state; |
||
| 87 | |||
| 88 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 89 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 90 | ///////////////////////////////////////// PRIVATE CONSTRUCTOR ////////////////////////////////////////////// |
||
| 91 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 92 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Constructor |
||
| 96 | */ |
||
| 97 | 93 | private function __construct() |
|
| 101 | |||
| 102 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 103 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 104 | ////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
||
| 105 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 106 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the id of the game |
||
| 110 | * |
||
| 111 | * @return MiniGameId |
||
| 112 | */ |
||
| 113 | 78 | public function getId() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Returns the aggregate id |
||
| 120 | * |
||
| 121 | * @return MiniGameId |
||
| 122 | */ |
||
| 123 | 93 | public function getAggregateRootId() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Returns the name of the mini-game |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | 3 | public static function getName() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Get the player identified by PlayerId |
||
| 140 | * |
||
| 141 | * @param PlayerId $playerId |
||
| 142 | * @return HangmanPlayer |
||
| 143 | */ |
||
| 144 | 57 | public function getPlayer(PlayerId $playerId = null) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the player who can play |
||
| 155 | * |
||
| 156 | * @return Player |
||
| 157 | */ |
||
| 158 | 3 | public function getCurrentPlayer() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get the players |
||
| 165 | * |
||
| 166 | * @return Player[] |
||
| 167 | */ |
||
| 168 | 93 | public function getPlayers() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Is game started? |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 36 | public function isGameStarted() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Is it the player's turn? |
||
| 185 | * |
||
| 186 | * @param PlayerId $playerId |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | 36 | public function canPlayerPlay(PlayerId $playerId) |
|
| 193 | |||
| 194 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 195 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 196 | /////////////////////////////////////////// DOMAIN METHODS ///////////////////////////////////////////////// |
||
| 197 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 198 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Starts the game |
||
| 202 | * |
||
| 203 | * @param PlayerId $playerId |
||
| 204 | * |
||
| 205 | * @return GameResult |
||
| 206 | */ |
||
| 207 | 51 | public function startGame(PlayerId $playerId) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Adds a player to the game |
||
| 239 | * |
||
| 240 | * @param PlayerOptions $playerOptions |
||
| 241 | * @throws HangmanPlayerOptionsException |
||
| 242 | * @throws HangmanException |
||
| 243 | * @return GameResult |
||
| 244 | */ |
||
| 245 | 75 | public function addPlayerToGame(PlayerOptions $playerOptions) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * A player leaves the game |
||
| 278 | * |
||
| 279 | * @param PlayerId $playerId |
||
| 280 | * @return GameResult |
||
| 281 | */ |
||
| 282 | 9 | public function leaveGame(PlayerId $playerId) |
|
| 298 | |||
| 299 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 300 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 301 | ////////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////////// |
||
| 302 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 303 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Initialize the game |
||
| 307 | * |
||
| 308 | * @param MiniGameId $id |
||
| 309 | * @param string $word |
||
| 310 | */ |
||
| 311 | 93 | private function initialize(MiniGameId $id, $word) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Player proposes a letter |
||
| 318 | * |
||
| 319 | * @param PlayerId $playerId |
||
| 320 | * @param Proposition $move |
||
| 321 | * @return GameResult |
||
| 322 | */ |
||
| 323 | 21 | private function playProposition(PlayerId $playerId, Proposition $move) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Player tries an answer |
||
| 335 | * |
||
| 336 | * @param PlayerId $playerId |
||
| 337 | * @param Answer $move |
||
| 338 | * @return GameResult |
||
| 339 | */ |
||
| 340 | 15 | private function playAnswer(PlayerId $playerId, Answer $move) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Returns an error event if player cannot play |
||
| 362 | * |
||
| 363 | * @param PlayerId $playerId |
||
| 364 | * @return GameResult |
||
| 365 | */ |
||
| 366 | 36 | private function ensurePlayerCanPlay(PlayerId $playerId) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Propose a letter |
||
| 389 | * |
||
| 390 | * @param string $letter |
||
| 391 | * @return HangmanBadProposition|HangmanGoodProposition |
||
| 392 | */ |
||
| 393 | 15 | private function currentPlayerProposeLetter($letter) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Propose an answer |
||
| 407 | * |
||
| 408 | * @param string $answer |
||
| 409 | * @return HangmanLost|HangmanWon |
||
| 410 | */ |
||
| 411 | 12 | private function currentPlayerProposeAnswer($answer) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Function to call when a bad proposition has been made |
||
| 424 | * |
||
| 425 | * @param string $letter |
||
| 426 | * |
||
| 427 | * @return HangmanBadProposition |
||
| 428 | */ |
||
| 429 | 9 | private function currentPlayerBadProposition($letter) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Function to call after a good proposition of letter has been made |
||
| 464 | * |
||
| 465 | * @param string $letter |
||
| 466 | * |
||
| 467 | * @return HangmanGoodProposition |
||
| 468 | */ |
||
| 469 | 6 | private function currentPlayerGoodProposition($letter) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Function to call when game is won by a player |
||
| 502 | * |
||
| 503 | * @param HangmanPlayer $player |
||
| 504 | * @return HangmanWon |
||
| 505 | */ |
||
| 506 | 6 | private function playerWins(HangmanPlayer $player) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Just make one player lose |
||
| 534 | * |
||
| 535 | * @param HangmanPlayer $player |
||
| 536 | * @return HangmanPlayerLostEvent |
||
| 537 | */ |
||
| 538 | 21 | private function makePlayerLose(HangmanPlayer $player) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Function to call when game is lost by a player |
||
| 560 | * |
||
| 561 | * @param HangmanPlayer $player |
||
| 562 | * @return HangmanLost |
||
| 563 | */ |
||
| 564 | 15 | private function playerLoses(HangmanPlayer $player) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Sets the next player |
||
| 590 | * |
||
| 591 | * @param PlayerId $id |
||
| 592 | */ |
||
| 593 | 48 | private function setNextPlayer(PlayerId $id = null) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Returns the next player in line |
||
| 606 | * |
||
| 607 | * @return PlayerId |
||
| 608 | */ |
||
| 609 | 24 | private function getNextPlayerId() |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Returns the list of played letters |
||
| 636 | * |
||
| 637 | * @param PlayerId $playerId |
||
| 638 | * @return array |
||
| 639 | */ |
||
| 640 | 27 | private function getPlayedLettersForPlayer(PlayerId $playerId) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Gets the remaining lives for the player |
||
| 647 | * |
||
| 648 | * @param PlayerId $playerId |
||
| 649 | * @return int |
||
| 650 | */ |
||
| 651 | 27 | private function getRemainingLives(PlayerId $playerId) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Returns the indexes of the letter in the word |
||
| 658 | * |
||
| 659 | * @param string $letter |
||
| 660 | * @return boolean |
||
| 661 | */ |
||
| 662 | 15 | private function wordContains($letter) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Get the letters of the word |
||
| 669 | * |
||
| 670 | * @return string[] |
||
| 671 | */ |
||
| 672 | 27 | private function getLettersFromWord() |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Build the word from played letters |
||
| 679 | * |
||
| 680 | * @param string[] $playedLetters |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | 27 | private function buildWord($playedLetters) |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Checks if all letters for the word have been found |
||
| 700 | * |
||
| 701 | * @param HangmanPlayer $player |
||
| 702 | * @return bool |
||
| 703 | */ |
||
| 704 | 6 | private function isAllLettersFoundForPlayer(HangmanPlayer $player) |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Checks if the answer is valid |
||
| 713 | * If it's not, ends player turn and throws an HangmanException |
||
| 714 | * |
||
| 715 | * @param string $answer |
||
| 716 | * @throws HangmanException |
||
| 717 | */ |
||
| 718 | 12 | private function checkAnswerIsValid($answer) |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Checks if the word is the same as the solution |
||
| 727 | * |
||
| 728 | * @param string $word |
||
| 729 | * @return bool |
||
| 730 | */ |
||
| 731 | 9 | private function isTheAnswer($word) |
|
| 735 | |||
| 736 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 737 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 738 | //////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
||
| 739 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 740 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Apply the game created event |
||
| 744 | * |
||
| 745 | * @param HangmanGameCreatedEvent $event |
||
| 746 | * @return void |
||
| 747 | */ |
||
| 748 | 93 | protected function applyHangmanGameCreatedEvent(HangmanGameCreatedEvent $event) |
|
| 756 | |||
| 757 | /** |
||
| 758 | * Apply the player created event |
||
| 759 | * |
||
| 760 | * @param HangmanPlayerCreatedEvent $event |
||
| 761 | * @return void |
||
| 762 | */ |
||
| 763 | 75 | protected function applyHangmanPlayerCreatedEvent(HangmanPlayerCreatedEvent $event) |
|
| 776 | |||
| 777 | /** |
||
| 778 | * Apply the game created event |
||
| 779 | */ |
||
| 780 | 48 | protected function applyHangmanGameStartedEvent() |
|
| 784 | |||
| 785 | /** |
||
| 786 | * Apply the player turn event |
||
| 787 | * |
||
| 788 | * @param HangmanPlayerTurnEvent $event |
||
| 789 | */ |
||
| 790 | 48 | protected function applyHangmanPlayerTurnEvent(HangmanPlayerTurnEvent $event) |
|
| 794 | |||
| 795 | /** |
||
| 796 | * Apply the hangman player lost event |
||
| 797 | * |
||
| 798 | * @param HangmanPlayerLostEvent $event |
||
| 799 | */ |
||
| 800 | 21 | protected function applyHangmanPlayerLostEvent(HangmanPlayerLostEvent $event) |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Apply the hangman player win event |
||
| 808 | * |
||
| 809 | * @return void |
||
| 810 | */ |
||
| 811 | 6 | protected function applyHangmanPlayerWinEvent() |
|
| 816 | |||
| 817 | /** |
||
| 818 | * Apply the hangman lost by all event |
||
| 819 | * |
||
| 820 | * @return void |
||
| 821 | */ |
||
| 822 | 3 | protected function applyHangmanGameLostEvent() |
|
| 827 | |||
| 828 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 829 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 830 | //////////////////////////////////////////// EVENT SOURCED ///////////////////////////////////////////////// |
||
| 831 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 832 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @return Player[] |
||
| 836 | */ |
||
| 837 | 93 | protected function getChildEntities() |
|
| 841 | |||
| 842 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 843 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 844 | ///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
||
| 845 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 846 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Create a new instance |
||
| 850 | * |
||
| 851 | * @param MiniGameId $id |
||
| 852 | * @param string $word |
||
| 853 | * @return Hangman |
||
| 854 | */ |
||
| 855 | 93 | public static function createGame(MiniGameId $id, $word) |
|
| 862 | |||
| 863 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 864 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 865 | /////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
||
| 866 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 867 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Static construction method for reconstitution |
||
| 871 | * |
||
| 872 | * @return Hangman |
||
| 873 | */ |
||
| 874 | 3 | public static function instantiateForReconstitution() |
|
| 878 | |||
| 879 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 880 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 881 | ///////////////////////////////////////// APPLY RESTRICTIONS /////////////////////////////////////////////// |
||
| 882 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 883 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 884 | |||
| 885 | |||
| 886 | /** |
||
| 887 | * @param mixed $event |
||
| 888 | */ |
||
| 889 | 93 | public function handleRecursively($event) |
|
| 897 | |||
| 898 | /** |
||
| 899 | * @param mixed $event |
||
| 900 | * |
||
| 901 | * @return bool |
||
| 902 | */ |
||
| 903 | 93 | private function isSupportedEvent($event) |
|
| 910 | } |
||
| 911 |