Skip to content

Commit 3a8db71

Browse files
committed
Strip leading @ from InvalidTag name
Tag names are normally exposed without their leading "@", but when an InvalidTag is built from the name PHPStan's phpdoc-parser advertises (e.g. for a malformed `@var $self SomeClass`), the name is passed through as-is and `getName()` ends up returning "@var" instead of "var". Normalizing the name at the single entry point (`InvalidTag::create`) keeps the behaviour consistent with every other tag in the library regardless of the upstream parser's convention. Fixes #449
1 parent 7bae675 commit 3a8db71

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

src/DocBlock/Tags/InvalidTag.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function is_array;
1919
use function is_object;
2020
use function is_resource;
21+
use function ltrim;
2122
use function sprintf;
2223

2324
use const PHP_VERSION_ID;
@@ -58,7 +59,9 @@ public function getName(): string
5859

5960
public static function create(string $body, string $name = ''): self
6061
{
61-
return new self($name, $body);
62+
// Some upstream parsers (e.g. phpstan/phpdoc-parser) keep the leading "@" in the tag name they expose.
63+
// All other tags strip it before reaching the Tag layer, so normalize here to keep getName() consistent.
64+
return new self(ltrim($name, '@'), $body);
6265
}
6366

6467
public function withError(Throwable $exception): self

tests/unit/DocBlock/Tags/Factory/AbstractPHPStanFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testCreateReturnsInvalidTagWhenNoFactorySupports(): void
6868
$result = $sut->create('@unknown string $param');
6969

7070
self::assertInstanceOf(InvalidTag::class, $result);
71-
self::assertEquals('@unknown', $result->getName());
71+
self::assertEquals('unknown', $result->getName());
7272
}
7373

7474
/**

tests/unit/DocBlock/Tags/InvalidTagTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public function testCreationWithoutError(): void
3333
self::assertNull($tag->getException());
3434
}
3535

36+
public function testCreationStripsLeadingAtFromName(): void
37+
{
38+
$tag = InvalidTag::create('Body', '@var');
39+
40+
self::assertSame('var', $tag->getName());
41+
self::assertSame('@var Body', $tag->render());
42+
}
43+
3644
/**
3745
* @covers ::withError
3846
* @covers ::__toString

0 commit comments

Comments
 (0)