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 |
||
36 | class Hangman extends EventSourcedAggregateRoot implements MiniGame |
||
37 | { |
||
38 | use PlayTrait; |
||
39 | |||
40 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
41 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
42 | ////////////////////////////////////////////// CONSTANTS /////////////////////////////////////////////////// |
||
43 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
44 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
45 | |||
46 | const STATE_UNINITIALIZED = 'uninitialized'; |
||
47 | const STATE_READY = 'ready'; |
||
48 | const STATE_STARTED = 'started'; |
||
49 | const STATE_OVER = 'over'; |
||
50 | |||
51 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
52 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
53 | ///////////////////////////////////////////// PROPERTIES /////////////////////////////////////////////////// |
||
54 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
55 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
56 | |||
57 | /** |
||
58 | * @var MiniGameId |
||
59 | */ |
||
60 | private $id; |
||
61 | |||
62 | /** |
||
63 | * @var Word |
||
64 | */ |
||
65 | private $word; |
||
66 | |||
67 | /** |
||
68 | * @var PlayersCollection |
||
69 | **/ |
||
70 | private $players; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $state; |
||
76 | |||
77 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
78 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
79 | ///////////////////////////////////////// PRIVATE CONSTRUCTOR ////////////////////////////////////////////// |
||
80 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
81 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
82 | |||
83 | /** |
||
84 | * Constructor |
||
85 | */ |
||
86 | 114 | private function __construct() |
|
90 | |||
91 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
92 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
93 | ////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
||
94 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
95 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
96 | |||
97 | /** |
||
98 | * Returns the id of the game |
||
99 | * |
||
100 | * @return MiniGameId |
||
101 | */ |
||
102 | 87 | public function getId() |
|
103 | 12 | { |
|
104 | 87 | return $this->id; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the aggregate id |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 114 | public function getAggregateRootId() |
|
113 | { |
||
114 | 114 | return (string) $this->id; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Returns the name of the mini-game |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | 3 | public static function getName() |
|
126 | |||
127 | /** |
||
128 | * Get the player identified by PlayerId |
||
129 | * |
||
130 | * @param PlayerId $playerId |
||
131 | * |
||
132 | * @return HangmanPlayer |
||
133 | */ |
||
134 | 12 | public function getPlayer(PlayerId $playerId = null) |
|
142 | |||
143 | /** |
||
144 | * Returns the player who can play |
||
145 | * |
||
146 | * @return Player |
||
147 | */ |
||
148 | 3 | public function getCurrentPlayer() |
|
152 | |||
153 | /** |
||
154 | * Get the players |
||
155 | * |
||
156 | * @return Player[] |
||
157 | */ |
||
158 | 114 | public function getPlayers() |
|
162 | |||
163 | /** |
||
164 | * Is game started? |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 36 | public function isGameStarted() |
|
172 | |||
173 | /** |
||
174 | * Is it the player's turn? |
||
175 | * |
||
176 | * @param PlayerId $playerId |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 36 | public function canPlayerPlay(PlayerId $playerId) |
|
184 | |||
185 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
186 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
187 | /////////////////////////////////////////// DOMAIN METHODS ///////////////////////////////////////////////// |
||
188 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
189 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
190 | |||
191 | /** |
||
192 | * Starts the game |
||
193 | * |
||
194 | * @param PlayerId $playerId |
||
195 | * |
||
196 | * @return GameResult |
||
197 | */ |
||
198 | 51 | public function startGame(PlayerId $playerId) |
|
215 | |||
216 | /** |
||
217 | * Adds a player to the game |
||
218 | * |
||
219 | * @param PlayerOptions $playerOptions |
||
220 | * |
||
221 | * @throws HangmanPlayerOptionsException |
||
222 | * @throws HangmanException |
||
223 | * |
||
224 | * @return GameResult |
||
225 | */ |
||
226 | 66 | public function addPlayerToGame(PlayerOptions $playerOptions) |
|
256 | |||
257 | /** |
||
258 | * A player leaves the game |
||
259 | * |
||
260 | * @param PlayerId $playerId |
||
261 | * |
||
262 | * @return GameResult |
||
263 | */ |
||
264 | 9 | public function leaveGame(PlayerId $playerId) |
|
265 | { |
||
266 | 9 | switch ($this->state) { |
|
267 | 9 | case self::STATE_STARTED: |
|
268 | 3 | $player = $this->getPlayer($playerId); |
|
269 | 3 | return $player ? $this->playerLoses($player) : null; |
|
270 | 6 | case self::STATE_OVER: |
|
271 | 3 | break; |
|
272 | 2 | default: |
|
273 | 3 | $this->players->remove((string) $playerId); |
|
274 | 3 | break; |
|
275 | 4 | } |
|
276 | 6 | return null; |
|
277 | 4 | } |
|
278 | |||
279 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
280 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
281 | ////////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////////// |
||
282 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
283 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
284 | |||
285 | /** |
||
286 | * Initialize the game |
||
287 | * |
||
288 | * @param MiniGameId $id |
||
289 | * @param string $word |
||
290 | */ |
||
291 | 114 | private function initialize(MiniGameId $id, $word) |
|
295 | |||
296 | /** |
||
297 | * @return bool |
||
298 | */ |
||
299 | 66 | private function isGameReady() |
|
303 | |||
304 | /** |
||
305 | * @param PlayerId $playerId |
||
306 | * @param string $reason |
||
307 | * |
||
308 | * @return HangmanGameFailedStartingEvent |
||
309 | */ |
||
310 | 6 | private function failStarting(PlayerId $playerId, $reason) |
|
320 | |||
321 | /** |
||
322 | * Player proposes a letter |
||
323 | * |
||
324 | * @param PlayerId $playerId |
||
325 | * @param Proposition $move |
||
326 | * |
||
327 | * @return GameResult |
||
328 | */ |
||
329 | 21 | private function playProposition(PlayerId $playerId, Proposition $move) |
|
338 | |||
339 | /** |
||
340 | * Player tries an answer |
||
341 | * |
||
342 | * @param PlayerId $playerId |
||
343 | * @param Answer $move |
||
344 | * |
||
345 | * @return GameResult |
||
346 | */ |
||
347 | 15 | private function playAnswer(PlayerId $playerId, Answer $move) |
|
366 | |||
367 | /** |
||
368 | * Returns an error event if player cannot play |
||
369 | * |
||
370 | * @param PlayerId $playerId |
||
371 | * |
||
372 | * @return GameResult |
||
373 | */ |
||
374 | 36 | private function ensurePlayerCanPlay(PlayerId $playerId) |
|
394 | |||
395 | /** |
||
396 | * Propose a letter |
||
397 | * |
||
398 | * @param string $letter |
||
399 | * |
||
400 | * @return HangmanBadProposition | HangmanGoodProposition |
||
401 | */ |
||
402 | 15 | private function currentPlayerProposeLetter($letter) |
|
410 | |||
411 | /** |
||
412 | * Propose an answer |
||
413 | * |
||
414 | * @param string $answer |
||
415 | * |
||
416 | * @return HangmanLost | HangmanWon |
||
417 | */ |
||
418 | 12 | private function currentPlayerProposeAnswer($answer) |
|
428 | |||
429 | /** |
||
430 | * Function to call when a bad proposition has been made |
||
431 | * |
||
432 | * @param string $letter |
||
433 | * |
||
434 | * @return HangmanBadProposition | HangmanLost |
||
435 | */ |
||
436 | 9 | private function currentPlayerBadProposition($letter) |
|
450 | |||
451 | /** |
||
452 | * Function to call after a good proposition of letter has been made |
||
453 | * |
||
454 | * @param string $letter |
||
455 | * |
||
456 | * @return HangmanGoodProposition | HangmanWon |
||
457 | */ |
||
458 | 6 | private function currentPlayerGoodProposition($letter) |
|
472 | |||
473 | /** |
||
474 | * Function to call when game is won by a player |
||
475 | * |
||
476 | * @param HangmanPlayer $player |
||
477 | * |
||
478 | * @return HangmanWon |
||
479 | */ |
||
480 | 6 | private function playerWins(HangmanPlayer $player) |
|
493 | |||
494 | /** |
||
495 | * Function to call when game is lost by a player |
||
496 | * |
||
497 | * @param HangmanPlayer $player |
||
498 | * |
||
499 | * @return hangmanLost | HangmanGameLostEvent |
||
500 | */ |
||
501 | 15 | private function playerLoses(HangmanPlayer $player) |
|
521 | |||
522 | /** |
||
523 | * Sets the next player |
||
524 | * |
||
525 | * @param PlayerId $id |
||
526 | */ |
||
527 | 48 | private function setNextPlayer(PlayerId $id = null) |
|
528 | { |
||
529 | 48 | if ($id === null || $this->players->isCurrentPlayer($id)) { |
|
530 | 48 | return; |
|
531 | } |
||
532 | |||
533 | 18 | $this->apply( |
|
534 | 18 | new HangmanPlayerTurnEvent($this->getId(), $id) |
|
535 | 12 | ); |
|
536 | 18 | } |
|
537 | |||
538 | /** |
||
539 | * Build the word from played letters |
||
540 | * |
||
541 | * @param string[] $playedLetters |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | 42 | public function buildWord($playedLetters) |
|
549 | |||
550 | /** |
||
551 | * Checks if all letters for the word have been found |
||
552 | * |
||
553 | * @param HangmanPlayer $player |
||
554 | * |
||
555 | * @return bool |
||
556 | */ |
||
557 | 6 | private function isAllLettersFoundForPlayer(HangmanPlayer $player) |
|
563 | |||
564 | /** |
||
565 | * Checks if the answer is valid |
||
566 | * If it's not, ends player turn and throws an HangmanException |
||
567 | * |
||
568 | * @param string $answer |
||
569 | * |
||
570 | * @throws HangmanException |
||
571 | */ |
||
572 | 12 | private function checkAnswerIsValid($answer) |
|
578 | |||
579 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
580 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
581 | //////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
||
582 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
583 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
584 | |||
585 | /** |
||
586 | * Apply the game created event |
||
587 | * |
||
588 | * @param HangmanGameCreatedEvent $event |
||
589 | * |
||
590 | * @return void |
||
591 | */ |
||
592 | 114 | protected function applyHangmanGameCreatedEvent(HangmanGameCreatedEvent $event) |
|
599 | |||
600 | /** |
||
601 | * Apply the player created event |
||
602 | * |
||
603 | * @param HangmanPlayerCreatedEvent $event |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | 63 | protected function applyHangmanPlayerCreatedEvent(HangmanPlayerCreatedEvent $event) |
|
619 | |||
620 | /** |
||
621 | * Apply the game created event |
||
622 | */ |
||
623 | 48 | protected function applyHangmanGameStartedEvent() |
|
627 | |||
628 | /** |
||
629 | * Apply the player turn event |
||
630 | * |
||
631 | * @param HangmanPlayerTurnEvent $event |
||
632 | */ |
||
633 | 18 | protected function applyHangmanPlayerTurnEvent(HangmanPlayerTurnEvent $event) |
|
637 | |||
638 | /** |
||
639 | * Apply the hangman player lost event |
||
640 | * |
||
641 | * @param HangmanPlayerLostEvent $event |
||
642 | */ |
||
643 | 21 | protected function applyHangmanPlayerLostEvent(HangmanPlayerLostEvent $event) |
|
647 | |||
648 | /** |
||
649 | * Apply the hangman player win event |
||
650 | * |
||
651 | * @return void |
||
652 | */ |
||
653 | 6 | protected function applyHangmanPlayerWinEvent() |
|
658 | |||
659 | /** |
||
660 | * Apply the hangman lost by all event |
||
661 | * |
||
662 | * @return void |
||
663 | */ |
||
664 | 3 | protected function applyHangmanGameLostEvent() |
|
669 | |||
670 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
671 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
672 | //////////////////////////////////////////// EVENT SOURCED ///////////////////////////////////////////////// |
||
673 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
674 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
675 | |||
676 | /** |
||
677 | * @return Player[] |
||
678 | */ |
||
679 | 114 | protected function getChildEntities() |
|
683 | |||
684 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
685 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
686 | ///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
||
687 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
688 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
689 | |||
690 | /** |
||
691 | * Create a new instance |
||
692 | * |
||
693 | * @param MiniGameId $id |
||
694 | * @param string $word |
||
695 | * |
||
696 | * @return Hangman |
||
697 | */ |
||
698 | 114 | public static function createGame(MiniGameId $id, $word) |
|
705 | |||
706 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
707 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
708 | /////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
||
709 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
710 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
711 | |||
712 | /** |
||
713 | * Static construction method for reconstitution |
||
714 | * |
||
715 | * @return Hangman |
||
716 | */ |
||
717 | 3 | public static function instantiateForReconstitution() |
|
721 | |||
722 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
723 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
724 | ///////////////////////////////////////// APPLY RESTRICTIONS /////////////////////////////////////////////// |
||
725 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
726 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
727 | |||
728 | /** |
||
729 | * @param mixed $event |
||
730 | * |
||
731 | * @throws HangmanException |
||
732 | */ |
||
733 | 114 | public function apply($event) |
|
741 | |||
742 | /** |
||
743 | * @param mixed $event |
||
744 | * |
||
745 | * @return bool |
||
746 | */ |
||
747 | 114 | private function isSupportedEvent($event) |
|
754 | } |
||
755 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.