|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leaditin\Code\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Leaditin\Code\DocBlock; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @package Leaditin\Code |
|
9
|
|
|
* @author Igor Vuckovic <[email protected]> |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
class DocBlockGenerator extends Generator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var TagGenerator |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $tagGenerator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param TagGenerator $tagGenerator |
|
21
|
|
|
*/ |
|
22
|
3 |
|
public function __construct(TagGenerator $tagGenerator) |
|
23
|
|
|
{ |
|
24
|
3 |
|
$this->tagGenerator = $tagGenerator; |
|
25
|
3 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param DocBlock $docBlock |
|
29
|
|
|
* |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function generate(DocBlock $docBlock): string |
|
33
|
|
|
{ |
|
34
|
3 |
|
$lines = []; |
|
35
|
3 |
|
$output = $this->generateLine('/**'); |
|
36
|
|
|
|
|
37
|
3 |
|
if ($docBlock->shortDescription() !== null) { |
|
38
|
2 |
|
$lines[] = $this->textToComment($docBlock->shortDescription()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
if ($docBlock->longDescription() !== null) { |
|
42
|
1 |
|
$lines[] = $this->textToComment($docBlock->longDescription()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
foreach ($this->sortTags($docBlock) as $tag) { |
|
46
|
2 |
|
$lines[] = $this->textToComment($this->tagGenerator->generate($tag)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
$output .= implode($this->textToComment(''), $lines); |
|
50
|
3 |
|
$output .= $this->generateLine(' */'); |
|
51
|
|
|
|
|
52
|
3 |
|
return rtrim($output, $this->endOfLine); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $text |
|
57
|
|
|
* @param bool $wrap |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
3 |
|
protected function textToComment(string $text, bool $wrap = false): string |
|
62
|
|
|
{ |
|
63
|
3 |
|
$text = $wrap ? wordwrap($text, 80, $this->endOfLine) : $text; |
|
64
|
|
|
|
|
65
|
3 |
|
$comment = ''; |
|
66
|
3 |
|
$lines = explode($this->endOfLine, $text); |
|
67
|
|
|
|
|
68
|
3 |
|
foreach ($lines as $line) { |
|
69
|
3 |
|
$comment .= $this->generateLine(rtrim(' * ' . $line)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
return $comment; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param DocBlock $docBlock |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
3 |
|
protected function sortTags(DocBlock $docBlock): array |
|
81
|
|
|
{ |
|
82
|
3 |
|
$array = []; |
|
83
|
|
|
$map = [ |
|
84
|
3 |
|
'return' => 3, |
|
85
|
|
|
'throws' => 2, |
|
86
|
|
|
'property' => 1, |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
3 |
|
foreach ($docBlock->tags() as $tag) { |
|
90
|
2 |
|
$key = $map[$tag->name()] ?? 0; |
|
91
|
2 |
|
$array[$key][] = $tag; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
ksort($array, SORT_NATURAL | SORT_FLAG_CASE); |
|
95
|
|
|
|
|
96
|
3 |
|
if (empty($array)) { |
|
97
|
1 |
|
return []; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
return array_merge(...$array); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|