| Conditions | 15 |
| Paths | 9 |
| Total Lines | 39 |
| Lines | 12 |
| Ratio | 30.77 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 129 | public function endGame(Facegame $game, $wrongAnswers, $duration) |
||
| 130 | { |
||
| 131 | if (!empty($game->getDuration())) { |
||
| 132 | throw new BadRequestHttpException('Jeu déjà fini'); |
||
| 133 | } |
||
| 134 | |||
| 135 | $game->setWrongAnswers($wrongAnswers); |
||
| 136 | $game->setDuration($duration); |
||
| 137 | $this->manager->flush(); |
||
| 138 | |||
| 139 | $achievementCheck = new AchievementCheckEvent(Achievement::GAME_PLAY); |
||
| 140 | $this->dispatcher->dispatch('upont.achievement', $achievementCheck); |
||
| 141 | |||
| 142 | $promoUser = (int)$this->tokenStorage->getToken()->getUser()->getPromo(); |
||
| 143 | $promoGame = (int)$game->getPromo(); |
||
| 144 | |||
| 145 | if ($wrongAnswers == 0 && $promoGame == $promoUser - 1 && $duration < 60 * 1000) { |
||
| 146 | |||
| 147 | $achievementCheck = new AchievementCheckEvent(Achievement::GAME_BEFORE); |
||
| 148 | $this->dispatcher->dispatch('upont.achievement', $achievementCheck); |
||
| 149 | |||
| 150 | } else if ($wrongAnswers == 0 && $promoGame == $promoUser && $duration < 60 * 1000) { |
||
| 151 | |||
| 152 | $achievementCheck = new AchievementCheckEvent(Achievement::GAME_SELF); |
||
| 153 | $this->dispatcher->dispatch('upont.achievement', $achievementCheck); |
||
| 154 | |||
| 155 | View Code Duplication | } else if ($wrongAnswers == 0 && $promoGame == $promoUser + 1 && $duration < 60 * 1000) { |
|
| 156 | |||
| 157 | $achievementCheck = new AchievementCheckEvent(Achievement::GAME_NEXT); |
||
| 158 | $this->dispatcher->dispatch('upont.achievement', $achievementCheck); |
||
| 159 | |||
| 160 | } |
||
| 161 | View Code Duplication | if ($wrongAnswers == 0 && $promoGame < $promoUser && $game->getHardcore() && $duration < 60 * 1000) { |
|
| 162 | |||
| 163 | $achievementCheck = new AchievementCheckEvent(Achievement::GAME_OLD); |
||
| 164 | $this->dispatcher->dispatch('upont.achievement', $achievementCheck); |
||
| 165 | |||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.