Skip to content

Commit

Permalink
fix: method docs parse error on multi line
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 26, 2021
1 parent 40a18a5 commit 87c8fff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/Annotate/DocblockRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ public function parse(): self
*/
protected function parseMultiLines(array $lines): array
{
$index = 0;
$index = $keyWidth = 0;
$rules = $kvRules = [];

$keyWidth = 16; // with an default value.
$sepChar = ' ';
$sepLen = strlen($sepChar);
foreach ($lines as $line) {
$trimmed = trim($line);
if (!$trimmed) {
continue;
}

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

// TIP: special - if line indent space len gt keyWidth, is desc message of multi line.
if (!trim(substr($line, 0, $keyWidth))) {
$rules[$index - 1][1] .= "\n" . $trimmed; // multi desc message.
continue;
}

$name = trim($nodes[0], '.');
if (!preg_match('/^[\w ,-]{0,48}$/', $name)) {
if ($index === 0) { // invalid first line
Expand All @@ -176,9 +171,15 @@ protected function parseMultiLines(array $lines): array
continue;
}

$nameLen = strlen($name);
$nameLen = strlen($name) + $sepLen;
$keyWidth = $nameLen > $keyWidth ? $nameLen : $keyWidth;

// TIP: special - if line indent space len gt keyWidth, is desc message of multi line.
if (!trim(substr($line, 0, $keyWidth))) {
$rules[$index - 1][1] .= "\n" . $trimmed; // multi desc message.
continue;
}

// append
$rules[$index] = [$name, $nodes[1]];
$index++;
Expand Down
35 changes: 35 additions & 0 deletions test/Annotate/DocblockRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ public function testParse_byDocComment(): void
$this->assertArrayHasKey('repoPath', $args);
}

public function testParse_byDocComment_mlOptions(): void
{
$dr = DocblockRules::new();
$dr->setDocTagsByDocblock(<<<DOC
/**
* Match directory paths by given keywords
*
* @arguments
* keywords The jump target directory keywords for match.
*
* @options
* --flag The flag set for match paths.
* Allow:
* 1 Only match name path list
* 2 Only match history path list
* 3 match all directory path list(default)
* --no-name bool;Not output name for named paths, useful for bash env.
* --limit bool;Limit the match result rows
*
*/
DOC
);

$dr->parse();

$this->assertNotEmpty($opts = $dr->getOptRules());
$this->assertCount(3, $opts);
vdump($opts);
$this->assertStringContainsString('2 Only match history path list', $opts['--flag']);

$this->assertNotEmpty($args = $dr->getArgRules());
$this->assertCount(1, $args);
$this->assertArrayHasKey('keywords', $args);
}

public function testParse_byDocComment_complex(): void
{
$dr = DocblockRules::new();
Expand Down

0 comments on commit 87c8fff

Please sign in to comment.