Skip to content

Commit de5e7f1

Browse files
committed
add doc block parser
1 parent 60aeac7 commit de5e7f1

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/Processors/DocBlockProcessor.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace AndreasElia\PostmanGenerator\Processors;
4+
5+
use Illuminate\Support\Str;
6+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
7+
use PHPStan\PhpDocParser\Lexer\Lexer;
8+
use PHPStan\PhpDocParser\Parser\ConstExprParser;
9+
use PHPStan\PhpDocParser\Parser\PhpDocParser;
10+
use PHPStan\PhpDocParser\Parser\TokenIterator;
11+
use PHPStan\PhpDocParser\Parser\TypeParser;
12+
use ReflectionFunction;
13+
use ReflectionMethod;
14+
use Throwable;
15+
16+
class DocBlockProcessor
17+
{
18+
public function __invoke(ReflectionMethod|ReflectionFunction $reflectionMethod): string
19+
{
20+
try {
21+
$lexer = new Lexer;
22+
$constExprParser = new ConstExprParser;
23+
$parser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser);
24+
25+
$description = '';
26+
$comment = $reflectionMethod->getDocComment();
27+
$tokens = new TokenIterator($lexer->tokenize($comment));
28+
$phpDocNode = $parser->parse($tokens);
29+
30+
foreach ($phpDocNode->children as $child) {
31+
if ($child instanceof PhpDocTextNode) {
32+
$description .= ' '.$child->text;
33+
}
34+
}
35+
36+
return Str::squish($description);
37+
} catch (Throwable $e) {
38+
return '';
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)