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
|
5 |
|
public function __construct(TagGenerator $tagGenerator) |
23
|
|
|
{ |
24
|
5 |
|
$this->tagGenerator = $tagGenerator; |
25
|
5 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param DocBlock $docBlock |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
5 |
|
public function generate(DocBlock $docBlock): string |
33
|
|
|
{ |
34
|
5 |
|
$lines = []; |
35
|
|
|
|
36
|
5 |
View Code Duplication |
if ($docBlock->shortDescription() !== null) { |
|
|
|
|
37
|
3 |
|
$lines[-2][] = $this->textToComment($docBlock->shortDescription()); |
38
|
|
|
} |
39
|
|
|
|
40
|
5 |
View Code Duplication |
if ($docBlock->longDescription() !== null) { |
|
|
|
|
41
|
1 |
|
$lines[-1][] = $this->textToComment($docBlock->longDescription()); |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
foreach ($this->sortTags($docBlock) as $i => $tags) { |
45
|
3 |
|
foreach ($tags as $tag) { |
46
|
3 |
|
$lines[$i][] = $this->textToComment($this->tagGenerator->generate($tag)); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
if (count($lines) === 0) { |
51
|
1 |
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
$output = $this->generateLine('/**'); |
55
|
|
|
|
56
|
4 |
|
foreach ($lines as $group) { |
57
|
4 |
|
$output .= implode('', $group); |
58
|
4 |
|
$output .= $this->textToComment(''); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
$output = rtrim($output, $this->textToComment('')) . static::END_OF_LINE; |
62
|
4 |
|
$output .= $this->generateLine(' */'); |
63
|
|
|
|
64
|
4 |
|
return rtrim($output, static::END_OF_LINE); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $text |
69
|
|
|
* @param bool $wrap |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
4 |
|
protected function textToComment(string $text, bool $wrap = false): string |
74
|
|
|
{ |
75
|
4 |
|
$text = $wrap ? wordwrap($text, 80, static::END_OF_LINE) : $text; |
76
|
|
|
|
77
|
4 |
|
$comment = ''; |
78
|
4 |
|
$lines = explode(static::END_OF_LINE, $text); |
79
|
|
|
|
80
|
4 |
|
foreach ($lines as $line) { |
81
|
4 |
|
$comment .= $this->generateLine(rtrim(' * ' . $line)); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
return $comment; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param DocBlock $docBlock |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
5 |
|
protected function sortTags(DocBlock $docBlock): array |
93
|
|
|
{ |
94
|
5 |
|
$array = []; |
95
|
|
|
$map = [ |
96
|
5 |
|
'return' => 3, |
97
|
|
|
'throws' => 2, |
98
|
|
|
'param' => 1, |
99
|
|
|
]; |
100
|
|
|
|
101
|
5 |
|
foreach ($docBlock->tags() as $tag) { |
102
|
3 |
|
$key = $map[$tag->name()] ?? 0; |
103
|
3 |
|
$array[$key][] = $tag; |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
ksort($array, SORT_NATURAL | SORT_FLAG_CASE); |
107
|
|
|
|
108
|
5 |
|
if (empty($array)) { |
109
|
2 |
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
return $array; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.