Skip to content

Commit 90340eb

Browse files
committed
add some tests for parse doc
1 parent a4137a3 commit 90340eb

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

src/Obj/Traits/NameAliasTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Toolkit\Stdlib\Obj\Traits;
44

55
use InvalidArgumentException;
6+
use function count;
67

78
/**
89
* trait NameAliasTrait
@@ -57,6 +58,14 @@ public function hasAlias(string $alias): bool
5758
return isset($this->aliases[$alias]);
5859
}
5960

61+
/**
62+
* @return int
63+
*/
64+
public function countAlias(): int
65+
{
66+
return count($this->aliases);
67+
}
68+
6069
/**
6170
* @param string $name
6271
*

src/Util/PhpDoc.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
use function array_merge;
1313
use function in_array;
1414
use function is_array;
15+
use function preg_match;
16+
use function preg_replace;
1517
use function preg_split;
1618
use function str_replace;
17-
use function preg_replace;
18-
use function preg_match;
1919
use function substr;
2020
use function trim;
21-
use const PREG_SPLIT_NO_EMPTY;
2221
use const PREG_OFFSET_CAPTURE;
22+
use const PREG_SPLIT_NO_EMPTY;
2323

2424
/**
2525
* Class PhpDoc
@@ -36,11 +36,14 @@ class PhpDoc
3636
* Parses the comment block into tags.
3737
*
3838
* @param string $comment The comment block text
39-
* @param array $options
39+
* @param array $options
4040
* - 'allow' // only allowed tags
41+
* - 'multi' // allowed multi tag names
4142
* - 'ignore' // ignored tags
4243
* - 'default' => 'description', // default tag name, first line text will attach to it.
4344
*
45+
* @psalm-param array{allow: array, multi: array, ignore: array, default: string} $options
46+
*
4447
* @return array The parsed tags
4548
*/
4649
public static function getTags(string $comment, array $options = []): array
@@ -51,16 +54,22 @@ public static function getTags(string $comment, array $options = []): array
5154

5255
$options = array_merge([
5356
'allow' => [], // only allowed tags
54-
'ignore' => ['param', 'return'], // ignore tags
57+
'multi' => [], // allowed multi tags
58+
'ignore' => ['param', 'return'], // ignored tags
5559
'default' => 'description', // default tag name, first line text will attach to it.
5660
], $options);
5761

58-
$allow = (array)$options['allow'];
62+
$multi = (array)$options['multi'];
63+
$allowed = (array)$options['allow'];
5964
$ignored = (array)$options['ignore'];
6065
$default = (string)$options['default'];
6166

6267
$comment = str_replace("\r\n", "\n", $comment);
63-
$comment = "@{$default} \n" . str_replace("\r", '', trim(preg_replace('/^\s*\**( |\t)?/m', '', $comment)));
68+
$comment = "@$default \n" . str_replace(
69+
"\r",
70+
'',
71+
trim(preg_replace('/^\s*\**( |\t)?/m', '', $comment))
72+
);
6473

6574
$tags = [];
6675
$parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY);
@@ -73,7 +82,7 @@ public static function getTags(string $comment, array $options = []): array
7382
}
7483

7584
// always allow default tag
76-
if ($default !== $name && $allow && !in_array($name, $allow, true)) {
85+
if ($default !== $name && $allowed && !in_array($name, $allowed, true)) {
7786
continue;
7887
}
7988

test/Helper/DataHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testToString(): void
3838
self::assertEquals('abc', DataHelper::toString('abc'));
3939
self::assertEquals('bool(TRUE)', DataHelper::toString(true));
4040
self::assertEquals('bool(FALSE)', DataHelper::toString(false));
41-
self::assertEquals('<NULL>', DataHelper::toString(null));
41+
self::assertEquals('NULL', DataHelper::toString(null));
4242
self::assertEquals('["ab",23]', DataHelper::toString(['ab', 23]));
4343

4444
$str = DataHelper::toString((object)['ab', 23]);

test/Util/PhpDocTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest\Util;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use ReflectionMethod;
7+
use Toolkit\Stdlib\Util\PhpDoc;
8+
use function vdump;
9+
10+
/**
11+
* class PhpDocTest
12+
*/
13+
class PhpDocTest extends TestCase
14+
{
15+
/**
16+
* Open an github repository by browser
17+
*
18+
* @options
19+
* -r, --remote The git remote name. default is `origin`
20+
* --main Use the config `mainRemote` name
21+
*
22+
* @arguments
23+
* repoPath The remote git repo URL or repository group/name.
24+
* If not input, will auto parse from current work directory
25+
*
26+
* @param string $input
27+
* @param int $output
28+
*
29+
* @example
30+
* {fullCmd} php-toolkit/cli-utils
31+
* {fullCmd} https://github.com/php-toolkit/cli-utils
32+
*/
33+
public function testDoc_basic(): void
34+
{
35+
$rftMth = new ReflectionMethod($this, 'testDoc_basic');
36+
37+
$tags = PhpDoc::getTags($rftMth->getDocComment(), [
38+
'default' => 'desc',
39+
]);
40+
41+
$this->assertArrayHasKey('desc', $tags);
42+
$this->assertArrayHasKey('example', $tags);
43+
$this->assertArrayHasKey('options', $tags);
44+
$this->assertArrayHasKey('arguments', $tags);
45+
46+
$this->assertEquals('Open an github repository by browser', $tags['desc']);
47+
}
48+
}

0 commit comments

Comments
 (0)