| Conditions | 6 |
| Paths | 16 |
| Total Lines | 53 |
| Code Lines | 31 |
| 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 |
||
| 41 | public function findByUser( |
||
| 42 | User $user, |
||
| 43 | Course $course, |
||
| 44 | Session $session = null, |
||
| 45 | $orderField = 'creation_date', |
||
| 46 | $orderDirection = 'DESC' |
||
| 47 | ) { |
||
| 48 | switch ($orderField) { |
||
| 49 | case 'creation_date': |
||
| 50 | $orderField = 'N.creationDate'; |
||
| 51 | break; |
||
| 52 | case 'update_date': |
||
| 53 | $orderField = 'N.updateDate'; |
||
| 54 | break; |
||
| 55 | case 'title': |
||
| 56 | $orderField = 'N.title'; |
||
| 57 | break; |
||
| 58 | } |
||
| 59 | |||
| 60 | $qb = $this->createQueryBuilder('N'); |
||
| 61 | $qb |
||
| 62 | ->where( |
||
| 63 | $qb->expr()->andX( |
||
| 64 | $qb->expr()->eq('N.userId', $user->getId()), |
||
| 65 | $qb->expr()->eq('N.cId', $course->getId()) |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | |||
| 69 | if ($session) { |
||
| 70 | $qb->andWhere( |
||
| 71 | $qb->expr()->eq('N.sessionId', $session->getId()) |
||
| 72 | ); |
||
| 73 | } else { |
||
| 74 | $qb->andWhere( |
||
| 75 | $qb->expr()->orX( |
||
| 76 | $qb->expr()->eq('N.sessionId', 0), |
||
| 77 | $qb->expr()->isNull('N.sessionId') |
||
| 78 | ) |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($orderField === 'N.updateDate') { |
||
| 83 | $qb->andWhere( |
||
| 84 | $qb->expr()->orX( |
||
| 85 | $qb->expr()->neq('N.updateDate', ''), |
||
| 86 | $qb->expr()->isNotNull('N.updateDate') |
||
| 87 | ) |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | |||
| 91 | $qb->orderBy($orderField, $orderDirection); |
||
| 92 | |||
| 93 | return $qb->getQuery()->getResult(); |
||
| 94 | } |
||
| 96 |