1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 03.09.16 at 09:58 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonphp\generator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class generator class. |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ClassGenerator extends AbstractGenerator |
14
|
|
|
{ |
15
|
|
|
use AbstractFinalTrait; |
16
|
|
|
|
17
|
|
|
/** OOP public visibility */ |
18
|
|
|
const VISIBILITY_PUBLIC = 'public'; |
19
|
|
|
|
20
|
|
|
/** OOP protected visibility */ |
21
|
|
|
const VISIBILITY_PROTECTED = 'protected'; |
22
|
|
|
|
23
|
|
|
/** OOP private visibility */ |
24
|
|
|
const VISIBILITY_PRIVATE = 'private'; |
25
|
|
|
|
26
|
|
|
/** @var string Class name */ |
27
|
|
|
protected $className; |
28
|
|
|
|
29
|
|
|
/** @var string Class namespace */ |
30
|
|
|
protected $namespace; |
31
|
|
|
|
32
|
|
|
/** @var array Collection of class uses */ |
33
|
|
|
protected $uses = []; |
34
|
|
|
|
35
|
|
|
/** @var string Multiline file description */ |
36
|
|
|
protected $fileDescription; |
37
|
|
|
|
38
|
|
|
/** @var array Class constants */ |
39
|
|
|
protected $constants; |
40
|
|
|
|
41
|
|
|
/** @var array Class static properties */ |
42
|
|
|
protected $staticProperties; |
43
|
|
|
|
44
|
|
|
/** @var array Class static methods */ |
45
|
|
|
protected $staticMethods; |
46
|
|
|
|
47
|
|
|
/** @var array Class properties */ |
48
|
|
|
protected $properties; |
49
|
|
|
|
50
|
|
|
/** @var array Class methods */ |
51
|
|
|
protected $methods; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* ClassGenerator constructor. |
55
|
|
|
* |
56
|
|
|
* @param string $className Class name |
57
|
|
|
* @param GenericGenerator $parent Parent generator |
58
|
|
|
*/ |
59
|
|
|
public function __construct(string $className, GenericGenerator $parent = null) |
60
|
|
|
{ |
61
|
|
|
$this->className = $className; |
62
|
|
|
|
63
|
|
|
parent::__construct($parent); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set class file description. |
68
|
|
|
* |
69
|
|
|
* @param array $description Collection of class file description lines |
70
|
|
|
* |
71
|
|
|
* @return ClassGenerator |
72
|
|
|
*/ |
73
|
|
|
public function defDescription(array $description) : ClassGenerator |
74
|
|
|
{ |
75
|
|
|
$commentsGenerator = new CommentsGenerator($this); |
76
|
|
|
foreach ($description as $line) { |
77
|
|
|
$commentsGenerator->defLine($line); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->fileDescription = $commentsGenerator->code(); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set class namespace. |
87
|
|
|
* |
88
|
|
|
* @param string $namespace |
89
|
|
|
* |
90
|
|
|
* @return ClassGenerator |
91
|
|
|
*/ |
92
|
|
|
public function defNamespace(string $namespace) : ClassGenerator |
93
|
|
|
{ |
94
|
|
|
$this->namespace = $namespace; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set class use. |
101
|
|
|
* |
102
|
|
|
* @param string $use |
103
|
|
|
* |
104
|
|
|
* @return ClassGenerator |
105
|
|
|
*/ |
106
|
|
|
public function defUse(string $use) : ClassGenerator |
107
|
|
|
{ |
108
|
|
|
$this->uses[] = $use; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set protected class property. |
115
|
|
|
* |
116
|
|
|
* @param string $name Property name |
117
|
|
|
* @param string $type Property type |
118
|
|
|
* @param mixed $value Property value |
119
|
|
|
* @param string $description Property description |
120
|
|
|
* |
121
|
|
|
* @return PropertyGenerator |
122
|
|
|
*/ |
123
|
|
|
public function defProtectedProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
124
|
|
|
{ |
125
|
|
|
return $this->defProperty($name, $type, $value, $description)->defProtected(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set class property. |
130
|
|
|
* |
131
|
|
|
* @param string $name Property name |
132
|
|
|
* @param string $type Property type |
133
|
|
|
* @param mixed $value Property value |
134
|
|
|
* @param string $description Property description |
135
|
|
|
* |
136
|
|
|
* @return PropertyGenerator |
137
|
|
|
*/ |
138
|
|
|
public function defProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
139
|
|
|
{ |
140
|
|
|
return (new PropertyGenerator($name, $value, $this)) |
141
|
|
|
->increaseIndentation() |
142
|
|
|
->defComment() |
143
|
|
|
->defVar($type, $description) |
144
|
|
|
->end(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set protected static class property. |
149
|
|
|
* |
150
|
|
|
* @param string $name Property name |
151
|
|
|
* @param string $type Property type |
152
|
|
|
* @param mixed $value Property value |
153
|
|
|
* @param string $description Property description |
154
|
|
|
* |
155
|
|
|
* @return PropertyGenerator |
156
|
|
|
*/ |
157
|
|
|
public function defProtectedStaticProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
158
|
|
|
{ |
159
|
|
|
return $this->defStaticProperty($name, $type, $value, $description)->defProtected(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set static class property. |
164
|
|
|
* |
165
|
|
|
* @param string $name Property name |
166
|
|
|
* @param string $type Property type |
167
|
|
|
* @param mixed $value Property value |
168
|
|
|
* @param string $description Property description |
169
|
|
|
* |
170
|
|
|
* @return PropertyGenerator |
171
|
|
|
*/ |
172
|
|
|
public function defStaticProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
173
|
|
|
{ |
174
|
|
|
return $this->defProperty($name, $type, $value, $description)->defStatic(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set protected class method. |
179
|
|
|
* |
180
|
|
|
* @param string $name Method name |
181
|
|
|
* |
182
|
|
|
* @return MethodGenerator |
183
|
|
|
*/ |
184
|
|
|
public function defProtectedMethod(string $name) : MethodGenerator |
185
|
|
|
{ |
186
|
|
|
return $this->defMethod($name); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set public class method. |
191
|
|
|
* |
192
|
|
|
* @param string $name Method name |
193
|
|
|
* |
194
|
|
|
* @return MethodGenerator |
195
|
|
|
*/ |
196
|
|
|
public function defMethod(string $name) : MethodGenerator |
197
|
|
|
{ |
198
|
|
|
return new MethodGenerator($name, $this); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set protected static class method. |
203
|
|
|
* |
204
|
|
|
* @param string $name Method name |
205
|
|
|
* |
206
|
|
|
* @return MethodGenerator |
207
|
|
|
*/ |
208
|
|
|
public function defProtectedStaticMethod(string $name) : MethodGenerator |
209
|
|
|
{ |
210
|
|
|
return $this->defStaticMethod($name)->defProtected(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Set public static class method. |
215
|
|
|
* |
216
|
|
|
* @param string $name Method name |
217
|
|
|
* |
218
|
|
|
* @return MethodGenerator |
219
|
|
|
*/ |
220
|
|
|
public function defStaticMethod(string $name) : MethodGenerator |
221
|
|
|
{ |
222
|
|
|
return $this->defMethod($name)->defStatic(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
* @throws \InvalidArgumentException |
228
|
|
|
*/ |
229
|
|
|
public function code(int $indentation = 0) : string |
230
|
|
|
{ |
231
|
|
|
if ($this->namespace === null) { |
232
|
|
|
throw new \InvalidArgumentException('Class namespace should be defined'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$formattedCode = ['namespace ' . $this->namespace . ';']; |
236
|
|
|
|
237
|
|
|
// One empty line after namespace |
238
|
|
|
$formattedCode[] = ''; |
239
|
|
|
|
240
|
|
|
// Add uses |
241
|
|
|
foreach ($this->uses as $use) { |
242
|
|
|
$formattedCode[] = 'use ' . $use . ';'; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// One empty line after uses if we have them |
246
|
|
|
if (count($this->uses)) { |
247
|
|
|
$formattedCode[] = ''; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Add comments |
251
|
|
|
if (array_key_exists(CommentsGenerator::class, $this->generatedCode)) { |
252
|
|
|
$formattedCode[] = $this->generatedCode[CommentsGenerator::class]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// Add previously generated code |
256
|
|
|
$formattedCode[] = $this->buildDefinition(); |
257
|
|
|
$formattedCode[] = '{'; |
258
|
|
|
|
259
|
|
|
// Prepend file description if present |
260
|
|
|
if ($this->fileDescription !== null) { |
261
|
|
|
array_unshift($formattedCode, $this->fileDescription); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// Add properties |
265
|
|
|
if (array_key_exists(PropertyGenerator::class, $this->generatedCode)) { |
266
|
|
|
$formattedCode[] = $this->indentation($indentation + 1) . $this->generatedCode[PropertyGenerator::class]; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$formattedCode[] = '}'; |
270
|
|
|
|
271
|
|
|
$code = implode("\n" . $this->indentation($indentation), $formattedCode); |
272
|
|
|
|
273
|
|
|
return $code; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Build function definition. |
278
|
|
|
* |
279
|
|
|
* @return string Function definition |
280
|
|
|
*/ |
281
|
|
|
protected function buildDefinition() |
282
|
|
|
{ |
283
|
|
|
return ($this->isFinal ? 'final ' : '') . |
284
|
|
|
($this->isAbstract ? 'abstract ' : '') . |
285
|
|
|
'class ' . |
286
|
|
|
$this->className; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: