| Conditions | 8 |
| Paths | 6 |
| Total Lines | 76 |
| Code Lines | 46 |
| 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 |
||
| 108 | public function checkAnswer($message) |
||
| 109 | { |
||
| 110 | $game = $this->findGame($message); |
||
| 111 | $user = $this->getCurrentUser($message); |
||
| 112 | |||
| 113 | if ($game->status == 1) { |
||
| 114 | /** @var Question $question */ |
||
| 115 | $question = $this->em->getRepository('AppBundle:Question')->find($game->lastQuestion); |
||
| 116 | |||
| 117 | if (!$question) { |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (mb_strtoupper($question->a1, 'UTF-8') == mb_strtoupper($message['text'], 'UTF-8')) { |
||
| 122 | // Correct answer! |
||
| 123 | |||
| 124 | $user->setPoints($user->getPoints()+$question->price); |
||
| 125 | $this->em->persist($user); |
||
| 126 | |||
| 127 | $this->botApi->sendMessage( |
||
| 128 | $game->chatId, |
||
| 129 | 'Correct! @'.$user->getAlias().' gets *'. |
||
| 130 | $question->price.'* and now has *'.$user->getPoints().'* points!', |
||
| 131 | 'markdown', |
||
| 132 | false, |
||
| 133 | $message['message_id'] |
||
| 134 | ); |
||
| 135 | |||
| 136 | $question->correct++; |
||
| 137 | $this->em->persist($question); |
||
| 138 | |||
| 139 | $this->askQuestion($game); |
||
| 140 | } else { |
||
| 141 | // Incorrect answer |
||
| 142 | $game->incorrectTries++; |
||
| 143 | $hint = $game->hint; |
||
| 144 | |||
| 145 | if (mb_substr_count($hint,'*','UTF-8') >= mb_strlen($hint,'UTF-8')/2) { |
||
| 146 | //tell hint |
||
| 147 | |||
| 148 | for($t = 0; $t< mb_strlen($hint);$t++) { |
||
| 149 | if (mb_substr($hint,$t,1,'UTF-8') == '*') { |
||
| 150 | if (rand(0,1)==1) { |
||
| 151 | $hint = mb_substr($hint,0,$t,'UTF-8') |
||
| 152 | .mb_substr($question->a1,$t,1) |
||
| 153 | .mb_substr($hint,$t+1); |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | $game->hint = $hint; |
||
| 160 | |||
| 161 | $this->botApi->sendMessage( |
||
| 162 | $game->chatId, |
||
| 163 | '<b>Hint</b>: '.$hint, |
||
| 164 | 'html' |
||
| 165 | ); |
||
| 166 | |||
| 167 | } else { |
||
| 168 | $this->botApi->sendMessage( |
||
| 169 | $game->chatId, |
||
| 170 | 'Noone answered, correct answer was: *'.$question->a1.'*', |
||
| 171 | 'markdown' |
||
| 172 | ); |
||
| 173 | $this->em->persist($game); |
||
| 174 | $this->em->flush(); |
||
| 175 | $this->askQuestion($game); |
||
| 176 | } |
||
| 177 | |||
| 178 | } |
||
| 179 | |||
| 180 | $this->em->persist($game); |
||
| 181 | $this->em->flush(); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 229 |
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.