Conditions | 12 |
Paths | 20 |
Total Lines | 88 |
Lines | 8 |
Ratio | 9.09 % |
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 |
||
153 | public function getAlbum($game, $user) |
||
154 | { |
||
155 | // all the models of the album |
||
156 | $models = $this->getTradingCardModelMapper()->findAll(); |
||
157 | $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
||
158 | |||
159 | if ($user) { |
||
160 | $qb = $em->createQueryBuilder(); |
||
161 | $and = $qb->expr()->andx(); |
||
162 | $and->add($qb->expr()->eq('g.id', ':game')); |
||
163 | $qb->setParameter('game', $game); |
||
164 | $and->add($qb->expr()->eq('u.id', ':user')); |
||
165 | $qb->setParameter('user', $user); |
||
166 | $qb->select('c') |
||
167 | ->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
||
168 | ->innerJoin('c.game', 'g') |
||
169 | ->innerJoin('c.model', 'm') |
||
170 | ->innerJoin('c.user', 'u') |
||
171 | ->where($and) |
||
172 | ->orderBy('m.id', 'ASC') |
||
173 | ->groupBy('c.model'); |
||
174 | $query = $qb->getQuery(); |
||
175 | } elseif ($game->getAnonymousAllowed() && $this->getAnonymousIdentifier()) { |
||
176 | $limitDate = $this->getLimitDate('always'); |
||
177 | $entries = $this->getEntryMapper()->findLastEntriesByAnonymousIdentifier( |
||
178 | $game, |
||
179 | $this->getAnonymousIdentifier(), |
||
180 | $limitDate |
||
181 | ); |
||
182 | $qb = $em->createQueryBuilder(); |
||
183 | $qb->andWhere('e.id IN (:entries)')->setParameter('entries', $entries); |
||
184 | $qb->select('c') |
||
185 | ->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
||
186 | ->innerJoin('c.model', 'm') |
||
187 | ->innerJoin('c.game', 'g') |
||
188 | ->innerJoin('c.entry', 'e') |
||
189 | ->orderBy('m.id', 'ASC') |
||
190 | ->groupBy('c.model'); |
||
191 | $query = $qb->getQuery(); |
||
192 | } elseif ($game->getAnonymousAllowed()) { |
||
193 | $limitDate = $this->getLimitDate('always'); |
||
194 | $entries = $this->getEntryMapper()->findLastEntriesByIp( |
||
195 | $game, |
||
196 | $this->getIp(), |
||
197 | $limitDate |
||
198 | ); |
||
199 | $qb = $em->createQueryBuilder(); |
||
200 | $qb->andWhere('e.id IN (:entries)')->setParameter('entries', $entries); |
||
201 | $qb->select('c') |
||
202 | ->from('PlaygroundGame\Entity\TradingCardCard', 'c') |
||
203 | ->innerJoin('c.model', 'm') |
||
204 | ->innerJoin('c.game', 'g') |
||
205 | ->innerJoin('c.entry', 'e') |
||
206 | ->orderBy('m.id', 'ASC') |
||
207 | ->groupBy('c.model'); |
||
208 | $query = $qb->getQuery(); |
||
209 | View Code Duplication | } else { |
|
210 | // If the game is supposed to be a regular user game or an anonymous identified game, |
||
211 | // it means that the registration/login is at the end of the game |
||
212 | if ((!$user && !$game->getAnonymousAllowed()) || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) { |
||
213 | return 0; |
||
214 | } |
||
215 | return $this->getEntryMapper()->findLastEntriesByIp($game, $this->getIp(), $limitDate); |
||
216 | } |
||
217 | |||
218 | |||
219 | // all the cards of the user |
||
220 | $cards = $query->getResult(); |
||
221 | $cardsArray = []; |
||
222 | foreach ($cards as $card) { |
||
223 | $cardsArray[$card->getModel()->getId()] = $card; |
||
224 | } |
||
225 | |||
226 | $album = []; |
||
227 | |||
228 | // I create the complete album including the cards of the user |
||
229 | foreach ($models as $model) { |
||
230 | $sticker['model'] = $model; |
||
231 | if (isset($cardsArray[$model->getId()])) { |
||
232 | $sticker['card'] = $cardsArray[$model->getId()]; |
||
233 | } else { |
||
234 | $sticker['card'] = null; |
||
235 | } |
||
236 | $album[] = $sticker; |
||
237 | } |
||
238 | |||
239 | return $album; |
||
240 | } |
||
241 | |||
274 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.