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 | 111 | private function __construct() |
|
96 | { |
||
97 | 111 | $this->state = self::STATE_UNINITIALIZED; |
|
98 | 111 | } |
|
99 | |||
100 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
101 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
102 | ////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
||
103 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
104 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
105 | |||
106 | /** |
||
107 | * Returns the id of the game |
||
108 | * |
||
109 | * @return MiniGameId |
||
110 | */ |
||
111 | 93 | public function getId() |
|
112 | { |
||
113 | 93 | return $this->id; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Returns the aggregate id |
||
118 | * |
||
119 | * @return MiniGameId |
||
120 | */ |
||
121 | 111 | public function getAggregateRootId() |
|
122 | { |
||
123 | 111 | return $this->id; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns the name of the mini-game |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 3 | public static function getName() |
|
132 | { |
||
133 | 3 | return 'HANGMAN'; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Get the player identified by PlayerId |
||
138 | * |
||
139 | * @param PlayerId $playerId |
||
140 | * |
||
141 | * @return HangmanPlayer |
||
142 | */ |
||
143 | 57 | public function getPlayer(PlayerId $playerId = null) |
|
144 | { |
||
145 | 57 | if ($playerId === null) { |
|
146 | 3 | return null; |
|
147 | } |
||
148 | |||
149 | 54 | return isset($this->players[(string)$playerId]) ? $this->players[(string)$playerId] : null; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Returns the player who can play |
||
154 | * |
||
155 | * @return Player |
||
156 | */ |
||
157 | 3 | public function getCurrentPlayer() |
|
158 | { |
||
159 | 3 | return $this->currentPlayer; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get the players |
||
164 | * |
||
165 | * @return Player[] |
||
166 | */ |
||
167 | 111 | public function getPlayers() |
|
168 | { |
||
169 | 111 | return array_values($this->players); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Is game started? |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | 36 | public function isGameStarted() |
|
178 | { |
||
179 | 36 | return $this->state === self::STATE_STARTED; |
|
180 | } |
||
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 | * @return GameResult |
||
246 | */ |
||
247 | 75 | public function addPlayerToGame(PlayerOptions $playerOptions) |
|
277 | |||
278 | /** |
||
279 | * A player leaves the game |
||
280 | * |
||
281 | * @param PlayerId $playerId |
||
282 | * |
||
283 | * @return GameResult |
||
284 | */ |
||
285 | 9 | public function leaveGame(PlayerId $playerId) |
|
301 | |||
302 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
303 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
304 | ////////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////////// |
||
305 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
306 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
307 | |||
308 | /** |
||
309 | * Initialize the game |
||
310 | * |
||
311 | * @param MiniGameId $id |
||
312 | * @param string $word |
||
313 | */ |
||
314 | 111 | private function initialize(MiniGameId $id, $word) |
|
318 | |||
319 | /** |
||
320 | * Player proposes a letter |
||
321 | * |
||
322 | * @param PlayerId $playerId |
||
323 | * @param Proposition $move |
||
324 | * |
||
325 | * @return GameResult |
||
326 | */ |
||
327 | 21 | private function playProposition(PlayerId $playerId, Proposition $move) |
|
336 | |||
337 | /** |
||
338 | * Player tries an answer |
||
339 | * |
||
340 | * @param PlayerId $playerId |
||
341 | * @param Answer $move |
||
342 | * |
||
343 | * @return GameResult |
||
344 | */ |
||
345 | 15 | private function playAnswer(PlayerId $playerId, Answer $move) |
|
364 | |||
365 | /** |
||
366 | * Returns an error event if player cannot play |
||
367 | * |
||
368 | * @param PlayerId $playerId |
||
369 | * |
||
370 | * @return GameResult |
||
371 | */ |
||
372 | 36 | private function ensurePlayerCanPlay(PlayerId $playerId) |
|
392 | |||
393 | /** |
||
394 | * Propose a letter |
||
395 | * |
||
396 | * @param string $letter |
||
397 | * |
||
398 | * @return HangmanBadProposition | HangmanGoodProposition |
||
399 | */ |
||
400 | 15 | private function currentPlayerProposeLetter($letter) |
|
411 | |||
412 | /** |
||
413 | * Propose an answer |
||
414 | * |
||
415 | * @param string $answer |
||
416 | * |
||
417 | * @return HangmanLost | HangmanWon |
||
418 | */ |
||
419 | 12 | private function currentPlayerProposeAnswer($answer) |
|
429 | |||
430 | /** |
||
431 | * Function to call when a bad proposition has been made |
||
432 | * |
||
433 | * @param string $letter |
||
434 | * |
||
435 | * @return HangmanBadProposition | HangmanLost |
||
436 | */ |
||
437 | 9 | private function currentPlayerBadProposition($letter) |
|
451 | |||
452 | /** |
||
453 | * Function to call after a good proposition of letter has been made |
||
454 | * |
||
455 | * @param string $letter |
||
456 | * |
||
457 | * @return HangmanGoodProposition | HangmanWon |
||
458 | */ |
||
459 | 6 | private function currentPlayerGoodProposition($letter) |
|
473 | |||
474 | /** |
||
475 | * Function to call when game is won by a player |
||
476 | * |
||
477 | * @param HangmanPlayer $player |
||
478 | * |
||
479 | * @return HangmanWon |
||
480 | */ |
||
481 | 6 | private function playerWins(HangmanPlayer $player) |
|
506 | |||
507 | /** |
||
508 | * Function to call when game is lost by a player |
||
509 | * |
||
510 | * @param HangmanPlayer $player |
||
511 | * |
||
512 | * @return hangmanLost | HangmanGameLostEvent |
||
513 | */ |
||
514 | 15 | private function playerLoses(HangmanPlayer $player) |
|
535 | |||
536 | /** |
||
537 | * Sets the next player |
||
538 | * |
||
539 | * @param PlayerId $id |
||
540 | */ |
||
541 | 48 | private function setNextPlayer(PlayerId $id = null) |
|
551 | |||
552 | /** |
||
553 | * Returns the next player in line |
||
554 | * |
||
555 | * @return PlayerId |
||
556 | */ |
||
557 | 18 | private function getNextPlayerId() |
|
577 | |||
578 | /** |
||
579 | * Returns the list of played letters |
||
580 | * |
||
581 | * @param PlayerId $playerId |
||
582 | * |
||
583 | * @return array |
||
584 | */ |
||
585 | 6 | private function getPlayedLettersForPlayer(PlayerId $playerId) |
|
589 | |||
590 | /** |
||
591 | * Gets the remaining lives for the player |
||
592 | * |
||
593 | * @param PlayerId $playerId |
||
594 | * |
||
595 | * @return int |
||
596 | */ |
||
597 | 6 | private function getRemainingLives(PlayerId $playerId) |
|
601 | |||
602 | /** |
||
603 | * Returns the indexes of the letter in the word |
||
604 | * |
||
605 | * @param string $letter |
||
606 | * |
||
607 | * @return boolean |
||
608 | */ |
||
609 | 15 | private function wordContains($letter) |
|
613 | |||
614 | /** |
||
615 | * Get the letters of the word |
||
616 | * |
||
617 | * @return string[] |
||
618 | */ |
||
619 | 42 | private function getLettersFromWord() |
|
623 | |||
624 | /** |
||
625 | * Build the word from played letters |
||
626 | * |
||
627 | * @param string[] $playedLetters |
||
628 | * |
||
629 | * @return string |
||
630 | */ |
||
631 | 42 | public function buildWord($playedLetters) |
|
645 | |||
646 | /** |
||
647 | * Checks if all letters for the word have been found |
||
648 | * |
||
649 | * @param HangmanPlayer $player |
||
650 | * |
||
651 | * @return bool |
||
652 | */ |
||
653 | 6 | private function isAllLettersFoundForPlayer(HangmanPlayer $player) |
|
659 | |||
660 | /** |
||
661 | * Checks if the answer is valid |
||
662 | * If it's not, ends player turn and throws an HangmanException |
||
663 | * |
||
664 | * @param string $answer |
||
665 | * |
||
666 | * @throws HangmanException |
||
667 | */ |
||
668 | 12 | private function checkAnswerIsValid($answer) |
|
674 | |||
675 | /** |
||
676 | * Checks if the word is the same as the solution |
||
677 | * |
||
678 | * @param string $word |
||
679 | * |
||
680 | * @return bool |
||
681 | */ |
||
682 | 9 | private function isTheAnswer($word) |
|
686 | |||
687 | /** |
||
688 | * @return bool |
||
689 | */ |
||
690 | 15 | private function thereIsAnInGamePlayer() |
|
702 | |||
703 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
704 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
705 | //////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
||
706 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
707 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
708 | |||
709 | /** |
||
710 | * Apply the game created event |
||
711 | * |
||
712 | * @param HangmanGameCreatedEvent $event |
||
713 | * |
||
714 | * @return void |
||
715 | */ |
||
716 | 111 | protected function applyHangmanGameCreatedEvent(HangmanGameCreatedEvent $event) |
|
724 | |||
725 | /** |
||
726 | * Apply the player created event |
||
727 | * |
||
728 | * @param HangmanPlayerCreatedEvent $event |
||
729 | * |
||
730 | * @return void |
||
731 | */ |
||
732 | 75 | protected function applyHangmanPlayerCreatedEvent(HangmanPlayerCreatedEvent $event) |
|
745 | |||
746 | /** |
||
747 | * Apply the game created event |
||
748 | */ |
||
749 | 48 | protected function applyHangmanGameStartedEvent() |
|
753 | |||
754 | /** |
||
755 | * Apply the player turn event |
||
756 | * |
||
757 | * @param HangmanPlayerTurnEvent $event |
||
758 | */ |
||
759 | 48 | protected function applyHangmanPlayerTurnEvent(HangmanPlayerTurnEvent $event) |
|
763 | |||
764 | /** |
||
765 | * Apply the hangman player lost event |
||
766 | * |
||
767 | * @param HangmanPlayerLostEvent $event |
||
768 | */ |
||
769 | 21 | protected function applyHangmanPlayerLostEvent(HangmanPlayerLostEvent $event) |
|
773 | |||
774 | /** |
||
775 | * Apply the hangman player win event |
||
776 | * |
||
777 | * @return void |
||
778 | */ |
||
779 | 6 | protected function applyHangmanPlayerWinEvent() |
|
784 | |||
785 | /** |
||
786 | * Apply the hangman lost by all event |
||
787 | * |
||
788 | * @return void |
||
789 | */ |
||
790 | 3 | protected function applyHangmanGameLostEvent() |
|
795 | |||
796 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
797 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
798 | //////////////////////////////////////////// EVENT SOURCED ///////////////////////////////////////////////// |
||
799 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
800 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
801 | |||
802 | /** |
||
803 | * @return Player[] |
||
804 | */ |
||
805 | 111 | protected function getChildEntities() |
|
809 | |||
810 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
811 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
812 | ///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
||
813 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
814 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
815 | |||
816 | /** |
||
817 | * Create a new instance |
||
818 | * |
||
819 | * @param MiniGameId $id |
||
820 | * @param string $word |
||
821 | * |
||
822 | * @return Hangman |
||
823 | */ |
||
824 | 111 | public static function createGame(MiniGameId $id, $word) |
|
831 | |||
832 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
833 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
834 | /////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
||
835 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
836 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
837 | |||
838 | /** |
||
839 | * Static construction method for reconstitution |
||
840 | * |
||
841 | * @return Hangman |
||
842 | */ |
||
843 | 3 | public static function instantiateForReconstitution() |
|
847 | |||
848 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
849 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
850 | ///////////////////////////////////////// APPLY RESTRICTIONS /////////////////////////////////////////////// |
||
851 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
852 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
853 | |||
854 | /** |
||
855 | * @param mixed $event |
||
856 | */ |
||
857 | 111 | public function apply($event) |
|
865 | |||
866 | /** |
||
867 | * @param mixed $event |
||
868 | * |
||
869 | * @return bool |
||
870 | */ |
||
871 | 111 | private function isSupportedEvent($event) |
|
878 | } |
||
879 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.