Skip to content

Commit 87c8fff

Browse files
committed
fix: method docs parse error on multi line
1 parent 40a18a5 commit 87c8fff

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/Annotate/DocblockRules.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,18 @@ public function parse(): self
138138
*/
139139
protected function parseMultiLines(array $lines): array
140140
{
141-
$index = 0;
141+
$index = $keyWidth = 0;
142142
$rules = $kvRules = [];
143143

144-
$keyWidth = 16; // with an default value.
144+
$sepChar = ' ';
145+
$sepLen = strlen($sepChar);
145146
foreach ($lines as $line) {
146147
$trimmed = trim($line);
147148
if (!$trimmed) {
148149
continue;
149150
}
150151

151-
$nodes = Str::explode($trimmed, ' ', 2);
152+
$nodes = Str::explode($trimmed, $sepChar, 2);
152153
if (!isset($nodes[1])) {
153154
if ($index === 0) { // invalid first line
154155
continue;
@@ -159,12 +160,6 @@ protected function parseMultiLines(array $lines): array
159160
continue;
160161
}
161162

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-
168163
$name = trim($nodes[0], '.');
169164
if (!preg_match('/^[\w ,-]{0,48}$/', $name)) {
170165
if ($index === 0) { // invalid first line
@@ -176,9 +171,15 @@ protected function parseMultiLines(array $lines): array
176171
continue;
177172
}
178173

179-
$nameLen = strlen($name);
174+
$nameLen = strlen($name) + $sepLen;
180175
$keyWidth = $nameLen > $keyWidth ? $nameLen : $keyWidth;
181176

177+
// TIP: special - if line indent space len gt keyWidth, is desc message of multi line.
178+
if (!trim(substr($line, 0, $keyWidth))) {
179+
$rules[$index - 1][1] .= "\n" . $trimmed; // multi desc message.
180+
continue;
181+
}
182+
182183
// append
183184
$rules[$index] = [$name, $nodes[1]];
184185
$index++;

test/Annotate/DocblockRulesTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,41 @@ public function testParse_byDocComment(): void
6969
$this->assertArrayHasKey('repoPath', $args);
7070
}
7171

72+
public function testParse_byDocComment_mlOptions(): void
73+
{
74+
$dr = DocblockRules::new();
75+
$dr->setDocTagsByDocblock(<<<DOC
76+
/**
77+
* Match directory paths by given keywords
78+
*
79+
* @arguments
80+
* keywords The jump target directory keywords for match.
81+
*
82+
* @options
83+
* --flag The flag set for match paths.
84+
* Allow:
85+
* 1 Only match name path list
86+
* 2 Only match history path list
87+
* 3 match all directory path list(default)
88+
* --no-name bool;Not output name for named paths, useful for bash env.
89+
* --limit bool;Limit the match result rows
90+
*
91+
*/
92+
DOC
93+
);
94+
95+
$dr->parse();
96+
97+
$this->assertNotEmpty($opts = $dr->getOptRules());
98+
$this->assertCount(3, $opts);
99+
vdump($opts);
100+
$this->assertStringContainsString('2 Only match history path list', $opts['--flag']);
101+
102+
$this->assertNotEmpty($args = $dr->getArgRules());
103+
$this->assertCount(1, $args);
104+
$this->assertArrayHasKey('keywords', $args);
105+
}
106+
72107
public function testParse_byDocComment_complex(): void
73108
{
74109
$dr = DocblockRules::new();

0 commit comments

Comments
 (0)