Lines 100.00% 5 / 5
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
FileChangeCollection 100.00% 5 / 5 100.00% 5 / 5 5 100.00% 1 / 1
 fromChangesSortedByPath 100.00% 1 / 1 100.00% 1 / 1 1
 filterStale 100.00% 1 / 1 100.00% 1 / 1 1
 countWithOutcome 100.00% 1 / 1 100.00% 1 / 1 1
 countExistingFiles 100.00% 1 / 1 100.00% 1 / 1 1
 isClean 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen\Output\Collection;
6
7use Stewart\Codegen\Output\FileChange;
8use Stewart\Codegen\Output\FileOutcome;
9use Stewart\Contracts\Collection\ListCollection;
10
11/** @extends ListCollection<FileChange> */
12final readonly class FileChangeCollection extends ListCollection
13{
14    /** @param iterable<FileChange> $changes */
15    public static function fromChangesSortedByPath(iterable $changes): self
16    {
17        return self::fromList($changes)->sortedBy(static fn(FileChange $a, FileChange $b): int => strcmp($a->path, $b->path));
18    }
19
20    public function filterStale(): self
21    {
22        return $this->filter(static fn(FileChange $change): bool => $change->outcome->isChange());
23    }
24
25    public function countWithOutcome(FileOutcome $outcome): int
26    {
27        return $this->filter(static fn(FileChange $change): bool => $change->outcome === $outcome)->count();
28    }
29
30    public function countExistingFiles(): int
31    {
32        return $this->filter(static fn(FileChange $change): bool => $change->outcome !== FileOutcome::Created)->count();
33    }
34
35    public function isClean(): bool
36    {
37        return !$this->containsWhere(static fn(FileChange $change): bool => $change->outcome->isChange());
38    }
39}