Lines 88.88% 8 / 9
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 withService 100.00% 1 / 1 100.00% 1 / 1 1
 withOverrides 100.00% 1 / 1 100.00% 1 / 1 1
 declareDefinitionsOn 75.00% 3 / 4 0.00% 0 / 1 3.14
 setInstancesOn 100.00% 2 / 2 100.00% 1 / 1 2
10final readonly class SyntheticServices
11{
12    /** @param array<string, object> $services */
13    public function __construct(private array $services = []) {}
14
15    public function withService(string $id, object $service): self
16    {
17        return new self([...$this->services, $id => $service]);
18    }
19
20    public function withOverrides(self $overrides): self
21    {
22        return new self([...$this->services, ...$overrides->services]);
23    }
24
25    public function declareDefinitionsOn(ContainerBuilder $builder): void
26    {
27        foreach ($this->services as $id => $service) {
28            if ($builder->hasAlias($id)) {
29                $builder->removeAlias($id);
30            }
31
32            // Public so an unused one survives compilation and can still be set.
33            $builder->setDefinition($id, new Definition($service::class)->setSynthetic(true)->setPublic(true));
34        }
35    }
36
37    // Call after compile: setting earlier drops the definition, and factories using it fail.
38    public function setInstancesOn(ContainerBuilder $builder): void
39    {
40        foreach ($this->services as $id => $service) {
41            $builder->set($id, $service);
42        }
43    }
44}