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 |
||
35 | class Hangman extends EventSourcedAggregateRoot implements MiniGame |
||
36 | { |
||
37 | use PlayTrait; |
||
38 | |||
39 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
40 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
41 | ////////////////////////////////////////////// CONSTANTS /////////////////////////////////////////////////// |
||
42 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
43 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
44 | |||
45 | const STATE_UNINITIALIZED = 'uninitialized'; |
||
46 | const STATE_READY = 'ready'; |
||
47 | const STATE_STARTED = 'started'; |
||
48 | const STATE_OVER = 'over'; |
||
49 | |||
50 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
51 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
52 | ///////////////////////////////////////////// PROPERTIES /////////////////////////////////////////////////// |
||
53 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
54 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
55 | |||
56 | /** |
||
57 | * @var MiniGameId |
||
58 | */ |
||
59 | private $id; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $word; |
||
65 | |||
66 | /** |
||
67 | * @var HangmanPlayer[] |
||
68 | **/ |
||
69 | private $players; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $gameOrder; |
||
75 | |||
76 | /** |
||
77 | * @var HangmanPlayer |
||
78 | **/ |
||
79 | private $currentPlayer; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | private $state; |
||
85 | |||
86 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
87 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
88 | ///////////////////////////////////////// PRIVATE CONSTRUCTOR ////////////////////////////////////////////// |
||
89 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
90 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
91 | |||
92 | /** |
||
93 | * Constructor |
||
94 | */ |
||
95 | private function __construct() |
||
96 | { |
||
97 | 93 | $this->state = self::STATE_UNINITIALIZED; |
|
98 | } |
||
99 | 93 | ||
100 | 93 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
101 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
102 | ////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
||
103 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
104 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
105 | |||
106 | /** |
||
107 | * Returns the id of the game |
||
108 | * |
||
109 | * @return MiniGameId |
||
110 | */ |
||
111 | public function getId() |
||
112 | { |
||
113 | 78 | return $this->id; |
|
114 | } |
||
115 | 78 | ||
116 | /** |
||
117 | * Returns the aggregate id |
||
118 | * |
||
119 | * @return MiniGameId |
||
120 | */ |
||
121 | public function getAggregateRootId() |
||
122 | { |
||
123 | 93 | return $this->id; |
|
124 | } |
||
125 | 93 | ||
126 | /** |
||
127 | * Returns the name of the mini-game |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public static function getName() |
||
132 | { |
||
133 | 3 | return 'HANGMAN'; |
|
134 | } |
||
135 | 3 | ||
136 | /** |
||
137 | * Get the player identified by PlayerId |
||
138 | * |
||
139 | * @param PlayerId $playerId |
||
140 | * |
||
141 | * @return HangmanPlayer |
||
142 | */ |
||
143 | public function getPlayer(PlayerId $playerId = null) |
||
144 | 57 | { |
|
145 | if ($playerId === null) { |
||
146 | 57 | return null; |
|
147 | 3 | } |
|
148 | |||
149 | return isset($this->players[(string)$playerId]) ? $this->players[(string)$playerId] : null; |
||
150 | 54 | } |
|
151 | |||
152 | /** |
||
153 | * Returns the player who can play |
||
154 | * |
||
155 | * @return Player |
||
156 | */ |
||
157 | public function getCurrentPlayer() |
||
158 | 3 | { |
|
159 | return $this->currentPlayer; |
||
160 | 3 | } |
|
161 | |||
162 | /** |
||
163 | * Get the players |
||
164 | * |
||
165 | * @return Player[] |
||
166 | */ |
||
167 | public function getPlayers() |
||
168 | 93 | { |
|
169 | return $this->players; |
||
170 | 93 | } |
|
171 | |||
172 | /** |
||
173 | * Is game started? |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function isGameStarted() |
||
178 | 36 | { |
|
179 | return $this->state === self::STATE_STARTED; |
||
180 | 36 | } |
|
181 | |||
182 | /** |
||
183 | * Is it the player's turn? |
||
184 | * |
||
185 | * @param PlayerId $playerId |
||
186 | * |
||
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 | * |
||
242 | * @throws HangmanPlayerOptionsException |
||
243 | * @throws HangmanException |
||
244 | * |
||
245 | 75 | * @return GameResult |
|
246 | */ |
||
247 | 75 | public function addPlayerToGame(PlayerOptions $playerOptions) |
|
277 | |||
278 | /** |
||
279 | * A player leaves the game |
||
280 | * |
||
281 | * @param PlayerId $playerId |
||
282 | 9 | * |
|
283 | * @return GameResult |
||
284 | 9 | */ |
|
285 | 9 | public function leaveGame(PlayerId $playerId) |
|
301 | |||
302 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
303 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
304 | ////////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////////// |
||
305 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
306 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
307 | |||
308 | /** |
||
309 | * Initialize the game |
||
310 | * |
||
311 | 93 | * @param MiniGameId $id |
|
312 | * @param string $word |
||
313 | 93 | */ |
|
314 | 93 | private function initialize(MiniGameId $id, $word) |
|
318 | |||
319 | /** |
||
320 | * Player proposes a letter |
||
321 | * |
||
322 | * @param PlayerId $playerId |
||
323 | 21 | * @param Proposition $move |
|
324 | * |
||
325 | 21 | * @return GameResult |
|
326 | 6 | */ |
|
327 | 6 | private function playProposition(PlayerId $playerId, Proposition $move) |
|
336 | |||
337 | /** |
||
338 | * Player tries an answer |
||
339 | * |
||
340 | 15 | * @param PlayerId $playerId |
|
341 | * @param Answer $move |
||
342 | 15 | * |
|
343 | 3 | * @return GameResult |
|
344 | 3 | */ |
|
345 | private function playAnswer(PlayerId $playerId, Answer $move) |
||
364 | |||
365 | /** |
||
366 | 36 | * Returns an error event if player cannot play |
|
367 | * |
||
368 | 36 | * @param PlayerId $playerId |
|
369 | 3 | * |
|
370 | 3 | * @return GameResult |
|
371 | */ |
||
372 | 2 | private function ensurePlayerCanPlay(PlayerId $playerId) |
|
392 | |||
393 | 15 | /** |
|
394 | * Propose a letter |
||
395 | 15 | * |
|
396 | 15 | * @param string $letter |
|
397 | * |
||
398 | 15 | * @return HangmanBadProposition | HangmanGoodProposition |
|
399 | 13 | */ |
|
400 | 15 | private function currentPlayerProposeLetter($letter) |
|
411 | 12 | ||
412 | /** |
||
413 | 12 | * Propose an answer |
|
414 | * |
||
415 | 9 | * @param string $answer |
|
416 | 3 | * |
|
417 | * @return HangmanLost | HangmanWon |
||
418 | 6 | */ |
|
419 | private function currentPlayerProposeAnswer($answer) |
||
429 | 9 | ||
430 | /** |
||
431 | 9 | * Function to call when a bad proposition has been made |
|
432 | 9 | * |
|
433 | 9 | * @param string $letter |
|
434 | * |
||
435 | 9 | * @return HangmanBadProposition | HangmanLost |
|
436 | 9 | */ |
|
437 | 9 | private function currentPlayerBadProposition($letter) |
|
451 | 9 | ||
452 | /** |
||
453 | 9 | * Function to call after a good proposition of letter has been made |
|
454 | 6 | * |
|
455 | * @param string $letter |
||
456 | * |
||
457 | 3 | * @return HangmanGoodProposition | HangmanWon |
|
458 | */ |
||
459 | 3 | private function currentPlayerGoodProposition($letter) |
|
473 | 6 | ||
474 | /** |
||
475 | 6 | * Function to call when game is won by a player |
|
476 | 6 | * |
|
477 | 6 | * @param HangmanPlayer $player |
|
478 | 6 | * |
|
479 | 6 | * @return HangmanWon |
|
480 | */ |
||
481 | 6 | private function playerWins(HangmanPlayer $player) |
|
506 | 6 | ||
507 | /** |
||
508 | 6 | * Function to call when game is lost by a player |
|
509 | * |
||
510 | 6 | * @param HangmanPlayer $player |
|
511 | 6 | * |
|
512 | * @return hangmanLost | HangmanGameLostEvent |
||
513 | 6 | */ |
|
514 | 6 | private function playerLoses(HangmanPlayer $player) |
|
537 | |||
538 | 21 | /** |
|
539 | * Sets the next player |
||
540 | 21 | * |
|
541 | * @param PlayerId $id |
||
542 | 21 | */ |
|
543 | 21 | private function setNextPlayer(PlayerId $id = null) |
|
553 | 21 | ||
554 | /** |
||
555 | 21 | * Returns the next player in line |
|
556 | * |
||
557 | * @return PlayerId |
||
558 | */ |
||
559 | private function getNextPlayerId() |
||
583 | 3 | ||
584 | /** |
||
585 | 3 | * Returns the list of played letters |
|
586 | * |
||
587 | * @param PlayerId $playerId |
||
588 | * |
||
589 | * @return array |
||
590 | */ |
||
591 | private function getPlayedLettersForPlayer(PlayerId $playerId) |
||
595 | 48 | ||
596 | /** |
||
597 | * Gets the remaining lives for the player |
||
598 | * |
||
599 | 48 | * @param PlayerId $playerId |
|
600 | 48 | * |
|
601 | 32 | * @return int |
|
602 | 48 | */ |
|
603 | private function getRemainingLives(PlayerId $playerId) |
||
607 | |||
608 | /** |
||
609 | 24 | * Returns the indexes of the letter in the word |
|
610 | * |
||
611 | 24 | * @param string $letter |
|
612 | * |
||
613 | * @return boolean |
||
614 | */ |
||
615 | 24 | private function wordContains($letter) |
|
619 | 24 | ||
620 | /** |
||
621 | 24 | * Get the letters of the word |
|
622 | 24 | * |
|
623 | * @return string[] |
||
624 | 24 | */ |
|
625 | 24 | private function getLettersFromWord() |
|
629 | |||
630 | /** |
||
631 | * Build the word from played letters |
||
632 | * |
||
633 | * @param string[] $playedLetters |
||
634 | * |
||
635 | * @return string |
||
636 | */ |
||
637 | public function buildWord($playedLetters) |
||
651 | 27 | ||
652 | /** |
||
653 | 27 | * Checks if all letters for the word have been found |
|
654 | * |
||
655 | * @param HangmanPlayer $player |
||
656 | * |
||
657 | * @return bool |
||
658 | */ |
||
659 | private function isAllLettersFoundForPlayer(HangmanPlayer $player) |
||
665 | |||
666 | /** |
||
667 | * Checks if the answer is valid |
||
668 | * If it's not, ends player turn and throws an HangmanException |
||
669 | * |
||
670 | * @param string $answer |
||
671 | * |
||
672 | 27 | * @throws HangmanException |
|
673 | */ |
||
674 | 27 | private function checkAnswerIsValid($answer) |
|
680 | |||
681 | /** |
||
682 | * Checks if the word is the same as the solution |
||
683 | 27 | * |
|
684 | * @param string $word |
||
685 | 27 | * |
|
686 | * @return bool |
||
687 | 27 | */ |
|
688 | private function isTheAnswer($word) |
||
692 | 27 | ||
693 | 18 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
694 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
695 | 27 | //////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
|
696 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
697 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
698 | |||
699 | /** |
||
700 | * Apply the game created event |
||
701 | * |
||
702 | * @param HangmanGameCreatedEvent $event |
||
703 | * |
||
704 | 6 | * @return void |
|
705 | */ |
||
706 | 6 | protected function applyHangmanGameCreatedEvent(HangmanGameCreatedEvent $event) |
|
714 | |||
715 | /** |
||
716 | * Apply the player created event |
||
717 | * |
||
718 | 12 | * @param HangmanPlayerCreatedEvent $event |
|
719 | * |
||
720 | 12 | * @return void |
|
721 | 3 | */ |
|
722 | protected function applyHangmanPlayerCreatedEvent(HangmanPlayerCreatedEvent $event) |
||
735 | |||
736 | /** |
||
737 | * Apply the game created event |
||
738 | */ |
||
739 | protected function applyHangmanGameStartedEvent() |
||
743 | |||
744 | /** |
||
745 | * Apply the player turn event |
||
746 | * |
||
747 | * @param HangmanPlayerTurnEvent $event |
||
748 | 93 | */ |
|
749 | protected function applyHangmanPlayerTurnEvent(HangmanPlayerTurnEvent $event) |
||
753 | 93 | ||
754 | 93 | /** |
|
755 | 93 | * Apply the hangman player lost event |
|
756 | * |
||
757 | * @param HangmanPlayerLostEvent $event |
||
758 | */ |
||
759 | protected function applyHangmanPlayerLostEvent(HangmanPlayerLostEvent $event) |
||
764 | |||
765 | 75 | /** |
|
766 | 75 | * Apply the hangman player win event |
|
767 | 75 | * |
|
768 | 75 | * @return void |
|
769 | 50 | */ |
|
770 | 75 | protected function applyHangmanPlayerWinEvent() |
|
775 | 75 | ||
776 | /** |
||
777 | * Apply the hangman lost by all event |
||
778 | * |
||
779 | * @return void |
||
780 | 48 | */ |
|
781 | protected function applyHangmanGameLostEvent() |
||
786 | |||
787 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
788 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
789 | //////////////////////////////////////////// EVENT SOURCED ///////////////////////////////////////////////// |
||
790 | 48 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
791 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
792 | 48 | ||
793 | 48 | /** |
|
794 | * @return Player[] |
||
795 | */ |
||
796 | protected function getChildEntities() |
||
800 | 21 | ||
801 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
802 | 21 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
803 | 21 | ///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
|
804 | 21 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
805 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
806 | |||
807 | /** |
||
808 | * Create a new instance |
||
809 | * |
||
810 | * @param MiniGameId $id |
||
811 | 6 | * @param string $word |
|
812 | * |
||
813 | 6 | * @return Hangman |
|
814 | 6 | */ |
|
815 | 6 | public static function createGame(MiniGameId $id, $word) |
|
822 | 3 | ||
823 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
824 | 3 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
825 | 3 | /////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
|
826 | 3 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
827 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
828 | |||
829 | /** |
||
830 | * Static construction method for reconstitution |
||
831 | * |
||
832 | * @return Hangman |
||
833 | */ |
||
834 | public static function instantiateForReconstitution() |
||
838 | |||
839 | 93 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
840 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
841 | ///////////////////////////////////////// APPLY RESTRICTIONS /////////////////////////////////////////////// |
||
842 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
843 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
844 | |||
845 | |||
846 | /** |
||
847 | * @param mixed $event |
||
848 | */ |
||
849 | public function apply($event) |
||
857 | 93 | ||
858 | 93 | /** |
|
859 | * @param mixed $event |
||
860 | 93 | * |
|
861 | * @return bool |
||
862 | */ |
||
863 | private function isSupportedEvent($event) |
||
870 | } |
||
871 |