Passed
Branch master (da5350)
by Timur
02:19
created

PhpFile::addComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Murtukov\PHPCodeGenerator;
6
7
use function dirname;
8
use function file_put_contents;
9
use function implode;
10
use function ksort;
11
use function mkdir;
12
13
class PhpFile extends DependencyAwareGenerator
14
{
15
    protected string   $namespace = '';
16
    protected string   $name;
17
    protected ?Comment $comment;
18
19
    /** @var PhpClass[] */
20
    protected array $classes = [];
21
22
    /** @var string[] */
23
    protected array $declares = [];
24
25
    public function __construct(string $name = '')
26
    {
27
        $this->name = $name;
28
        $this->dependencyAwareChildren = [&$this->classes];
29
    }
30
31
    public static function new(string $name = ''): self
32
    {
33
        return new self($name);
34
    }
35
36
    public function generate(): string
37
    {
38
        $namespace = $this->namespace ? "\nnamespace $this->namespace;\n" : '';
39
        $classes = implode("\n\n", $this->classes);
40
41
        return <<<CODE
42
        <?php
43
        $namespace{$this->buildUseStatements()}
44
        $classes
45
        CODE;
46
    }
47
48
    public function __toString(): string
49
    {
50
        return $this->generate();
51
    }
52
53
    public function addClass(PhpClass $class): self
54
    {
55
        $this->classes[] = $class;
56
57
        return $this;
58
    }
59
60
    public function createClass(string $name): PhpClass
61
    {
62
        return $this->classes[] = PhpClass::new($name);
63
    }
64
65
    public function removeClass(string $name): self
66
    {
67
        foreach ($this->classes as $key => $class) {
68
            if ($class->name === $name) {
69
                unset($this->classes[$key]);
70
            }
71
        }
72
73
        return $this;
74
    }
75
76
    public function getClass(string $name): ?PhpClass
77
    {
78
        foreach ($this->classes as $key => $class) {
79
            if ($class->name === $name) {
80
                return $this->classes[$key];
81
            }
82
        }
83
84
        return null;
85
    }
86
87
    public function getNamespace(): string
88
    {
89
        return $this->namespace;
90
    }
91
92
    public function setNamespace(string $namespace): self
93
    {
94
        $this->namespace = $namespace;
95
96
        return $this;
97
    }
98
99
    private function buildUseStatements(): string
100
    {
101
        $code = '';
102
103
        $paths = $this->getUsePaths();
104
105
        if (empty($paths)) {
106
            return $code;
107
        }
108
109
        if (!empty(ksort($paths))) {
110
            $code = "\n";
111
112
            foreach ($paths as $path => $aliases) {
113
                $code .= "use $path";
114
115
                if ($aliases) {
116
                    $code .= " as $aliases";
117
                }
118
119
                $code .= ";\n";
120
            }
121
        }
122
123
        return $code;
124
    }
125
126
    public function getComment(): Comment
127
    {
128
        return $this->comment;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->comment could return the type null which is incompatible with the type-hinted return Murtukov\PHPCodeGenerator\Comment. Consider adding an additional type-check to rule them out.
Loading history...
129
    }
130
131
    public function createComment(string $text): Comment
132
    {
133
        return $this->comment = Comment::block($text);
134
    }
135
136
    public function setComment(string $text): self
137
    {
138
        $this->comment = Comment::block($text);
139
140
        return $this;
141
    }
142
143
    public function removeComment(): self
144
    {
145
        $this->comment = null;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @return false|int
152
     */
153
    public function save(string $path, int $mask = 0775)
154
    {
155
        $dir = dirname($path);
156
157
        if (!is_dir($dir)) {
158
            mkdir($dir, $mask, true);
159
        }
160
161
        return file_put_contents($path, $this);
162
    }
163
}
164