1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2014-04-24 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\CodeGenerator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DocumentationGenerator |
11
|
|
|
* @package Net\Bazzline\Component\Locator\LocatorGenerator\Generator |
12
|
|
|
* @see http://www.phpdoc.org/docs/latest/index.html |
13
|
|
|
* @todo implement all tags from phpdoc |
14
|
|
|
*/ |
15
|
|
|
class DocumentationGenerator extends AbstractGenerator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var boolean |
19
|
|
|
* @todo see if it is needed or of phpdoc says |
20
|
|
|
*/ |
21
|
|
|
private $addEmptyLine; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $comment |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
|
|
public function addComment($comment) |
28
|
|
|
{ |
29
|
|
|
$this->addGeneratorProperty('comments', (string) $comment, true); |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $className |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function setClass($className) |
39
|
|
|
{ |
40
|
|
|
$this->addGeneratorProperty('class', (string) $className, false); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $interfaceName |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setInterface($interfaceName) |
50
|
|
|
{ |
51
|
|
|
$this->addGeneratorProperty('interface', (string) $interfaceName, false); |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $package |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setPackage($package) |
61
|
|
|
{ |
62
|
|
|
$this->addGeneratorProperty('package', (string) $package, false); |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @param array $typeHints |
70
|
|
|
* @param string $comment |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function addParameter($name, $typeHints = [], $comment = '') |
74
|
|
|
{ |
75
|
|
|
$parameter = [ |
76
|
|
|
'comment' => (string) $comment, |
77
|
|
|
'name' => (string) $name, |
78
|
|
|
'type_hints' => (array) $typeHints |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$this->addGeneratorProperty('parameters', $parameter); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $typeHints |
88
|
|
|
* @param string $comment |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setReturn($typeHints, $comment = '') |
92
|
|
|
{ |
93
|
|
|
$return = [ |
94
|
|
|
'comment' => (string) $comment, |
95
|
|
|
'type_hints' => (array) $typeHints |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$this->addGeneratorProperty('return', $return, false); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $see |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function addSee($see) |
108
|
|
|
{ |
109
|
|
|
$this->addGeneratorProperty('sees', (string) $see); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $exception |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function addThrows($exception) |
119
|
|
|
{ |
120
|
|
|
$this->addGeneratorProperty('throws', (string) $exception); |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $toDo |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function addTodoS($toDo) |
130
|
|
|
{ |
131
|
|
|
$this->addGeneratorProperty('todos', (string) $toDo); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $name |
138
|
|
|
* @param string $email |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function setAuthor($name, $email = '') |
142
|
|
|
{ |
143
|
|
|
$author = [ |
144
|
|
|
'email' => (string) $email, |
145
|
|
|
'name' => (string) $name |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
$this->addGeneratorProperty('author', $author, false); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $name |
155
|
|
|
* @param array $typeHints |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function setVariable($name, $typeHints = []) |
159
|
|
|
{ |
160
|
|
|
$variable = [ |
161
|
|
|
'name' => $name, |
162
|
|
|
'type_hints' => $typeHints |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
$this->addGeneratorProperty('variable', $variable, false); |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $number |
172
|
|
|
* @param string $description |
173
|
|
|
* @return $this |
174
|
|
|
* @see http://www.phpdoc.org/docs/latest/for-users/phpdoc/tags/version.html |
175
|
|
|
*/ |
176
|
|
|
public function setVersion($number, $description = '') |
177
|
|
|
{ |
178
|
|
|
$version = [ |
179
|
|
|
'description' => (string) $description, |
180
|
|
|
'number' => (string) $number |
181
|
|
|
]; |
182
|
|
|
$this->addGeneratorProperty('version', $version, false); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @throws InvalidArgumentException|RuntimeException |
189
|
|
|
* @return string |
190
|
|
|
* @todo implement exception throwing if mandatory parameter is missing |
191
|
|
|
*/ |
192
|
|
|
public function generate() |
193
|
|
|
{ |
194
|
|
|
if ($this->canBeGenerated()) { |
195
|
|
|
$this->addEmptyLine = false; |
196
|
|
|
$this->resetContent(); |
197
|
|
|
|
198
|
|
|
$this->addContent('/**'); |
199
|
|
|
$this->generateSees(); |
200
|
|
|
$this->generateComments(); |
201
|
|
|
$this->generateInterface(); |
202
|
|
|
$this->generateClass(); |
203
|
|
|
$this->generatePackage(); |
204
|
|
|
$this->generateToDoS(); |
205
|
|
|
$this->generateParameters(); |
206
|
|
|
$this->generateReturn(); |
207
|
|
|
$this->generateThrows(); |
208
|
|
|
$this->generateVariable(); |
209
|
|
|
$this->generateAuthor(); |
210
|
|
|
$this->generateVersion(); |
211
|
|
|
$this->addContent(' */'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this->generateStringFromContent(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
private function generateAuthor() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$author = $this->getGeneratorProperty('author'); |
220
|
|
|
|
221
|
|
|
if (is_array($author)) { |
222
|
|
|
if ($this->addEmptyLine) { |
223
|
|
|
$this->addContent(' *'); |
224
|
|
|
$this->addEmptyLine = false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$line = $this->getLineGenerator(' * @author ' . $author['name']); |
228
|
|
|
|
229
|
|
|
if (strlen($author['email']) > 0) { |
230
|
|
|
$line->add('<' . $author['email'] . '>'); |
231
|
|
|
} |
232
|
|
|
$this->addContent($line); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
private function generateClass() |
237
|
|
|
{ |
238
|
|
|
$class = $this->getGeneratorProperty('class'); |
239
|
|
|
|
240
|
|
|
if (is_string($class)) { |
241
|
|
|
if ($this->addEmptyLine) { |
242
|
|
|
$this->addContent(' *'); |
243
|
|
|
$this->addEmptyLine = false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$line = $this->getLineGenerator(' * Class ' . $class); |
247
|
|
|
$this->addContent($line); |
248
|
|
|
$this->addEmptyLine = true; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
private function generateInterface() |
253
|
|
|
{ |
254
|
|
|
$interface = $this->getGeneratorProperty('interface'); |
255
|
|
|
|
256
|
|
|
if (is_string($interface)) { |
257
|
|
|
if ($this->addEmptyLine) { |
258
|
|
|
$this->addContent(' *'); |
259
|
|
|
$this->addEmptyLine = false; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$line = $this->getLineGenerator(' * Interface ' . $interface); |
263
|
|
|
$this->addContent($line); |
264
|
|
|
$this->addEmptyLine = true; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
private function generatePackage() |
269
|
|
|
{ |
270
|
|
|
$package = $this->getGeneratorProperty('package'); |
271
|
|
|
|
272
|
|
|
if (is_string($package)) { |
273
|
|
|
if ($this->addEmptyLine) { |
274
|
|
|
$this->addContent(' *'); |
275
|
|
|
$this->addEmptyLine = false; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$line = $this->getLineGenerator(' * @package ' . $package); |
279
|
|
|
$this->addContent($line); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
View Code Duplication |
private function generateComments() |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
$comments = $this->getGeneratorProperty('comments'); |
286
|
|
|
|
287
|
|
|
if (is_array($comments)) { |
288
|
|
|
if ($this->addEmptyLine) { |
289
|
|
|
$this->addContent(' *'); |
290
|
|
|
$this->addEmptyLine = false; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
foreach ($comments as $comment) { |
294
|
|
|
$line = $this->getLineGenerator(' * ' . $comment); |
295
|
|
|
$this->addContent($line); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
private function generateParameters() |
301
|
|
|
{ |
302
|
|
|
$parameters = $this->getGeneratorProperty('parameters'); |
303
|
|
|
|
304
|
|
|
if (is_array($parameters)) { |
305
|
|
|
if ($this->addEmptyLine) { |
306
|
|
|
$this->addContent(' *'); |
307
|
|
|
$this->addEmptyLine = false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
foreach ($parameters as $parameter) { |
311
|
|
|
$line = $this->getLineGenerator(' * @param'); |
312
|
|
|
if (!empty($parameter['type_hints'])) { |
313
|
|
|
$line->add(implode('|', $parameter['type_hints'])); |
314
|
|
|
} |
315
|
|
|
$line->add('$' . $parameter['name']); |
316
|
|
|
if (strlen($parameter['comment']) > 0) { |
317
|
|
|
$line->add($parameter['comment']); |
318
|
|
|
} |
319
|
|
|
$this->addContent($line); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
View Code Duplication |
private function generateReturn() |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
$return = $this->getGeneratorProperty('return'); |
327
|
|
|
|
328
|
|
|
if (is_array($return)) { |
329
|
|
|
if ($this->addEmptyLine) { |
330
|
|
|
$this->addContent(' *'); |
331
|
|
|
$this->addEmptyLine = false; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
$line = $this->getLineGenerator(' * @return'); |
335
|
|
|
|
336
|
|
|
if (!empty($return['type_hints'])) { |
337
|
|
|
$line->add(implode('|', $return['type_hints'])); |
338
|
|
|
} |
339
|
|
|
if (strlen($return['comment']) > 0) { |
340
|
|
|
$line->add($return['comment']); |
341
|
|
|
} |
342
|
|
|
$this->addContent($line); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
View Code Duplication |
private function generateSees() |
|
|
|
|
347
|
|
|
{ |
348
|
|
|
$sees = $this->getGeneratorProperty('sees'); |
349
|
|
|
|
350
|
|
|
if (is_array($sees)) { |
351
|
|
|
if ($this->addEmptyLine) { |
352
|
|
|
$this->addContent(' *'); |
353
|
|
|
$this->addEmptyLine = false; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
foreach ($sees as $see) { |
357
|
|
|
$line = $this->getLineGenerator(' * @see ' . $see); |
358
|
|
|
$this->addContent($line); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
View Code Duplication |
private function generateThrows() |
|
|
|
|
364
|
|
|
{ |
365
|
|
|
$throws = $this->getGeneratorProperty('throws'); |
366
|
|
|
|
367
|
|
|
if (is_array($throws)) { |
368
|
|
|
if ($this->addEmptyLine) { |
369
|
|
|
$this->addContent(' *'); |
370
|
|
|
$this->addEmptyLine = false; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
$line = $this->getLineGenerator(' * @throws ' . implode('|', $throws)); |
374
|
|
|
$this->addContent($line); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
View Code Duplication |
private function generateToDoS() |
|
|
|
|
379
|
|
|
{ |
380
|
|
|
$todos = $this->getGeneratorProperty('todos'); |
381
|
|
|
|
382
|
|
|
if (is_array($todos)) { |
383
|
|
|
if ($this->addEmptyLine) { |
384
|
|
|
$this->addContent(' *'); |
385
|
|
|
$this->addEmptyLine = false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
foreach ($todos as $todo) { |
389
|
|
|
$line = $this->getLineGenerator(' * @todo ' . $todo); |
390
|
|
|
$this->addContent($line); |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
View Code Duplication |
private function generateVariable() |
|
|
|
|
396
|
|
|
{ |
397
|
|
|
$variable = $this->getGeneratorProperty('variable'); |
398
|
|
|
|
399
|
|
|
if (is_array($variable)) { |
400
|
|
|
if ($this->addEmptyLine) { |
401
|
|
|
$this->addContent(' *'); |
402
|
|
|
$this->addEmptyLine = false; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
$line = $this->getLineGenerator(' * @var'); |
406
|
|
|
if (!empty($variable['type_hints'])) { |
407
|
|
|
$line->add(implode('|', $variable['type_hints'])); |
408
|
|
|
} |
409
|
|
|
$line->add('$' . $variable['name']); |
410
|
|
|
$this->addContent($line); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
View Code Duplication |
private function generateVersion() |
|
|
|
|
415
|
|
|
{ |
416
|
|
|
$version = $this->getGeneratorProperty('version'); |
417
|
|
|
|
418
|
|
|
if (is_array($version)) { |
419
|
|
|
if ($this->addEmptyLine) { |
420
|
|
|
$this->addContent(' *'); |
421
|
|
|
$this->addEmptyLine = false; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
$line = $this->getLineGenerator(' * @version ' . $version['number']); |
425
|
|
|
if (strlen($version['description']) > 0) { |
426
|
|
|
$line->add($version['description']); |
427
|
|
|
} |
428
|
|
|
$this->addContent($line); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.