| Conditions | 9 |
| Paths | 9 |
| Total Lines | 115 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 109 | public function checkAnswer($message) |
||
| 110 | { |
||
| 111 | $game = $this->findGame($message); |
||
| 112 | $user = $this->getCurrentUser($message); |
||
| 113 | |||
| 114 | if ($game->status == 1) { |
||
| 115 | /** @var Question $question */ |
||
| 116 | $question = $this->em->getRepository('AppBundle:Question')->find($game->lastQuestion); |
||
| 117 | |||
| 118 | if (!$question) { |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | if (mb_strtoupper($question->a1, 'UTF-8') == mb_strtoupper($message['text'], 'UTF-8')) { |
||
| 123 | // Correct answer! |
||
| 124 | |||
| 125 | $user->setPoints($user->getPoints()+$question->price); |
||
| 126 | $this->em->persist($user); |
||
| 127 | |||
| 128 | // Log points |
||
| 129 | |||
| 130 | $pl = new PointLog(); |
||
| 131 | $pl->date = new \DateTime("now"); |
||
| 132 | $pl->day = date('d'); |
||
| 133 | $pl->month = date('m'); |
||
| 134 | $pl->year = date('Y'); |
||
| 135 | $pl->points = $question->price; |
||
| 136 | $pl->userId = $user->getId(); |
||
| 137 | $pl->gameId = $game->getId(); |
||
| 138 | |||
| 139 | $this->em->persist($pl); |
||
| 140 | |||
| 141 | $seconds = new \Datetime('now'); |
||
| 142 | $seconds = $seconds->getTimestamp() - $game->lastQuestionTime->getTimestamp(); |
||
| 143 | |||
| 144 | $this->botApi->sendMessage( |
||
| 145 | $game->chatId, |
||
| 146 | json_decode('"\ud83d\udc4d"').' Correct in *'.$seconds.'* secs! @'.$user->getAlias().' gets *'. |
||
| 147 | $question->price.'* and now has *'.$user->getPoints().'* points!', |
||
| 148 | 'markdown', |
||
| 149 | false, |
||
| 150 | $message['message_id'] |
||
| 151 | ); |
||
| 152 | |||
| 153 | $question->correct++; |
||
| 154 | $this->em->persist($question); |
||
| 155 | |||
| 156 | if ($user->fastestAnswer > $seconds) { |
||
| 157 | $user->fastestAnswer = $seconds; |
||
| 158 | $user->setPoints($user->getPoints() + 50); |
||
| 159 | |||
| 160 | $this->em->persist($user); |
||
| 161 | |||
| 162 | $pl = new PointLog(); |
||
| 163 | $pl->date = new \DateTime("now"); |
||
| 164 | $pl->day = date('d'); |
||
| 165 | $pl->month = date('m'); |
||
| 166 | $pl->year = date('Y'); |
||
| 167 | $pl->points = 50; |
||
| 168 | $pl->userId = $user->getId(); |
||
| 169 | $pl->gameId = $game->getId(); |
||
| 170 | |||
| 171 | $this->em->persist($pl); |
||
| 172 | |||
| 173 | $this->botApi->sendMessage( |
||
| 174 | $game->chatId, |
||
| 175 | '@'.$user->getAlias().' gets additional *50* points for his fastest answer ever!', |
||
| 176 | 'markdown', |
||
| 177 | false |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $this->askQuestion($game); |
||
| 182 | } else { |
||
| 183 | // Incorrect answer |
||
| 184 | $game->incorrectTries++; |
||
| 185 | $hint = $game->hint; |
||
| 186 | |||
| 187 | if (mb_substr_count($hint, '*', 'UTF-8') >= mb_strlen($hint, 'UTF-8')/2) { |
||
| 188 | //tell hint |
||
| 189 | |||
| 190 | for ($t = 0; $t< mb_strlen($hint); $t++) { |
||
| 191 | if (mb_substr($hint, $t, 1, 'UTF-8') == '*') { |
||
| 192 | if (rand(0, 1)==1) { |
||
| 193 | $hint = mb_substr($hint, 0, $t, 'UTF-8') |
||
| 194 | .mb_substr($question->a1, $t, 1) |
||
| 195 | .mb_substr($hint, $t+1); |
||
| 196 | break; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | $game->hint = $hint; |
||
| 202 | |||
| 203 | $this->botApi->sendMessage( |
||
| 204 | $game->chatId, |
||
| 205 | '<b>Hint</b>: '.$hint, |
||
| 206 | 'html' |
||
| 207 | ); |
||
| 208 | } else { |
||
| 209 | $this->botApi->sendMessage( |
||
| 210 | $game->chatId, |
||
| 211 | 'Noone answered, correct answer was: *'.$question->a1.'*', |
||
| 212 | 'markdown' |
||
| 213 | ); |
||
| 214 | $this->em->persist($game); |
||
| 215 | $this->em->flush(); |
||
| 216 | $this->askQuestion($game); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->em->persist($game); |
||
| 221 | $this->em->flush(); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 292 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.