| Conditions | 3 |
| Paths | 4 |
| Total Lines | 54 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 25 | public function __invoke(): mixed |
||
| 26 | { |
||
| 27 | $user = $this->getUser(); |
||
| 28 | |||
| 29 | $queryBuilder = $this->em->createQueryBuilder() |
||
| 30 | ->from('ProjetNormandie\ForumBundle\Entity\Category', 'c') |
||
| 31 | ->select('c') |
||
| 32 | ->join('c.forums', 'f') |
||
| 33 | ->join('f.lastMessage', 'm') |
||
| 34 | ->join('m.user', 'u') |
||
| 35 | ->addSelect('f') |
||
| 36 | ->addSelect('m') |
||
| 37 | ->addSelect('u'); |
||
| 38 | |||
| 39 | if ($user !== null) { |
||
| 40 | // Jointure LEFT pour les visites de forum (optionnelle) |
||
| 41 | $queryBuilder |
||
| 42 | ->leftJoin( |
||
| 43 | 'f.userLastVisits', |
||
| 44 | 'fuv', |
||
| 45 | 'WITH', |
||
| 46 | 'fuv.user = :user' |
||
| 47 | ) |
||
| 48 | ->addSelect('fuv') |
||
| 49 | ->where( |
||
| 50 | $queryBuilder->expr()->orX( |
||
| 51 | 'f.status = :status1', |
||
| 52 | '(f.status = :status2) AND (f.role IN (:roles))' |
||
| 53 | ) |
||
| 54 | ) |
||
| 55 | ->setParameter('status1', ForumStatus::PUBLIC) |
||
| 56 | ->setParameter('status2', ForumStatus::PRIVATE) |
||
| 57 | ->setParameter('user', $user) |
||
| 58 | ->setParameter('roles', $user->getRoles()); |
||
| 59 | } else { |
||
| 60 | $queryBuilder->where('f.status = :status') |
||
| 61 | ->setParameter('status', ForumStatus::PUBLIC); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Filtrer les catégories qui doivent être affichées sur la home |
||
| 65 | $queryBuilder->andWhere('c.displayOnHome = :displayOnHome') |
||
| 66 | ->setParameter('displayOnHome', true); |
||
| 67 | |||
| 68 | $queryBuilder->orderBy('c.position', 'ASC') |
||
| 69 | ->addOrderBy('f.position', 'ASC'); |
||
| 70 | |||
| 71 | $categories = $queryBuilder->getQuery()->getResult(); |
||
| 72 | |||
| 73 | // Si utilisateur connecté, enrichir avec les compteurs de topics non lus |
||
| 74 | if ($user !== null) { |
||
| 75 | $this->enrichWithReadStatus($categories, $user); |
||
| 76 | } |
||
| 77 | |||
| 78 | return $categories; |
||
| 79 | } |
||
| 171 |