$intervals */ public function calculate(iterable $intervals): MetricAggregate { $totalDays = 0; $patientCount = 0; foreach ($intervals as $interval) { if ($interval->endAt < $interval->startAt) { continue; } $totalDays += $interval->startAt->setTime(0, 0)->diff($interval->endAt->setTime(0, 0))->days; $patientCount++; } return new MetricAggregate( total: $totalDays, count: $patientCount, average: $patientCount > 0 ? round($totalDays / $patientCount, 2) : 0.0, ); } }