Lines 100.00% 11 / 11
Methods 100.00% 6 / 6
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 createNamespace 100.00% 1 / 1 100.00% 1 / 1 1
 importClass 100.00% 6 / 6 100.00% 1 / 1 2
 resolveClassName 100.00% 1 / 1 100.00% 1 / 1 1
 printFile 100.00% 1 / 1 100.00% 1 / 1 1
 printStatements 100.00% 1 / 1 100.00% 1 / 1 1
12final readonly class EmitContext
13{
14    private const string CLASHING_IMPORT_SUFFIX = 'Contract';
15
16    public function __construct(
17        private GenerationTarget $target,
18        private GeneratedCodePrinter $printer,
19    ) {}
20
21    public function createNamespace(): PhpNamespace
22    {
23        return new PhpNamespace($this->target->namespace);
24    }
25
26    public function importClass(PhpNamespace $namespace, string $class, string $declaredShortName): string
27    {
28        $shortName = Helpers::extractShortName($class);
29
30        if (strcasecmp($shortName, $declaredShortName) !== 0) {
31            $namespace->addUse($class);
32
33            return $shortName;
34        }
35
36        $namespace->addUse($class, $shortName . self::CLASHING_IMPORT_SUFFIX);
37
38        return $shortName . self::CLASHING_IMPORT_SUFFIX;
39    }
40
41    public function resolveClassName(string $shortName): string
42    {
43        return $this->target->classFor($shortName);
44    }
45
46    public function printFile(PhpNamespace $namespace, string $shortName): GeneratedFile
47    {
48        return GeneratedFile::forClass($shortName, $this->printer->printGenerated($namespace));
49    }
50
51    public function printStatements(string $path, string $namespace, string $statements): GeneratedFile
52    {
53        return new GeneratedFile($path, $this->printer->printGeneratedStatements($namespace, $statements));
54    }
55}