| Conditions | 7 |
| Paths | 13 |
| Total Lines | 63 |
| Code Lines | 40 |
| 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 |
||
| 46 | private function addWhere(QueryBuilder $qb, string $resourceClass): void |
||
| 47 | { |
||
| 48 | if (SessionRelUser::class !== $resourceClass) { |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | $request = $this->requestStack->getCurrentRequest(); |
||
| 53 | $alias = $qb->getRootAliases()[0]; |
||
| 54 | $content = $request->getContent(); |
||
| 55 | |||
| 56 | $now = new DateTime('now', new DateTimeZone('UTC')); |
||
| 57 | $date = $now->format('Y-m-d H:i:s'); |
||
| 58 | |||
| 59 | $qb->innerJoin("$alias.session", 's'); |
||
| 60 | |||
| 61 | if (str_contains($content, 'getCurrentSessions')) { |
||
| 62 | $qb->andWhere( |
||
| 63 | $qb->expr()->orX( |
||
| 64 | $qb->expr()->andX( |
||
| 65 | $qb->expr()->isNotNull('s.accessStartDate'), |
||
| 66 | $qb->expr()->isNull('s.accessEndDate'), |
||
| 67 | $qb->expr()->lte('s.accessStartDate', "'$date'") |
||
| 68 | ), |
||
| 69 | $qb->expr()->andX( |
||
| 70 | $qb->expr()->isNotNull('s.accessStartDate'), |
||
| 71 | $qb->expr()->isNotNull('s.accessEndDate'), |
||
| 72 | $qb->expr()->lte('s.accessStartDate', "'$date'"), |
||
| 73 | $qb->expr()->gte('s.accessEndDate', "'$date'") |
||
| 74 | ), |
||
| 75 | $qb->expr()->andX( |
||
| 76 | $qb->expr()->isNull('s.accessStartDate'), |
||
| 77 | $qb->expr()->isNotNull('s.accessEndDate'), |
||
| 78 | $qb->expr()->gte('s.accessEndDate', "'$date'") |
||
| 79 | ) |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | } elseif (str_contains($content, 'getUpcommingSessions')) { |
||
| 83 | $qb->andWhere( |
||
| 84 | $qb->expr()->andX( |
||
| 85 | $qb->expr()->isNotNull('s.accessStartDate'), |
||
| 86 | $qb->expr()->gt('s.accessStartDate', "'$date'") |
||
| 87 | ) |
||
| 88 | ); |
||
| 89 | } elseif (str_contains($content, 'getPastSessions')) { |
||
| 90 | $qb->andWhere( |
||
| 91 | $qb->expr()->andX( |
||
| 92 | $qb->expr()->isNotNull('s.accessEndDate'), |
||
| 93 | $qb->expr()->lt('s.accessEndDate', "'$date'") |
||
| 94 | ) |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($this->security->isGranted('ROLE_ADMIN')) { |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** @var User|null $user */ |
||
| 103 | if (null === $user = $this->security->getUser()) { |
||
| 104 | throw new AccessDeniedException('Access Denied SessionRelUser'); |
||
| 105 | } |
||
| 106 | |||
| 107 | $qb->andWhere(sprintf('%s.user = :current_user', $alias)); |
||
| 108 | $qb->setParameter('current_user', $user->getId()); |
||
| 109 | } |
||
| 111 |