Passed
Pull Request — master (#849)
by Maxim
06:23
created

PhpNamespace::getTraits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Reactor\Partial;
6
7
use Nette\PhpGenerator\ClassLike;
8
use Nette\PhpGenerator\ClassType;
9
use Nette\PhpGenerator\EnumType;
10
use Nette\PhpGenerator\InterfaceType;
11
use Nette\PhpGenerator\PhpNamespace as NettePhpNamespace;
12
use Nette\PhpGenerator\TraitType;
13
use Spiral\Reactor\AggregableInterface;
14
use Spiral\Reactor\Aggregator\Classes;
15
use Spiral\Reactor\Aggregator\Elements;
16
use Spiral\Reactor\Aggregator\Enums;
17
use Spiral\Reactor\Aggregator\Interfaces;
18
use Spiral\Reactor\Aggregator\Traits;
19
use Spiral\Reactor\ClassDeclaration;
20
use Spiral\Reactor\EnumDeclaration;
21
use Spiral\Reactor\InterfaceDeclaration;
22
use Spiral\Reactor\NamedInterface;
23
use Spiral\Reactor\TraitDeclaration;
24
use Spiral\Reactor\Traits\NameAware;
25
26
final class PhpNamespace implements NamedInterface, AggregableInterface, \Stringable
27
{
28
    use NameAware;
29
30
    private NettePhpNamespace $element;
31
32 29
    public function __construct(string $name)
33
    {
34 29
        $this->element = new NettePhpNamespace($name);
35
    }
36
37 4
    public function __toString(): string
38
    {
39 4
        return $this->element->__toString();
40
    }
41
42 1
    public function hasBracketedSyntax(): bool
43
    {
44 1
        return $this->element->hasBracketedSyntax();
45
    }
46
47 14
    public function addUse(string $name, ?string $alias = null, string $of = NettePhpNamespace::NameNormal): self
48
    {
49 14
        $this->element->addUse($name, $alias, $of);
50
51 14
        return $this;
52
    }
53
54 3
    public function removeUse(string $name, string $of = NettePhpNamespace::NameNormal): void
55
    {
56 3
        $this->element->removeUse($name, $of);
57
    }
58
59 1
    public function addUseFunction(string $name, ?string $alias = null): self
60
    {
61 1
        $this->element->addUseFunction($name, $alias);
62
63 1
        return $this;
64
    }
65
66 1
    public function addUseConstant(string $name, ?string $alias = null): self
67
    {
68 1
        $this->element->addUseConstant($name, $alias);
69
70 1
        return $this;
71
    }
72
73
    /** @return string[] */
74 3
    public function getUses(string $of = NettePhpNamespace::NameNormal): array
75
    {
76 3
        return $this->element->getUses($of);
77
    }
78
79
    public function resolveName(string $name, string $of = NettePhpNamespace::NameNormal): string
80
    {
81
        return $this->element->resolveName($name, $of);
82
    }
83
84
    public function simplifyType(string $type, string $of = NettePhpNamespace::NameNormal): string
85
    {
86
        return $this->element->simplifyType($type, $of);
87
    }
88
89
    public function simplifyName(string $name, string $of = NettePhpNamespace::NameNormal): string
90
    {
91
        return $this->element->simplifyName($name, $of);
92
    }
93
94 16
    public function addClass(string $name): ClassDeclaration
95
    {
96 16
        return ClassDeclaration::fromElement($this->element->addClass($name));
97
    }
98
99 6
    public function getClasses(): Classes
100
    {
101 6
        $classes = \array_filter(
102 6
            $this->element->getClasses(),
103 6
            static fn (ClassLike $element): bool => $element instanceof ClassType
104 6
        );
105
106 6
        return new Classes(\array_map(
107 6
            static fn (ClassType $class): ClassDeclaration => ClassDeclaration::fromElement($class),
108 6
            $classes
109 6
        ));
110
    }
111
112 1
    public function addInterface(string $name): InterfaceDeclaration
113
    {
114 1
        return InterfaceDeclaration::fromElement($this->element->addInterface($name));
115
    }
116
117 5
    public function getInterfaces(): Interfaces
118
    {
119 5
        $interfaces = \array_filter(
120 5
            $this->element->getClasses(),
121 5
            static fn (ClassLike $element): bool => $element instanceof InterfaceType
122 5
        );
123
124 5
        return new Interfaces(\array_map(
125 5
            static fn (InterfaceType $interface): InterfaceDeclaration => InterfaceDeclaration::fromElement($interface),
126 5
            $interfaces
127 5
        ));
128
    }
129
130 1
    public function addTrait(string $name): TraitDeclaration
131
    {
132 1
        return TraitDeclaration::fromElement($this->element->addTrait($name));
133
    }
134
135 5
    public function getTraits(): Traits
136
    {
137 5
        $traits = \array_filter(
138 5
            $this->element->getClasses(),
139 5
            static fn (ClassLike $element): bool => $element instanceof TraitType
140 5
        );
141
142 5
        return new Traits(\array_map(
143 5
            static fn (TraitType $trait): TraitDeclaration => TraitDeclaration::fromElement($trait),
144 5
            $traits
145 5
        ));
146
    }
147
148 1
    public function addEnum(string $name): EnumDeclaration
149
    {
150 1
        return EnumDeclaration::fromElement($this->element->addEnum($name));
151
    }
152
153 5
    public function getEnums(): Enums
154
    {
155 5
        $enums = \array_filter(
156 5
            $this->element->getClasses(),
157 5
            static fn (ClassLike $element): bool => $element instanceof EnumType
158 5
        );
159
160 5
        return new Enums(\array_map(
161 5
            static fn (EnumType $enum): EnumDeclaration => EnumDeclaration::fromElement($enum),
162 5
            $enums
163 5
        ));
164
    }
165
166 6
    public function getElements(): Elements
167
    {
168 6
        return new Elements(\array_map(
169 6
            static fn (ClassLike $element) => match (true) {
170 6
                $element instanceof ClassType => ClassDeclaration::fromElement($element),
171 6
                $element instanceof InterfaceType => InterfaceDeclaration::fromElement($element),
172 6
                $element instanceof TraitType => TraitDeclaration::fromElement($element),
173 6
                $element instanceof EnumType => EnumDeclaration::fromElement($element)
174 6
            },
175 6
            $this->element->getClasses()
176 6
        ));
177
    }
178
179
    /**
180
     * @deprecated since v3.5.
181
     * @see PhpNamespace::removeElement
182
     */
183
    public function removeClass(string $name): self
184
    {
185
        return $this->removeElement($name);
186
    }
187
188 1
    public function removeElement(string $name): self
189
    {
190 1
        $this->element->removeClass($name);
191
192 1
        return $this;
193
    }
194
195
    /**
196
     * @internal
197
     */
198 3
    public static function fromElement(NettePhpNamespace $element): self
199
    {
200 3
        $namespace = new self($element->getName());
201
202 3
        $namespace->element = $element;
203
204 3
        return $namespace;
205
    }
206
207
    /**
208
     * @internal
209
     */
210 17
    public function getElement(): NettePhpNamespace
211
    {
212 17
        return $this->element;
213
    }
214
}
215