Conditions | 4 |
Paths | 8 |
Total Lines | 53 |
Code Lines | 30 |
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 |
||
116 | private function getUnreadTopicsCountByForum($user, array $forumIds): array |
||
117 | { |
||
118 | // Requête 1: Topics visités mais avec nouveaux messages |
||
119 | $visitedUnreadQuery = $this->em->createQueryBuilder() |
||
120 | ->select('IDENTITY(t.forum) as forum_id, COUNT(t.id) as unread_count') |
||
121 | ->from('ProjetNormandie\ForumBundle\Entity\TopicUserLastVisit', 'tuv') |
||
122 | ->join('tuv.topic', 't') |
||
123 | ->join('t.lastMessage', 'lm') |
||
124 | ->where('t.forum IN (:forumIds)') |
||
125 | ->andWhere('tuv.user = :user') |
||
126 | ->andWhere('lm.createdAt > tuv.lastVisitedAt') |
||
127 | ->groupBy('t.forum') |
||
128 | ->setParameter('forumIds', $forumIds) |
||
129 | ->setParameter('user', $user); |
||
130 | |||
131 | $visitedUnread = $visitedUnreadQuery->getQuery()->getResult(); |
||
132 | |||
133 | // Requête 2: Topics jamais visités avec des messages |
||
134 | $neverVisitedQuery = $this->em->createQueryBuilder() |
||
135 | ->select('IDENTITY(t.forum) as forum_id, COUNT(t.id) as unread_count') |
||
136 | ->from('ProjetNormandie\ForumBundle\Entity\Topic', 't') |
||
137 | ->where('t.forum IN (:forumIds)') |
||
138 | ->andWhere('t.lastMessage IS NOT NULL') |
||
139 | ->andWhere('t.id NOT IN ( |
||
140 | SELECT IDENTITY(tuv2.topic) |
||
141 | FROM ProjetNormandie\ForumBundle\Entity\TopicUserLastVisit tuv2 |
||
142 | WHERE tuv2.user = :user |
||
143 | )') |
||
144 | ->groupBy('t.forum') |
||
145 | ->setParameter('forumIds', $forumIds) |
||
146 | ->setParameter('user', $user); |
||
147 | |||
148 | $neverVisited = $neverVisitedQuery->getQuery()->getResult(); |
||
149 | |||
150 | // Fusionner les résultats |
||
151 | $result = []; |
||
152 | |||
153 | // Initialiser tous les forums à 0 |
||
154 | foreach ($forumIds as $forumId) { |
||
155 | $result[$forumId] = 0; |
||
156 | } |
||
157 | |||
158 | // Ajouter les topics visités mais non lus |
||
159 | foreach ($visitedUnread as $row) { |
||
160 | $result[(int)$row['forum_id']] += (int)$row['unread_count']; |
||
161 | } |
||
162 | |||
163 | // Ajouter les topics jamais visités |
||
164 | foreach ($neverVisited as $row) { |
||
165 | $result[(int)$row['forum_id']] += (int)$row['unread_count']; |
||
166 | } |
||
167 | |||
168 | return $result; |
||
169 | } |
||
171 |