diff --git a/src/Collector/Collector.php b/src/Collector/Collector.php index 921afe1..155327e 100644 --- a/src/Collector/Collector.php +++ b/src/Collector/Collector.php @@ -10,6 +10,8 @@ use Panaly\Result\Metric; use Panaly\Result\Result; +use function assert; + readonly class Collector { public function __construct( @@ -22,7 +24,10 @@ public function collect(): Result { $result = new Result(); foreach ($this->configurationFile->metricGroups as $executingGroup) { - $group = new Group($executingGroup->title); + $title = $executingGroup->title; + assert($title !== ''); // Ensured by validation of the object + + $group = new Group($title); foreach ($executingGroup->metrics as $executingMetric) { $metricHandler = $this->runtimeConfiguration->getMetric($executingMetric->identifier); diff --git a/src/Result/Group.php b/src/Result/Group.php index c26f108..375dc09 100644 --- a/src/Result/Group.php +++ b/src/Result/Group.php @@ -6,7 +6,10 @@ class Group { - /** @param list $metrics */ + /** + * @param non-empty-string $title + * @param list $metrics + */ public function __construct( private readonly string $title, private array $metrics = [], @@ -29,7 +32,7 @@ public function addMetric(Metric $metric): void $this->metrics[] = $metric; } - /** @return array{title: string, metrics: list} */ + /** @return array{title: non-empty-string, metrics: list} */ public function toArray(): array { $metricsAsArray = []; diff --git a/src/Result/Result.php b/src/Result/Result.php index cce307f..627e881 100644 --- a/src/Result/Result.php +++ b/src/Result/Result.php @@ -4,11 +4,25 @@ namespace Panaly\Result; +use DateTimeImmutable; +use DateTimeInterface; + class Result { + private readonly DateTimeImmutable $createAt; /** @var list */ private array $groups = []; + public function __construct() + { + $this->createAt = new DateTimeImmutable(); + } + + public function getCreateAt(): DateTimeImmutable + { + return $this->createAt; + } + /** @return list */ public function getGroups(): array { @@ -20,12 +34,24 @@ public function addGroup(Group $group): void $this->groups[] = $group; } - /** @return list}> */ + /** + * @return array{ + * createdAt: non-falsy-string, + * groups: list + * }> + * } + */ public function toArray(): array { - $asArray = []; + $asArray = [ + 'createdAt' => $this->createAt->format(DateTimeInterface::ATOM), + 'groups' => [], + ]; + foreach ($this->groups as $group) { - $asArray[] = $group->toArray(); + $asArray['groups'][] = $group->toArray(); } return $asArray; diff --git a/tests/Result/ResultTest.php b/tests/Result/ResultTest.php new file mode 100644 index 0000000..c475e6a --- /dev/null +++ b/tests/Result/ResultTest.php @@ -0,0 +1,22 @@ +toArray(); + + self::assertArrayHasKey('createdAt', $resultAsArray); + self::assertIsString($resultAsArray['createdAt']); + self::assertSame($result->getCreateAt()->format(DateTimeInterface::ATOM), $resultAsArray['createdAt']); + } +}