Skip to content

Commit c741b17

Browse files
committed
Simplify and complete docblock parser
1 parent 1befc84 commit c741b17

File tree

6 files changed

+19
-64
lines changed

6 files changed

+19
-64
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
---
66

77
<p align="center">
8-
<a href="https://packagist.org/packages/type-lang/phpdoc"><img src="https://poser.pugx.org/type-lang/phpdoc/require/php?style=for-the-badge" alt="PHP 8.1+"></a>
9-
<a href="https://packagist.org/packages/type-lang/phpdoc"><img src="https://poser.pugx.org/type-lang/phpdoc/version?style=for-the-badge" alt="Latest Stable Version"></a>
10-
<a href="https://packagist.org/packages/type-lang/phpdoc"><img src="https://poser.pugx.org/type-lang/phpdoc/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a>
11-
<a href="https://github.com/php-type-language/phpdoc-parser/blob/master/LICENSE"><img src="https://poser.pugx.org/type-lang/phpdoc/license?style=for-the-badge" alt="License MIT"></a>
8+
<a href="https://packagist.org/packages/type-lang/phpdoc-parser"><img src="https://poser.pugx.org/type-lang/phpdoc-parser/require/php?style=for-the-badge" alt="PHP 8.1+"></a>
9+
<a href="https://packagist.org/packages/type-lang/phpdoc-parser"><img src="https://poser.pugx.org/type-lang/phpdoc-parser/version?style=for-the-badge" alt="Latest Stable Version"></a>
10+
<a href="https://packagist.org/packages/type-lang/phpdoc-parser"><img src="https://poser.pugx.org/type-lang/phpdoc-parser/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a>
11+
<a href="https://github.com/php-type-language/phpdoc-parser/blob/master/LICENSE"><img src="https://poser.pugx.org/type-lang/phpdoc-parser/license?style=for-the-badge" alt="License MIT"></a>
1212
</p>
1313
<p align="center">
1414
<a href="https://github.com/php-type-language/phpdoc-parser/actions"><img src="https://github.com/php-type-language/phpdoc-parser/workflows/tests/badge.svg"></a>

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
{
2-
"name": "type-lang/phpdoc",
2+
"name": "type-lang/phpdoc-parser",
33
"type": "library",
44
"description": "PHPDoc",
55
"keywords": ["language", "php", "phpdoc", "docblock", "tags", "dictionary"],
66
"license": "MIT",
77
"support": {
8-
"source": "https://github.com/php-type-language/phpdoc",
9-
"issues": "https://github.com/php-type-language/phpdoc/issues"
8+
"source": "https://github.com/php-type-language/phpdoc-parser",
9+
"issues": "https://github.com/php-type-language/phpdoc-parser/issues"
1010
},
1111
"require": {
1212
"php": "^8.1",
13-
"composer/semver": "^2.0|^3.0",
1413
"type-lang/parser": "^1.0"
1514
},
1615
"autoload": {

src/Exception/InternalError.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Exception/PHPDocException.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/DocBlockFactory.php renamed to src/Parser.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,18 @@
2222
/**
2323
* @psalm-suppress UndefinedAttributeClass : JetBrains language attribute may not be available
2424
*/
25-
class DocBlockFactory implements DocBlockFactoryInterface
25+
class Parser implements ParserInterface
2626
{
27-
private readonly CommentParserInterface $comment;
28-
29-
private readonly DescriptionParserInterface $description;
30-
31-
private readonly TagParserInterface $tags;
32-
33-
public function __construct()
34-
{
35-
$this->comment = new LexerAwareCommentParser();
36-
$this->description = new SprintfDescriptionReader();
37-
$this->tags = new TagParser();
38-
}
27+
public function __construct(
28+
private readonly CommentParserInterface $comments = new LexerAwareCommentParser(),
29+
private readonly DescriptionParserInterface $descriptions = new SprintfDescriptionReader(),
30+
private readonly TagParserInterface $tags = new TagParser(),
31+
) {}
3932

4033
/**
4134
* @throws RuntimeExceptionInterface
4235
*/
43-
public function create(#[Language('PHP')] string $docblock): DocBlock
36+
public function parse(#[Language('PHP')] string $docblock): DocBlock
4437
{
4538
$mapper = new SourceMap();
4639

@@ -88,9 +81,9 @@ private function analyze(string $docblock): \Generator
8881
foreach ($blocks->getReturn() as $block) {
8982
try {
9083
if ($description === null) {
91-
$description = $this->description->parse($block, $this->tags);
84+
$description = $this->descriptions->parse($block, $this->tags);
9285
} else {
93-
$tags[] = $this->tags->parse($block, $this->description);
86+
$tags[] = $this->tags->parse($block, $this->descriptions);
9487
}
9588
} catch (RuntimeExceptionInterface $e) {
9689
throw $e->withSource(
@@ -123,7 +116,7 @@ private function groupByCommentSections(string $docblock): \Generator
123116
$current = '';
124117
$blocks = [];
125118

126-
foreach ($this->comment->parse($docblock) as $segment) {
119+
foreach ($this->comments->parse($docblock) as $segment) {
127120
yield $segment;
128121

129122
if (\str_starts_with($segment->text, '@')) {

src/DocBlockFactoryInterface.php renamed to src/ParserInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
/**
1010
* @psalm-suppress UndefinedAttributeClass : JetBrains language attribute may not be available
1111
*/
12-
interface DocBlockFactoryInterface
12+
interface ParserInterface
1313
{
1414
/**
1515
* @param string $docblock A string containing the DocBlock to parse.
1616
*/
17-
public function create(#[Language('PHP')] string $docblock): DocBlock;
17+
public function parse(#[Language('PHP')] string $docblock): DocBlock;
1818
}

0 commit comments

Comments
 (0)