Completed
Push — master ( d47fd7...10fc86 )
by Igor
02:26
created

ClassAwareGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 21
    public function __construct(
79
        ConstantGenerator $constantGenerator,
80
        DocBlockGenerator $docBlockGenerator,
81
        MethodGenerator $methodGenerator,
82
        PropertyGenerator $propertyGenerator
83
    ) {
84 21
        $this->constantGenerator = $constantGenerator;
85 21
        $this->docBlockGenerator = $docBlockGenerator;
86 21
        $this->methodGenerator = $methodGenerator;
87 21
        $this->propertyGenerator = $propertyGenerator;
88 21
    }
89
90
    /**
91
     * @param string $name
92
     *
93
     * @return static
94
     */
95 15
    public function setName(string $name): self
96
    {
97 15
        $this->name = $name;
98
99 15
        return $this;
100
    }
101
102
    /**
103
     * @param null|string $namespace
104
     *
105
     * @return static
106
     */
107 4
    public function setNamespace(?string $namespace): self
108
    {
109 4
        $this->namespace = $namespace;
110
111 4
        return $this;
112
    }
113
114
    /**
115
     * @param null|string $extends
116
     *
117
     * @return static
118
     */
119 4
    public function setExtends(?string $extends): self
120
    {
121 4
        $this->extends = $extends;
122
123 4
        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 7
    public function addMethod(Method $method): self
168
    {
169 7
        $method->setScope($this->getScope());
170 7
        $this->methods[$method->name()] = $method;
171
172 7
        return $this;
173
    }
174
175
    /**
176
     * @return string
177
     */
178 14
    public function generate(): string
179
    {
180 14
        return $this->generateHead()
181 14
            . $this->generateConstants()
182 14
            . $this->generateProperties()
183 14
            . $this->generateMethods()
184 14
            . $this->generateFoot();
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    abstract protected function getScope(): string;
191
192
    /**
193
     * @return string
194
     */
195 14
    protected function generateHead(): string
196
    {
197 14
        $output = $this->generateLine('<?php', null, 1);
198
199 14
        if ($this->namespace !== null) {
200 3
            $output .= $this->generateLine('namespace ' . ltrim($this->namespace, '\\') . ';', null, 1);
201
        }
202
203 14
        if ($this->docBlock !== null) {
204 3
            $output .= $this->generateLine($this->docBlockGenerator->generate($this->docBlock));
205
        }
206
207 14
        $output .= $this->generateLine($this->getScope() . ' ' . $this->name . ($this->extends !== null ? ' extends \\' . ltrim($this->extends, '\\') : ''));
208 14
        $output .= $this->generateLine('{');
209
210 14
        return $output;
211
    }
212
213
    /**
214
     * @return string
215
     */
216 14
    protected function generateFoot(): string
217
    {
218 14
        return $this->generateLine('}');
219
    }
220
221
    /**
222
     * @return string
223
     */
224 14
    protected function generateConstants(): string
225
    {
226 14
        $lines = [];
227
228 14
        foreach ($this->constants as $constant) {
229 6
            $lines[] = $this->generateLine($this->constantGenerator->generate($constant));
230
        }
231
232 14
        return implode('', $lines);
233
    }
234
235
    /**
236
     * @return string
237
     */
238 10
    protected function generateProperties(): string
239
    {
240 10
        $output = '';
241
242 10
        if ($this->constants !== [] && ($this->properties !== [] || $this->methods !== [])) {
243 2
            $output .= $this->generateLine('', 0);
244
        }
245
246 10
        $lines = [];
247 10
        foreach ($this->properties as $property) {
248 4
            $lines[] = $this->generateLine($this->propertyGenerator->generate($property));
249
        }
250
251 10
        $output .= implode($this->endOfLine, $lines);
252
253 10
        return $output;
254
    }
255
256
    /**
257
     * @return string
258
     */
259 14
    protected function generateMethods(): string
260
    {
261 14
        $output = '';
262
263 14
        if ($this->methods !== [] && ($this->constants !== [] || $this->properties !== [])) {
264 3
            $output .= $this->generateLine('', 0);
265
        }
266
267 14
        $lines = [];
268 14
        foreach ($this->methods as $method) {
269 6
            $lines[] = $this->generateLine($this->methodGenerator->generate($method));
270
        }
271
272 14
        $output .= implode($this->endOfLine, $lines);
273
274 14
        return $output;
275
    }
276
}
277