Completed
Push — master ( a0d4dc...a17c0a )
by Igor
02:03
created

ClassAwareGenerator::generateHead()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
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 12
    public function __construct(
79
        ConstantGenerator $constantGenerator,
80
        DocBlockGenerator $docBlockGenerator,
81
        MethodGenerator $methodGenerator,
82
        PropertyGenerator $propertyGenerator
83
    ) {
84 12
        $this->constantGenerator = $constantGenerator;
85 12
        $this->docBlockGenerator = $docBlockGenerator;
86 12
        $this->methodGenerator = $methodGenerator;
87 12
        $this->propertyGenerator = $propertyGenerator;
88 12
    }
89
90
    /**
91
     * @param string $name
92
     *
93
     * @return static
94
     */
95 6
    public function setName(string $name): self
96
    {
97 6
        $this->name = $name;
98
99 6
        return $this;
100
    }
101
102
    /**
103
     * @param null|string $namespace
104
     *
105
     * @return static
106
     */
107 2
    public function setNamespace(?string $namespace): self
108
    {
109 2
        $this->namespace = $namespace;
110
111 2
        return $this;
112
    }
113
114
    /**
115
     * @param null|string $extends
116
     *
117
     * @return static
118
     */
119 2
    public function setExtends(?string $extends): self
120
    {
121 2
        $this->extends = $extends;
122
123 2
        return $this;
124
    }
125
126
    /**
127
     * @param null|DocBlock $docBlock
128
     *
129
     * @return static
130
     */
131 2
    public function setDocBlock(?DocBlock $docBlock): self
132
    {
133 2
        $this->docBlock = $docBlock;
134
135 2
        return $this;
136
    }
137
138
    /**
139
     * @param Constant $constant
140
     *
141
     * @return static
142
     */
143 3
    public function addConstant(Constant $constant): self
144
    {
145 3
        $this->constants[$constant->name()] = $constant;
146
147 3
        return $this;
148
    }
149
150
    /**
151
     * @param Property $property
152
     *
153
     * @return static
154
     */
155 3
    public function addProperty(Property $property): self
156
    {
157 3
        $this->properties[$property->name()] = $property;
158
159 3
        return $this;
160
    }
161
162
    /**
163
     * @param Method $method
164
     *
165
     * @return static
166
     */
167 3
    public function addMethod(Method $method): self
168
    {
169 3
        $this->methods[$method->name()] = $method;
170
171 3
        return $this;
172
    }
173
174
    /**
175
     * @return string
176
     */
177 5
    protected function generateHead(): string
178
    {
179 5
        $output = $this->generateLine('<?php', null, 1);
180
181 5
        if ($this->namespace !== null) {
182 1
            $output .= $this->generateLine('namespace ' . ltrim($this->namespace, '\\') . ';', null, 1);
183
        }
184
185 5
        if ($this->docBlock !== null) {
186 1
            $output .= $this->generateLine($this->docBlockGenerator->generate($this->docBlock));
187
        }
188
189 5
        return $output;
190
    }
191
192
    /**
193
     * @return string
194
     */
195 5
    protected function generateFoot(): string
196
    {
197 5
        return $this->generateLine('}');
198
    }
199
200
    /**
201
     * @return string
202
     */
203 5
    protected function generateConstants(): string
204
    {
205 5
        $lines = [];
206
207 5
        foreach ($this->constants as $constant) {
208 2
            $lines[] = $this->generateLine($this->constantGenerator->generate($constant));
209
        }
210
211 5
        return implode('', $lines);
212
    }
213
214
    /**
215
     * @return string
216
     */
217 5
    protected function generateProperties(): string
218
    {
219 5
        $output = '';
220
221 5
        if ($this->constants !== [] && ($this->properties !== [] || $this->methods !== [])) {
222 1
            $output .= $this->generateLine('', 0);
223
        }
224
225 5
        $lines = [];
226 5
        foreach ($this->properties as $property) {
227 2
            $lines[] = $this->generateLine($this->propertyGenerator->generate($property));
228
        }
229
230 5
        $output .= implode($this->endOfLine, $lines);
231
232 5
        return $output;
233
    }
234
235
    /**
236
     * @return string
237
     */
238 5
    protected function generateMethods(): string
239
    {
240 5
        $output = '';
241
242 5
        if ($this->properties !== [] && $this->methods !== []) {
243 1
            $output .= $this->generateLine('', 0);
244
        }
245
246 5
        $lines = [];
247 5
        foreach ($this->methods as $method) {
248 2
            $lines[] = $this->generateLine($this->methodGenerator->generate($method));
249
        }
250
251 5
        $output .= implode($this->endOfLine, $lines);
252
253 5
        return $output;
254
    }
255
}
256