| Conditions | 2 |
| Paths | 1 |
| Total Lines | 78 |
| Code Lines | 55 |
| 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 |
||
| 59 | public function getWanderStats(): array |
||
| 60 | { |
||
| 61 | $stats = $this->cache->get('wander_stats', function(ItemInterface $item) { |
||
| 62 | $item->tag('stats'); |
||
| 63 | $wanderStats = $this->wanderRepository |
||
| 64 | ->createQueryBuilder('w') |
||
| 65 | ->select('COUNT(w.id) as totalCount') |
||
| 66 | ->addSelect('COUNT(w.title) as countWithTitle') |
||
| 67 | ->addSelect('COUNT(w.description) as countWithDescription') |
||
| 68 | //->addSelect('SUM(w.durationSeconds) as totalDuration') |
||
| 69 | ->addSelect('COALESCE(SUM(w.distance), 0) as totalDistance') |
||
| 70 | ->addSelect('COALESCE(SUM(w.cumulativeElevationGain), 0) as totalCumulativeElevationGain') |
||
| 71 | ->getQuery() |
||
| 72 | ->getOneOrNullResult(); |
||
| 73 | |||
| 74 | $wanderStats['hasWanders'] = $wanderStats['totalCount'] > 0; |
||
| 75 | |||
| 76 | // Doctrine doesn't support calculating a difference |
||
| 77 | // in seconds from two datetime values via ORM. Let's |
||
| 78 | // go raw. |
||
| 79 | $conn = $this->entityManager->getConnection(); |
||
| 80 | $sql = 'SELECT COALESCE(SUM(TIME_TO_SEC(TIMEDIFF(end_time, start_time))), 0) FROM wander'; |
||
| 81 | $stmt = $conn->prepare($sql); |
||
| 82 | $stmt->execute(); |
||
| 83 | $seconds = $stmt->fetchOne(); |
||
| 84 | |||
| 85 | $interval = CarbonInterval::seconds($seconds)->cascade(); |
||
| 86 | |||
| 87 | $wanderStats['totalDuration'] = $interval; |
||
| 88 | $wanderStats['totalDurationForHumans'] = $interval->forHumans([ |
||
| 89 | 'short' => true |
||
| 90 | ]); |
||
| 91 | |||
| 92 | // Set up defaults for average, in case we have no wanders |
||
| 93 | $wanderStats['averageWanderDuration'] = 0; |
||
| 94 | $wanderStats['averageWanderDurationForHumans'] = '0s'; |
||
| 95 | |||
| 96 | if ($wanderStats['hasWanders']) { // Avoid divide by zero |
||
| 97 | $interval = CarbonInterval::seconds($seconds / $wanderStats['totalCount'])->cascade(); |
||
| 98 | $wanderStats['averageDuration'] = $interval; |
||
| 99 | $wanderStats['averageDurationForHumans'] = $interval->forHumans([ |
||
| 100 | 'short' => true |
||
| 101 | ]); |
||
| 102 | } |
||
| 103 | |||
| 104 | $wanderStats['longestWanderDistance'] = $this->wanderRepository |
||
| 105 | ->createQueryBuilder('w') |
||
| 106 | ->select('w.id, w.distance') |
||
| 107 | ->orderBy('w.distance', 'desc') |
||
| 108 | ->setMaxResults(1) |
||
| 109 | ->getQuery() |
||
| 110 | ->getOneOrNullResult(); |
||
| 111 | |||
| 112 | $wanderStats['shortestWanderDistance'] = $this->wanderRepository |
||
| 113 | ->createQueryBuilder('w') |
||
| 114 | ->select('w.id, w.distance') |
||
| 115 | ->orderBy('w.distance', 'asc') |
||
| 116 | ->setMaxResults(1) |
||
| 117 | ->getQuery() |
||
| 118 | ->getOneOrNullResult(); |
||
| 119 | |||
| 120 | $wanderStats['averageWanderDistance'] = $this->wanderRepository |
||
| 121 | ->createQueryBuilder('w') |
||
| 122 | ->select('AVG(w.distance)') |
||
| 123 | ->getQuery() |
||
| 124 | ->getSingleScalarResult() ?? 0; |
||
| 125 | |||
| 126 | $qb = $this->wanderRepository |
||
| 127 | ->createQueryBuilder('w'); |
||
| 128 | $wanderStats['imageProcessingBacklog'] = $this->wanderRepository->addWhereHasImages($qb, false) |
||
| 129 | ->select('COUNT(w.id)') |
||
| 130 | ->getQuery() |
||
| 131 | ->getSingleScalarResult(); |
||
| 132 | |||
| 133 | return $wanderStats; |
||
| 134 | }); |
||
| 135 | |||
| 136 | return $stats; |
||
| 137 | } |
||
| 138 | } |