Passed
Pull Request — master (#1005)
by Maxim
10:03
created

FileDeclaration::fromReflection()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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 51
    public function __construct()
36
    {
37 51
        $this->element = new PhpFile();
38 51
        $this->element->setStrictTypes(true);
39
    }
40
41 4
    public function __toString(): string
42
    {
43 4
        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 1
    public static function fromReflection(\ReflectionClass $reflection): static
52
    {
53 1
        return self::fromElement((new Factory())->fromCode(\file_get_contents($reflection->getFileName())));
54
    }
55
56 36
    public function addNamespace(string|PhpNamespace $namespace): PhpNamespace
57
    {
58 36
        if ($namespace instanceof PhpNamespace) {
59 35
            $this->element->addNamespace($namespace->getElement());
60
61 35
            return $namespace;
62
        }
63
64 2
        return PhpNamespace::fromElement($this->element->addNamespace($namespace));
65
    }
66
67 1
    public function addFunction(string $name): FunctionDeclaration
68
    {
69 1
        return FunctionDeclaration::fromElement($this->element->addFunction($name));
70
    }
71
72 1
    public function getNamespaces(): Namespaces
73
    {
74 1
        return new Namespaces(\array_map(
75 1
            static fn (NettePhpNamespace $namespace) => PhpNamespace::fromElement($namespace),
76 1
            $this->element->getNamespaces()
77 1
        ));
78
    }
79
80 1
    public function getFunctions(): Functions
81
    {
82 1
        return new Functions(\array_map(
83 1
            static fn (GlobalFunction $function) => FunctionDeclaration::fromElement($function),
84 1
            $this->element->getFunctions()
85 1
        ));
86
    }
87
88 1
    public function addUse(string $name, ?string $alias = null, string $of = NettePhpNamespace::NameNormal): static
89
    {
90 1
        $this->element->addUse($name, $alias, $of);
91
92 1
        return $this;
93
    }
94
95
    /**
96
     * Adds declare(strict_types=1) to output.
97
     */
98 1
    public function setStrictTypes(bool $on = true): static
99
    {
100 1
        $this->element->setStrictTypes($on);
101
102 1
        return $this;
103
    }
104
105 1
    public function hasStrictTypes(): bool
106
    {
107 1
        return $this->element->hasStrictTypes();
108
    }
109
110 6
    public function getElements(): Elements
111
    {
112 6
        return new Elements(\array_map(
113 6
            static fn (ClassLike $element) => match (true) {
114 6
                $element instanceof ClassType => ClassDeclaration::fromElement($element),
115 6
                $element instanceof InterfaceType => InterfaceDeclaration::fromElement($element),
116 6
                $element instanceof TraitType => TraitDeclaration::fromElement($element),
117 6
                $element instanceof EnumType => EnumDeclaration::fromElement($element)
118 6
            },
119 6
            $this->element->getClasses()
120 6
        ));
121
    }
122
123
    /**
124
     * @internal
125
     */
126 3
    public static function fromElement(PhpFile $element): static
127
    {
128 3
        $file = new static();
129
130 3
        $file->element = $element;
131
132 3
        return $file;
133
    }
134
135
    /**
136
     * @internal
137
     */
138 37
    public function getElement(): PhpFile
139
    {
140 37
        return $this->element;
141
    }
142
143 3
    public function render(): string
144
    {
145 3
        return $this->__toString();
146
    }
147
}
148