Skip to content

Commit eec62e3

Browse files
committed
enhance: parse method doc comments enhance
1 parent 496bb5c commit eec62e3

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

src/Annotate/DocblockRules.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use function array_merge;
99
use function explode;
1010
use function preg_match;
11+
use function strlen;
12+
use function substr;
1113
use function trim;
1214

1315
/**
@@ -139,7 +141,7 @@ protected function parseMultiLines(array $lines): array
139141
$index = 0;
140142
$rules = $kvRules = [];
141143

142-
// $keyWidth = 16;
144+
$keyWidth = 16; // with an default value.
143145
foreach ($lines as $line) {
144146
$trimmed = trim($line);
145147
if (!$trimmed) {
@@ -157,6 +159,12 @@ protected function parseMultiLines(array $lines): array
157159
continue;
158160
}
159161

162+
// TIP: special - if line indent space len gt keyWidth, is desc message of multi line.
163+
if (!trim(substr($line, 0, $keyWidth))) {
164+
$rules[$index - 1][1] .= "\n" . $trimmed; // multi desc message.
165+
continue;
166+
}
167+
160168
$name = trim($nodes[0], '.');
161169
if (!preg_match('/^[\w ,-]{0,48}$/', $name)) {
162170
if ($index === 0) { // invalid first line
@@ -168,10 +176,15 @@ protected function parseMultiLines(array $lines): array
168176
continue;
169177
}
170178

179+
$nameLen = strlen($name);
180+
$keyWidth = $nameLen > $keyWidth ? $nameLen : $keyWidth;
181+
182+
// append
171183
$rules[$index] = [$name, $nodes[1]];
172184
$index++;
173185
}
174186

187+
// convert to k-v data.
175188
if ($rules) {
176189
foreach ($rules as [$name, $rule]) {
177190
$kvRules[$name] = $rule;

test/Annotate/DocblockRulesTest.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
use Inhere\Console\Annotate\DocblockRules;
66
use Inhere\ConsoleTest\BaseTestCase;
7+
use ReflectionException;
78
use ReflectionMethod;
9+
use function vdump;
810

911
/**
1012
* class DocblockRulesTest
1113
*/
1214
class DocblockRulesTest extends BaseTestCase
1315
{
1416
/**
15-
* @throws \ReflectionException
17+
* @throws ReflectionException
1618
*/
1719
public function testParse_byReflect(): void
1820
{
@@ -53,7 +55,7 @@ public function testParse_byDocComment(): void
5355
*
5456
*/
5557
DOC
56-
);
58+
);
5759
$dr->parse();
5860

5961
$this->assertNotEmpty($dr->getTagValue('desc'));
@@ -66,4 +68,69 @@ public function testParse_byDocComment(): void
6668
$this->assertArrayHasKey('--main', $opts);
6769
$this->assertArrayHasKey('repoPath', $args);
6870
}
71+
72+
public function testParse_byDocComment_complex(): void
73+
{
74+
$dr = DocblockRules::new();
75+
$dr->setDocTagsByDocblock(<<<DOC
76+
/**
77+
* collect git change log information by `git log`
78+
*
79+
* @arguments
80+
* oldVersion string;The old version. eg: v1.0.2
81+
* - keywords `last/latest` will auto use latest tag.
82+
* - keywords `prev/previous` will auto use previous tag.;required
83+
* newVersion string;The new version. eg: v1.2.3
84+
* - keywords `head` will use `Head` commit.;required
85+
*
86+
* @options
87+
* --exclude Exclude contains given sub-string. multi by comma split.
88+
* --fetch-tags bool;Update repo tags list by `git fetch --tags`
89+
* --file Export changelog message to file
90+
* --filters Apply built in log filters. multi by `|` split
91+
* allow:
92+
* kw keyword filter. eg: `kw:tom`
93+
* kws keywords filter.
94+
* ml msg length filter.
95+
* wl word length filter.
96+
* --format The git log option `--pretty` value.
97+
* can be one of oneline, short, medium, full, fuller, reference, email, raw, format:<string> and tformat:<string>.
98+
* --style The style for generate for changelog.
99+
* allow: markdown(<cyan>default</cyan>), simple, gh-release
100+
* --repo-url The git repo URL address. eg: https://github.com/inhere/kite
101+
* default will auto use current git origin remote url
102+
* --no-merges bool;No contains merge request logs
103+
* --unshallow bool;Convert to a complete warehouse, useful on GitHub Action.
104+
* --with-author bool;Display commit author name
105+
*
106+
* @example
107+
* {binWithCmd} last head
108+
* {binWithCmd} last head --style gh-release --no-merges
109+
* {binWithCmd} v2.0.9 v2.0.10 --no-merges --style gh-release --exclude "cs-fixer,format codes"
110+
*/
111+
DOC
112+
);
113+
114+
$dr->parse();
115+
116+
$this->assertNotEmpty($dr->getTagValue('desc'));
117+
$this->assertNotEmpty($dr->getTagValue('example'));
118+
119+
$this->assertNotEmpty($opts = $dr->getOptRules());
120+
$this->assertArrayHasKey('--exclude', $opts);
121+
$this->assertArrayHasKey('--with-author', $opts);
122+
$this->assertArrayHasKey('--format', $opts);
123+
$this->assertArrayHasKey('--style', $opts);
124+
$this->assertArrayHasKey('--filters', $opts);
125+
126+
$this->assertStringContainsString('can be one of oneline, short', $opts['--format']);
127+
$this->assertStringContainsString('kw keyword filter', $opts['--filters']);
128+
$this->assertStringContainsString('wl word length filter', $opts['--filters']);
129+
vdump($opts);
130+
131+
$this->assertNotEmpty($args = $dr->getArgRules());
132+
$this->assertCount(2, $args);
133+
$this->assertArrayHasKey('oldVersion', $args);
134+
$this->assertArrayHasKey('newVersion', $args);
135+
}
69136
}

0 commit comments

Comments
 (0)