Skip to content

Commit fb467d5

Browse files
author
Kirill Nesmeyanov
committed
Fix phpdoc parser compatibility
1 parent d0604f3 commit fb467d5

File tree

6 files changed

+28
-61
lines changed

6 files changed

+28
-61
lines changed

src/DocBlock/Extractor/TagTypeExtractor.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
use TypeLang\Parser\Node\Name;
88
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
99
use TypeLang\Parser\Node\Stmt\TypeStatement;
10-
use TypeLang\Parser\ParserInterface;
11-
use TypeLang\Parser\Traverser;
10+
use TypeLang\Parser\Parser;
1211
use TypeLang\PhpDocParser\Exception\InvalidTagTypeException;
13-
use TypeLang\PhpDocParser\Visitor\NodeCompleteOffsetVisitor;
1412

1513
final class TagTypeExtractor
1614
{
1715
private static ?NamedTypeNode $mixed = null;
1816

1917
public function __construct(
20-
private readonly ParserInterface $parser,
18+
private readonly Parser $parser,
2119
) {}
2220

2321
/**
@@ -31,7 +29,7 @@ public function extractTypeOrFail(string $body): array
3129
$type = $this->parser->parse($body);
3230

3331
if ($type instanceof TypeStatement) {
34-
return [$type, $this->slice($type, $body)];
32+
return [$type, $this->slice($body)];
3533
}
3634

3735
throw InvalidTagTypeException::fromNonTyped();
@@ -62,20 +60,10 @@ protected function createMixedType(): NamedTypeNode
6260
/**
6361
* @return non-empty-string|null
6462
*/
65-
private function slice(TypeStatement $type, string $body): ?string
63+
private function slice(string $body): ?string
6664
{
67-
$offset = $this->getTypeOffset($type);
65+
$offset = $this->parser->lastProcessedTokenOffset;
6866

69-
return \ltrim(\substr($body, $offset)) ?: null;
70-
}
71-
72-
/**
73-
* @return int<0, max>
74-
*/
75-
public function getTypeOffset(TypeStatement $stmt): int
76-
{
77-
$visitor = Traverser::through(new NodeCompleteOffsetVisitor(), [$stmt]);
78-
79-
return $visitor->offset;
67+
return \trim(\substr($body, $offset)) ?: null;
8068
}
8169
}

src/DocBlock/Tag/CommonTypedTagFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TypeLang\PhpDocParser\DocBlock\Tag;
66

7+
use TypeLang\Parser\Parser;
78
use TypeLang\Parser\ParserInterface;
89
use TypeLang\PhpDocParser\DocBlock\DescriptionFactoryInterface;
910
use TypeLang\PhpDocParser\Exception\InvalidTagException;
@@ -19,7 +20,7 @@ final class CommonTypedTagFactory extends TypedTagFactory
1920
*/
2021
public function __construct(
2122
private readonly string $class,
22-
ParserInterface $parser,
23+
Parser $parser = new Parser(true),
2324
?DescriptionFactoryInterface $descriptions = null,
2425
) {
2526
parent::__construct($parser, $descriptions);

src/DocBlock/Tag/TypedTagFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TypeLang\PhpDocParser\DocBlock\Tag;
66

7+
use TypeLang\Parser\Parser;
78
use TypeLang\Parser\ParserInterface;
89
use TypeLang\PhpDocParser\DocBlock\DescriptionFactoryInterface;
910
use TypeLang\PhpDocParser\DocBlock\Extractor\TagTypeExtractor;
@@ -18,7 +19,7 @@ abstract class TypedTagFactory extends TagFactory
1819
protected readonly TagTypeExtractor $types;
1920

2021
public function __construct(
21-
ParserInterface $parser,
22+
Parser $parser = new Parser(true),
2223
?DescriptionFactoryInterface $descriptions = null,
2324
) {
2425
$this->types = new TagTypeExtractor($parser);

src/Visitor/NodeCompleteOffsetVisitor.php

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

tests/Functional/DocBlockParsingTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function testTagIsSupported(DocBlockFactoryInterface $factory, string $co
3636
{
3737
$phpdoc = $factory->create($comment);
3838

39-
$this->assertDocBlockNotContainsInvalidTags($phpdoc, self::NOT_IMPLEMENTED_TAGS);
39+
try {
40+
$this->assertDocBlockNotContainsInvalidTags($phpdoc, self::NOT_IMPLEMENTED_TAGS, $comment);
41+
} catch (\Throwable $e) {
42+
dump($phpdoc);
43+
throw $e;
44+
}
4045
}
4146
}

tests/Functional/TestCase.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,10 @@ abstract class TestCase extends BaseTestCase
293293
protected function assertDocBlockNotContainsInvalidTags(
294294
DocBlock $docBlock,
295295
array $except = [],
296+
string $message = '',
296297
): void {
297-
$this->assertDescriptionNotContainsInvalidTags($docBlock->getDescription(), $except);
298-
$this->assertTagsNotContainsInvalidTags($docBlock->getTags(), $except);
298+
$this->assertDescriptionNotContainsInvalidTags($docBlock->getDescription(), $except, $message);
299+
$this->assertTagsNotContainsInvalidTags($docBlock->getTags(), $except, $message);
299300
}
300301

301302
/**
@@ -304,29 +305,33 @@ protected function assertDocBlockNotContainsInvalidTags(
304305
protected function assertDescriptionNotContainsInvalidTags(
305306
\Stringable|string|null $description,
306307
array $except = [],
308+
string $message = '',
307309
): void {
308310
if ($description instanceof Description) {
309-
$this->assertTagsNotContainsInvalidTags($description->getTags(), $except);
311+
$this->assertTagsNotContainsInvalidTags($description->getTags(), $except, $message);
310312
}
311313
}
312314

313315
/**
314316
* @param iterable<TagInterface> $tags
315317
* @param list<non-empty-string> $except
316318
*/
317-
protected function assertTagsNotContainsInvalidTags(iterable $tags, array $except = []): void
318-
{
319+
protected function assertTagsNotContainsInvalidTags(
320+
iterable $tags,
321+
array $except = [],
322+
string $message = '',
323+
): void {
319324
// Suppress "no assertions" notice
320325
self::assertTrue(true);
321326

322327
foreach ($tags as $tag) {
323328
if ($tag instanceof InvalidTagInterface) {
324329
if (!$this->isSkipAllowed($tag)) {
325-
self::assertContains($tag->getName(), $except);
330+
self::assertContains($tag->getName(), $except, $message);
326331
}
327332
}
328333

329-
$this->assertDescriptionNotContainsInvalidTags($tag->getDescription(), $except);
334+
$this->assertDescriptionNotContainsInvalidTags($tag->getDescription(), $except, $message);
330335
}
331336
}
332337

0 commit comments

Comments
 (0)