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 Word |
||
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 | 87 | public function getId() |
|
112 | { |
||
113 | 87 | 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 | 66 | public function addPlayerToGame(PlayerOptions $playerOptions) |
|
277 | |||
278 | /** |
||
279 | * A player leaves the game |
||
280 | * |
||
281 | * @param PlayerId $playerId |
||
282 | * |
||
283 | * @return GameResult |
||
284 | */ |
||
285 | 19 | 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) |
|
494 | |||
495 | /** |
||
496 | * Function to call when game is lost by a player |
||
497 | * |
||
498 | * @param HangmanPlayer $player |
||
499 | * |
||
500 | * @return hangmanLost | HangmanGameLostEvent |
||
501 | */ |
||
502 | 15 | private function playerLoses(HangmanPlayer $player) |
|
523 | |||
524 | /** |
||
525 | * Sets the next player |
||
526 | * |
||
527 | * @param PlayerId $id |
||
528 | */ |
||
529 | 48 | private function setNextPlayer(PlayerId $id = null) |
|
539 | |||
540 | /** |
||
541 | * Returns the next player in line |
||
542 | * |
||
543 | * @return PlayerId |
||
544 | */ |
||
545 | 18 | private function getNextPlayerId() |
|
565 | |||
566 | /** |
||
567 | * Build the word from played letters |
||
568 | * |
||
569 | * @param string[] $playedLetters |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | 42 | public function buildWord($playedLetters) |
|
577 | |||
578 | /** |
||
579 | * Checks if all letters for the word have been found |
||
580 | * |
||
581 | * @param HangmanPlayer $player |
||
582 | * |
||
583 | * @return bool |
||
584 | */ |
||
585 | 6 | private function isAllLettersFoundForPlayer(HangmanPlayer $player) |
|
591 | |||
592 | /** |
||
593 | * Checks if the answer is valid |
||
594 | * If it's not, ends player turn and throws an HangmanException |
||
595 | * |
||
596 | * @param string $answer |
||
597 | * |
||
598 | * @throws HangmanException |
||
599 | */ |
||
600 | 12 | private function checkAnswerIsValid($answer) |
|
606 | |||
607 | /** |
||
608 | * @return bool |
||
609 | */ |
||
610 | 15 | private function thereIsAnInGamePlayer() |
|
622 | |||
623 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
624 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
625 | //////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
||
626 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
627 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
628 | |||
629 | /** |
||
630 | * Apply the game created event |
||
631 | * |
||
632 | * @param HangmanGameCreatedEvent $event |
||
633 | * |
||
634 | * @return void |
||
635 | */ |
||
636 | 111 | protected function applyHangmanGameCreatedEvent(HangmanGameCreatedEvent $event) |
|
644 | |||
645 | /** |
||
646 | * Apply the player created event |
||
647 | * |
||
648 | * @param HangmanPlayerCreatedEvent $event |
||
649 | * |
||
650 | * @return void |
||
651 | */ |
||
652 | 63 | protected function applyHangmanPlayerCreatedEvent(HangmanPlayerCreatedEvent $event) |
|
665 | |||
666 | /** |
||
667 | * Apply the game created event |
||
668 | */ |
||
669 | 48 | protected function applyHangmanGameStartedEvent() |
|
673 | |||
674 | /** |
||
675 | * Apply the player turn event |
||
676 | * |
||
677 | * @param HangmanPlayerTurnEvent $event |
||
678 | */ |
||
679 | 48 | protected function applyHangmanPlayerTurnEvent(HangmanPlayerTurnEvent $event) |
|
683 | |||
684 | /** |
||
685 | * Apply the hangman player lost event |
||
686 | * |
||
687 | * @param HangmanPlayerLostEvent $event |
||
688 | */ |
||
689 | 21 | protected function applyHangmanPlayerLostEvent(HangmanPlayerLostEvent $event) |
|
693 | |||
694 | /** |
||
695 | * Apply the hangman player win event |
||
696 | * |
||
697 | * @return void |
||
698 | */ |
||
699 | 6 | protected function applyHangmanPlayerWinEvent() |
|
704 | |||
705 | /** |
||
706 | * Apply the hangman lost by all event |
||
707 | * |
||
708 | * @return void |
||
709 | */ |
||
710 | 3 | protected function applyHangmanGameLostEvent() |
|
715 | |||
716 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
717 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
718 | //////////////////////////////////////////// EVENT SOURCED ///////////////////////////////////////////////// |
||
719 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
720 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
721 | |||
722 | /** |
||
723 | * @return Player[] |
||
724 | */ |
||
725 | 111 | protected function getChildEntities() |
|
729 | |||
730 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
731 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
732 | ///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
||
733 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
734 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
735 | |||
736 | /** |
||
737 | * Create a new instance |
||
738 | * |
||
739 | * @param MiniGameId $id |
||
740 | * @param string $word |
||
741 | * |
||
742 | * @return Hangman |
||
743 | */ |
||
744 | 111 | public static function createGame(MiniGameId $id, $word) |
|
751 | |||
752 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
753 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
754 | /////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
||
755 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
756 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
757 | |||
758 | /** |
||
759 | * Static construction method for reconstitution |
||
760 | * |
||
761 | * @return Hangman |
||
762 | */ |
||
763 | 3 | public static function instantiateForReconstitution() |
|
767 | |||
768 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
769 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
770 | ///////////////////////////////////////// APPLY RESTRICTIONS /////////////////////////////////////////////// |
||
771 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
772 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
773 | |||
774 | /** |
||
775 | * @param mixed $event |
||
776 | */ |
||
777 | 111 | public function apply($event) |
|
785 | |||
786 | /** |
||
787 | * @param mixed $event |
||
788 | * |
||
789 | * @return bool |
||
790 | */ |
||
791 | 111 | private function isSupportedEvent($event) |
|
798 | } |
||
799 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.