Completed
Push — master ( a943ea...15df77 )
by Igor
01:36
created

ClassAwareGenerator::generateNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
nc 2
cc 2
nop 0
crap 2
1
<?php
2
3
namespace Leaditin\Code\Generator;
4
5
use Leaditin\Code\DocBlock;
6
use Leaditin\Code\Member\Constant;
7
use Leaditin\Code\Member\Method;
8
use Leaditin\Code\Member\Property;
9
10
/**
11
 * @package Leaditin\Code
12
 * @author Igor Vuckovic <[email protected]>
13
 * @license MIT
14
 */
15
abstract class ClassAwareGenerator extends Generator
16
{
17
    /**
18
     * @var ConstantGenerator
19
     */
20
    protected $constantGenerator;
21
22
    /**
23
     * @var DocBlockGenerator
24
     */
25
    protected $docBlockGenerator;
26
27
    /**
28
     * @var MethodGenerator
29
     */
30
    protected $methodGenerator;
31
32
    /**
33
     * @var PropertyGenerator
34
     */
35
    protected $propertyGenerator;
36
37
    /**
38
     * @var string
39
     */
40
    protected $name;
41
42
    /**
43
     * @var null|string
44
     */
45
    protected $namespace;
46
47
    /**
48
     * @var null|string
49
     */
50
    protected $extends;
51
52
    /**
53
     * @var null|DocBlock
54
     */
55
    protected $docBlock;
56
57
    /**
58
     * @var Constant[]
59
     */
60
    protected $constants = [];
61
62
    /**
63
     * @var Property[]
64
     */
65
    protected $properties = [];
66
67
    /**
68
     * @var Method[]
69
     */
70
    protected $methods = [];
71
72
    /**
73
     * @param ConstantGenerator $constantGenerator
74
     * @param DocBlockGenerator $docBlockGenerator
75
     * @param MethodGenerator $methodGenerator
76
     * @param PropertyGenerator $propertyGenerator
77
     */
78 23
    public function __construct(
79
        ConstantGenerator $constantGenerator,
80
        DocBlockGenerator $docBlockGenerator,
81
        MethodGenerator $methodGenerator,
82
        PropertyGenerator $propertyGenerator
83
    ) {
84 23
        $this->constantGenerator = $constantGenerator;
85 23
        $this->docBlockGenerator = $docBlockGenerator;
86 23
        $this->methodGenerator = $methodGenerator;
87 23
        $this->propertyGenerator = $propertyGenerator;
88 23
    }
89
90
    /**
91
     * @param string $name
92
     *
93
     * @return static
94
     */
95 17
    public function setName(string $name): self
96
    {
97 17
        $this->name = $name;
98
99 17
        return $this;
100
    }
101
102
    /**
103
     * @param null|string $namespace
104
     *
105
     * @return static
106
     */
107 8
    public function setNamespace(?string $namespace): self
108
    {
109 8
        $this->namespace = $namespace;
110
111 8
        return $this;
112
    }
113
114
    /**
115
     * @param null|string $extends
116
     *
117
     * @return static
118
     */
119 8
    public function setExtends(?string $extends): self
120
    {
121 8
        $this->extends = $extends;
122
123 8
        return $this;
124
    }
125
126
    /**
127
     * @param null|DocBlock $docBlock
128
     *
129
     * @return static
130
     */
131 4
    public function setDocBlock(?DocBlock $docBlock): self
132
    {
133 4
        $this->docBlock = $docBlock;
134
135 4
        return $this;
136
    }
137
138
    /**
139
     * @param Constant $constant
140
     *
141
     * @return static
142
     */
143 7
    public function addConstant(Constant $constant): self
144
    {
145 7
        $this->constants[$constant->name()] = $constant;
146
147 7
        return $this;
148
    }
149
150
    /**
151
     * @param Property $property
152
     *
153
     * @return static
154
     */
155 5
    public function addProperty(Property $property): self
156
    {
157 5
        $this->properties[$property->name()] = $property;
158
159 5
        return $this;
160
    }
161
162
    /**
163
     * @param Method $method
164
     *
165
     * @return static
166
     */
167 8
    public function addMethod(Method $method): self
168
    {
169 8
        $method->setScope($this->scope());
170 8
        $this->methods[$method->name()] = $method;
171
172 8
        return $this;
173
    }
174
175
    /**
176
     * @return string
177
     */
178 16
    public function generate(): string
179
    {
180 16
        return $this->generateHead()
181 16
            . $this->generateConstants()
182 16
            . $this->generateProperties()
183 16
            . $this->generateMethods()
184 16
            . $this->generateFoot();
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    abstract protected function scope(): string;
191
192
    /**
193
     * @return string
194
     */
195
    abstract protected function generateScope(): string;
196
197
    /**
198
     * @return string
199
     */
200 16
    protected function generateHead(): string
201
    {
202 16
        $output = $this->generateLine('<?php', null, 1);
203 16
        $output .= $this->generateNamespace();
204 16
        $output .= $this->generateDocBlock();
205 16
        $output .= $this->generateScope();
206
207 16
        return $output;
208
    }
209
210
    /**
211
     * @return string
212
     */
213 16
    protected function generateNamespace(): string
214
    {
215 16
        if ($this->namespace === null) {
216 9
            return '';
217
        }
218
219 7
        return $this->generateLine('namespace ' . ltrim($this->namespace, '\\') . ';', null, 1);
220
    }
221
222
    /**
223
     * @return string
224
     */
225 16
    protected function generateDocBlock(): string
226
    {
227 16
        if ($this->docBlock === null) {
228 13
            return '';
229
        }
230
231 3
        return $this->generateLine($this->docBlockGenerator->generate($this->docBlock));
232
    }
233
234
    /**
235
     * @return string
236
     */
237 16
    protected function generateInheritance(): string
238
    {
239 16
        if ($this->extends === null) {
240 9
            return '';
241
        }
242
243 7
        return ' extends \\' . ltrim($this->extends, '\\');
244
    }
245
246
    /**
247
     * @return string
248
     */
249 16
    protected function generateFoot(): string
250
    {
251 16
        return $this->generateLine('}');
252
    }
253
254
    /**
255
     * @return string
256
     */
257 16
    protected function generateConstants(): string
258
    {
259 16
        $lines = [];
260
261 16
        foreach ($this->constants as $constant) {
262 6
            $lines[] = $this->generateLine($this->constantGenerator->generate($constant));
263
        }
264
265 16
        return implode('', $lines);
266
    }
267
268
    /**
269
     * @return string
270
     */
271 12
    protected function generateProperties(): string
272
    {
273 12
        $output = '';
274
275 12
        if ($this->constants !== [] && ($this->properties !== [] || $this->methods !== [])) {
276 2
            $output .= $this->generateLine('', 0);
277
        }
278
279 12
        $lines = [];
280 12
        foreach ($this->properties as $property) {
281 4
            $lines[] = $this->generateLine($this->propertyGenerator->generate($property));
282
        }
283
284 12
        $output .= implode($this->endOfLine, $lines);
285
286 12
        return $output;
287
    }
288
289
    /**
290
     * @return string
291
     */
292 16
    protected function generateMethods(): string
293
    {
294 16
        $output = '';
295
296 16
        if ($this->methods !== [] && ($this->constants !== [] || $this->properties !== [])) {
297 3
            $output .= $this->generateLine('', 0);
298
        }
299
300 16
        $lines = [];
301 16
        foreach ($this->methods as $method) {
302 7
            $lines[] = $this->generateLine($this->methodGenerator->generate($method));
303
        }
304
305 16
        $output .= implode($this->endOfLine, $lines);
306
307 16
        return $output;
308
    }
309
}
310