Skip to content

Commit ee0d8a5

Browse files
author
Kirill Nesmeyanov
committed
Add basic docblocks support
1 parent 9982a6e commit ee0d8a5

17 files changed

+171
-29
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"require": {
1212
"php": "^8.1",
13+
"psr/http-message": "^1.0|^2.0",
1314
"type-lang/parser": "^1.0"
1415
},
1516
"autoload": {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace TypeLang\PhpDocParser\DocBlock;
5+
namespace TypeLang\PhpDocParser\Description;
66

7+
use TypeLang\PhpDocParser\DocBlock\Description;
78
use TypeLang\PhpDocParser\DocBlock\Tag\TagInterface;
9+
use TypeLang\PhpDocParser\DocBlock\TagFactoryInterface;
810

911
final class DescriptionFactory implements DescriptionFactoryInterface
1012
{

src/DocBlock/DescriptionFactoryInterface.php renamed to src/Description/DescriptionFactoryInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace TypeLang\PhpDocParser\DocBlock;
5+
namespace TypeLang\PhpDocParser\Description;
6+
7+
use TypeLang\PhpDocParser\DocBlock\Description;
68

79
interface DescriptionFactoryInterface
810
{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDocParser\DocBlock\Extractor;
6+
7+
use TypeLang\PhpDocParser\DocBlock\Reference\ReferenceInterface;
8+
use TypeLang\PhpDocParser\DocBlock\Reference\UriReference;
9+
10+
final class TagReferenceExtractor
11+
{
12+
public function extract(string $body): array
13+
{
14+
$description = \strpbrk($body, " \t\n\r\0\x0B");
15+
16+
if ($description === false) {
17+
return [$this->parseReference($body), null];
18+
}
19+
20+
$descriptionOffset = \strlen($body) - \strlen($description);
21+
22+
return [
23+
$this->parseReference(\substr($body, 0, $descriptionOffset)),
24+
\ltrim($description),
25+
];
26+
}
27+
28+
private function parseReference(string $body): ReferenceInterface
29+
{
30+
return new UriReference($body);
31+
}
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDocParser\DocBlock\Reference;
6+
7+
interface ReferenceInterface extends \Stringable
8+
{
9+
/**
10+
* Returns string representation of the reference.
11+
*/
12+
public function __toString(): string;
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDocParser\DocBlock\Reference;
6+
7+
final class UriReference implements ReferenceInterface
8+
{
9+
public function __construct(
10+
private readonly string $uri,
11+
) {}
12+
13+
public function __toString(): string
14+
{
15+
return $this->uri;
16+
}
17+
}

src/DocBlock/Tag/LinkTag.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44

55
namespace TypeLang\PhpDocParser\DocBlock\Tag;
66

7+
use TypeLang\PhpDocParser\DocBlock\Reference\ReferenceInterface;
8+
79
/**
8-
* TODO Add url extraction: {@link https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/link.html#link}
10+
* @link https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/link.html#link
911
*/
1012
final class LinkTag extends Tag
1113
{
12-
public function __construct(\Stringable|string|null $description = null)
13-
{
14+
public function __construct(
15+
private readonly ReferenceInterface $reference,
16+
\Stringable|string|null $description = null,
17+
) {
1418
parent::__construct('link', $description);
1519
}
20+
21+
public function getReference(): ReferenceInterface
22+
{
23+
return $this->reference;
24+
}
1625
}

src/DocBlock/Tag/SeeTag.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44

55
namespace TypeLang\PhpDocParser\DocBlock\Tag;
66

7+
use TypeLang\PhpDocParser\DocBlock\Reference\ReferenceInterface;
8+
79
/**
8-
* TODO Add url/fqsen extraction: {@link https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/see.html#see}
10+
* @link https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/see.html#see
911
*/
1012
final class SeeTag extends Tag
1113
{
12-
public function __construct(\Stringable|string|null $description = null)
13-
{
14+
public function __construct(
15+
private readonly ReferenceInterface $reference,
16+
\Stringable|string|null $description = null,
17+
) {
1418
parent::__construct('see', $description);
1519
}
20+
21+
public function getReference(): ReferenceInterface
22+
{
23+
return $this->reference;
24+
}
1625
}

src/DocBlock/Tag/CommonTagFactory.php renamed to src/DocBlock/TagFactory/CommonTagFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace TypeLang\PhpDocParser\DocBlock\Tag;
5+
namespace TypeLang\PhpDocParser\DocBlock\TagFactory;
66

7-
use TypeLang\PhpDocParser\DocBlock\DescriptionFactoryInterface;
7+
use TypeLang\PhpDocParser\Description\DescriptionFactoryInterface;
8+
use TypeLang\PhpDocParser\DocBlock\Tag\Tag;
89
use TypeLang\PhpDocParser\Exception\InvalidTagException;
910

1011
/**

src/DocBlock/Tag/CommonTypedTagFactory.php renamed to src/DocBlock/TagFactory/CommonTypedTagFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
declare(strict_types=1);
44

5-
namespace TypeLang\PhpDocParser\DocBlock\Tag;
5+
namespace TypeLang\PhpDocParser\DocBlock\TagFactory;
66

77
use TypeLang\Parser\Parser;
8-
use TypeLang\PhpDocParser\DocBlock\DescriptionFactoryInterface;
8+
use TypeLang\PhpDocParser\Description\DescriptionFactoryInterface;
9+
use TypeLang\PhpDocParser\DocBlock\Tag\TypedTag;
10+
use TypeLang\PhpDocParser\DocBlock\TagFactory\TypedTagFactory;
911
use TypeLang\PhpDocParser\Exception\InvalidTagException;
1012

1113
/**

0 commit comments

Comments
 (0)