Skip to content

Add support for psalm-inheritors and phpstan-sealed tag #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Ast/PhpDoc/PhpDocNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ public function getRequireImplementsTagValues(string $tagName = '@phpstan-requir
);
}

/**
* @return SealedTagValueNode[]
*/
public function getSealedTagValues(string $tagName = '@phpstan-sealed'): array
{
return array_filter(
array_column($this->getTagsByName($tagName), 'value'),
static fn (PhpDocTagValueNode $value): bool => $value instanceof SealedTagValueNode,
);
}

/**
* @return DeprecatedTagValueNode[]
*/
Expand Down
31 changes: 31 additions & 0 deletions src/Ast/PhpDoc/SealedTagValueNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\NodeAttributes;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use function trim;

class SealedTagValueNode implements PhpDocTagValueNode
{

use NodeAttributes;

public TypeNode $type;

/** @var string (may be empty) */
public string $description;

public function __construct(TypeNode $type, string $description)
{
$this->type = $type;
$this->description = $description;
}


public function __toString(): string
{
return trim("{$this->type} {$this->description}");
}

}
12 changes: 12 additions & 0 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
$tagValue = $this->parseRequireImplementsTagValue($tokens);
break;

case '@psalm-inheritors':
case '@phpstan-sealed':
$tagValue = $this->parseSealedTagValue($tokens);
break;

case '@deprecated':
$tagValue = $this->parseDeprecatedTagValue($tokens);
break;
Expand Down Expand Up @@ -933,6 +938,13 @@ private function parseRequireImplementsTagValue(TokenIterator $tokens): Ast\PhpD
return new Ast\PhpDoc\RequireImplementsTagValueNode($type, $description);
}

private function parseSealedTagValue(TokenIterator $tokens): Ast\PhpDoc\SealedTagValueNode
{
$type = $this->typeParser->parse($tokens);
$description = $this->parseOptionalDescription($tokens, true);
return new Ast\PhpDoc\SealedTagValueNode($type, $description);
}

private function parseDeprecatedTagValue(TokenIterator $tokens): Ast\PhpDoc\DeprecatedTagValueNode
{
$description = $this->parseOptionalDescription($tokens, false);
Expand Down
5 changes: 5 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\SealedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
Expand Down Expand Up @@ -327,6 +328,10 @@ private function printTagValue(PhpDocTagValueNode $node): string
$type = $this->printType($node->type);
return trim("{$type} {$node->description}");
}
if ($node instanceof SealedTagValueNode) {
$type = $this->printType($node->type);
return trim("{$type} {$node->description}");
}
if ($node instanceof ParamOutTagValueNode) {
$type = $this->printType($node->type);
return trim("{$type} {$node->parameterName} {$node->description}");
Expand Down
77 changes: 77 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\SealedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
Expand Down Expand Up @@ -108,6 +109,7 @@ protected function setUp(): void
* @dataProvider provideMixinTagsData
* @dataProvider provideRequireExtendsTagsData
* @dataProvider provideRequireImplementsTagsData
* @dataProvider provideSealedTagsData
* @dataProvider provideDeprecatedTagsData
* @dataProvider providePropertyTagsData
* @dataProvider provideMethodTagsData
Expand Down Expand Up @@ -2210,6 +2212,81 @@ public function provideRequireImplementsTagsData(): Iterator
];
}

public function provideSealedTagsData(): Iterator
{
yield [
'OK without description',
'/** @phpstan-sealed Foo|Bar */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-sealed',
new SealedTagValueNode(
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
'',
),
),
]),
];

yield [
'OK with description',
'/** @phpstan-sealed Foo|Bar optional description */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-sealed',
new SealedTagValueNode(
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
'optional description',
),
),
]),
];

yield [
'OK with psalm-prefix description',
'/** @psalm-inheritors Foo|Bar optional description */',
new PhpDocNode([
new PhpDocTagNode(
'@psalm-inheritors',
new SealedTagValueNode(
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
'optional description',
),
),
]),
];

yield [
'invalid without type and description',
'/** @phpstan-sealed */',
new PhpDocNode([
new PhpDocTagNode(
'@phpstan-sealed',
new InvalidTagValueNode(
'',
new ParserException(
'*/',
Lexer::TOKEN_CLOSE_PHPDOC,
20,
Lexer::TOKEN_IDENTIFIER,
null,
1,
),
),
),
]),
];
}

public function provideDeprecatedTagsData(): Iterator
{
yield [
Expand Down
Loading