| Conditions | 6 |
| Paths | 16 |
| Total Lines | 56 |
| 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 |
||
| 25 | public function findByUser( |
||
| 26 | User $user, |
||
| 27 | Course $course, |
||
| 28 | Session $session = null, |
||
| 29 | $orderField = 'creation_date', |
||
| 30 | $orderDirection = 'DESC' |
||
| 31 | ) { |
||
| 32 | switch ($orderField) { |
||
| 33 | case 'creation_date': |
||
| 34 | $orderField = 'N.creationDate'; |
||
| 35 | |||
| 36 | break; |
||
| 37 | case 'update_date': |
||
| 38 | $orderField = 'N.updateDate'; |
||
| 39 | |||
| 40 | break; |
||
| 41 | case 'title': |
||
| 42 | $orderField = 'N.title'; |
||
| 43 | |||
| 44 | break; |
||
| 45 | } |
||
| 46 | |||
| 47 | $qb = $this->createQueryBuilder('N'); |
||
| 48 | $qb |
||
| 49 | ->where( |
||
| 50 | $qb->expr()->andX( |
||
| 51 | $qb->expr()->eq('N.userId', $user->getId()), |
||
| 52 | $qb->expr()->eq('N.cId', $course->getId()) |
||
| 53 | ) |
||
| 54 | ); |
||
| 55 | |||
| 56 | if ($session) { |
||
| 57 | $qb->andWhere( |
||
| 58 | $qb->expr()->eq('N.sessionId', $session->getId()) |
||
| 59 | ); |
||
| 60 | } else { |
||
| 61 | $qb->andWhere( |
||
| 62 | $qb->expr()->orX( |
||
| 63 | $qb->expr()->eq('N.sessionId', 0), |
||
| 64 | $qb->expr()->isNull('N.sessionId') |
||
| 65 | ) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | if ('N.updateDate' === $orderField) { |
||
| 70 | $qb->andWhere( |
||
| 71 | $qb->expr()->orX( |
||
| 72 | $qb->expr()->neq('N.updateDate', ''), |
||
| 73 | $qb->expr()->isNotNull('N.updateDate') |
||
| 74 | ) |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | $qb->orderBy($orderField, $orderDirection); |
||
| 79 | |||
| 80 | return $qb->getQuery()->getResult(); |
||
| 81 | } |
||
| 83 |