Conditions | 8 |
Paths | 8 |
Total Lines | 87 |
Code Lines | 55 |
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 | $this->botApi->sendMessage( |
||
142 | $game->chatId, |
||
143 | 'Correct! @'.$user->getAlias().' gets *'. |
||
144 | $question->price.'* and now has *'.$user->getPoints().'* points!', |
||
145 | 'markdown', |
||
146 | false, |
||
147 | $message['message_id'] |
||
148 | ); |
||
149 | |||
150 | $question->correct++; |
||
151 | $this->em->persist($question); |
||
152 | |||
153 | $this->askQuestion($game); |
||
154 | } else { |
||
155 | // Incorrect answer |
||
156 | $game->incorrectTries++; |
||
157 | $hint = $game->hint; |
||
158 | |||
159 | if (mb_substr_count($hint, '*', 'UTF-8') >= mb_strlen($hint, 'UTF-8')/2) { |
||
160 | //tell hint |
||
161 | |||
162 | for ($t = 0; $t< mb_strlen($hint); $t++) { |
||
163 | if (mb_substr($hint, $t, 1, 'UTF-8') == '*') { |
||
164 | if (rand(0, 1)==1) { |
||
165 | $hint = mb_substr($hint, 0, $t, 'UTF-8') |
||
166 | .mb_substr($question->a1, $t, 1) |
||
167 | .mb_substr($hint, $t+1); |
||
168 | break; |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
173 | $game->hint = $hint; |
||
174 | |||
175 | $this->botApi->sendMessage( |
||
176 | $game->chatId, |
||
177 | '<b>Hint</b>: '.$hint, |
||
178 | 'html' |
||
179 | ); |
||
180 | } else { |
||
181 | $this->botApi->sendMessage( |
||
182 | $game->chatId, |
||
183 | 'Noone answered, correct answer was: *'.$question->a1.'*', |
||
184 | 'markdown' |
||
185 | ); |
||
186 | $this->em->persist($game); |
||
187 | $this->em->flush(); |
||
188 | $this->askQuestion($game); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | $this->em->persist($game); |
||
193 | $this->em->flush(); |
||
194 | } |
||
195 | } |
||
196 | |||
264 |
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.