Lines 90.90% 10 / 11
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SourceTree 90.90% 10 / 11 50.00% 1 / 2 6.03 0.00% 0 / 1
 __construct 0.00% 0 / 1 0.00% 0 / 1 2
 renderFiles 100.00% 10 / 10 100.00% 1 / 1 5
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen\Emitter;
6
7use Stewart\Codegen\GenerationTarget;
8use Stewart\Codegen\Model\GenerationModel;
9use Stewart\Codegen\Output\GeneratedFile;
10
11final readonly class SourceTree
12{
13    /**
14     * @param iterable<ModelFileEmitter> $modelEmitters
15     * @param iterable<DomainFileEmitter> $domainEmitters
16     */
17    public function __construct(
18        private iterable $modelEmitters,
19        private iterable $domainEmitters,
20        private GeneratedCodePrinter $printer,
21    ) {}
22
23    /** @return list<GeneratedFile> */
24    public function renderFiles(GenerationModel $model, GenerationTarget $target): array
25    {
26        $context = new EmitContext($target, $this->printer);
27        $files = [];
28
29        foreach ($this->modelEmitters as $emitter) {
30            $files[] = $emitter->emitFile($model, $context);
31        }
32
33        foreach ($model->domains as $domain) {
34            foreach ($this->domainEmitters as $emitter) {
35                if ($emitter->supportsDomain($domain)) {
36                    $files[] = $emitter->emitFile($domain, $context);
37                }
38            }
39        }
40
41        usort($files, static fn(GeneratedFile $a, GeneratedFile $b): int => strcmp($a->path, $b->path));
42
43        return $files;
44    }
45}