Passed
Push — master ( ffdaaa...2bd757 )
by butschster
20:14 queued 13:13
created

FileDeclaration::addInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Reactor;
6
7
use Nette\PhpGenerator\ClassLike;
8
use Nette\PhpGenerator\ClassType;
9
use Nette\PhpGenerator\EnumType;
10
use Nette\PhpGenerator\GlobalFunction;
11
use Nette\PhpGenerator\InterfaceType;
12
use Nette\PhpGenerator\PhpNamespace as NettePhpNamespace;
13
use Nette\PhpGenerator\Factory;
14
use Nette\PhpGenerator\PhpFile;
15
use Nette\PhpGenerator\TraitType;
16
use Spiral\Reactor\Aggregator\Elements;
17
use Spiral\Reactor\Aggregator\Functions;
18
use Spiral\Reactor\Aggregator\Namespaces;
19
use Spiral\Reactor\Partial\PhpNamespace;
20
use Spiral\Reactor\Traits;
21
22
/**
23
 * Provides ability to render file content.
24
 */
25
class FileDeclaration implements \Stringable, DeclarationInterface
26
{
27
    use Traits\CommentAware;
28
    use Traits\EnumAware;
29
    use Traits\ClassAware;
30
    use Traits\InterfaceAware;
31
    use Traits\TraitAware;
32
33
    private PhpFile $element;
34
35 31
    public function __construct()
36
    {
37 31
        $this->element = new PhpFile();
38 31
        $this->element->setStrictTypes(true);
39
    }
40
41 3
    public function __toString(): string
42
    {
43 3
        return (new Printer())->print($this);
44
    }
45
46 1
    public static function fromCode(string $code): static
47
    {
48 1
        return self::fromElement((new Factory())->fromCode($code));
49
    }
50
51 17
    public function addNamespace(string|PhpNamespace $namespace): PhpNamespace
52
    {
53 17
        if ($namespace instanceof PhpNamespace) {
54 16
            $this->element->addNamespace($namespace->getElement());
55
56 16
            return $namespace;
57
        }
58
59 2
        return PhpNamespace::fromElement($this->element->addNamespace($namespace));
60
    }
61
62 1
    public function addFunction(string $name): FunctionDeclaration
63
    {
64 1
        return FunctionDeclaration::fromElement($this->element->addFunction($name));
65
    }
66
67 1
    public function getNamespaces(): Namespaces
68
    {
69 1
        return new Namespaces(\array_map(
70 1
            static fn (NettePhpNamespace $namespace) => PhpNamespace::fromElement($namespace),
71 1
            $this->element->getNamespaces()
72 1
        ));
73
    }
74
75 1
    public function getFunctions(): Functions
76
    {
77 1
        return new Functions(\array_map(
78 1
            static fn (GlobalFunction $function) => FunctionDeclaration::fromElement($function),
79 1
            $this->element->getFunctions()
80 1
        ));
81
    }
82
83 1
    public function addUse(string $name, ?string $alias = null, string $of = NettePhpNamespace::NameNormal): static
84
    {
85 1
        $this->element->addUse($name, $alias, $of);
86
87 1
        return $this;
88
    }
89
90
    /**
91
     * Adds declare(strict_types=1) to output.
92
     */
93 1
    public function setStrictTypes(bool $on = true): static
94
    {
95 1
        $this->element->setStrictTypes($on);
96
97 1
        return $this;
98
    }
99
100 1
    public function hasStrictTypes(): bool
101
    {
102 1
        return $this->element->hasStrictTypes();
103
    }
104
105 6
    public function getElements(): Elements
106
    {
107 6
        return new Elements(\array_map(
108 6
            static fn (ClassLike $element) => match (true) {
109 6
                $element instanceof ClassType => ClassDeclaration::fromElement($element),
110 6
                $element instanceof InterfaceType => InterfaceDeclaration::fromElement($element),
111 6
                $element instanceof TraitType => TraitDeclaration::fromElement($element),
112 6
                $element instanceof EnumType => EnumDeclaration::fromElement($element)
113 6
            },
114 6
            $this->element->getClasses()
115 6
        ));
116
    }
117
118
    /**
119
     * @internal
120
     */
121 2
    public static function fromElement(PhpFile $element): static
122
    {
123 2
        $file = new static();
124
125 2
        $file->element = $element;
126
127 2
        return $file;
128
    }
129
130
    /**
131
     * @internal
132
     */
133 16
    public function getElement(): PhpFile
134
    {
135 16
        return $this->element;
136
    }
137
138 2
    public function render(): string
139
    {
140 2
        return $this->__toString();
141
    }
142
}
143