Skip to content

(RFC) Implement template default syntax #148

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 2 commits into from
Oct 14, 2022
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
9 changes: 7 additions & 2 deletions src/Ast/PhpDoc/TemplateTagValueNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@ class TemplateTagValueNode implements PhpDocTagValueNode
/** @var TypeNode|null */
public $bound;

/** @var TypeNode|null */
public $default;

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

public function __construct(string $name, ?TypeNode $bound, string $description)
public function __construct(string $name, ?TypeNode $bound, string $description, ?TypeNode $default = null)
{
$this->name = $name;
$this->bound = $bound;
$this->default = $default;
$this->description = $description;
}


public function __toString(): string
{
$bound = $this->bound !== null ? " of {$this->bound}" : '';
return trim("{$this->name}{$bound} {$this->description}");
$default = $this->default !== null ? " = {$this->default}" : '';
return trim("{$this->name}{$bound}{$default} {$this->description}");
}

}
8 changes: 7 additions & 1 deletion src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,15 @@ private function parseTemplateTagValue(TokenIterator $tokens): Ast\PhpDoc\Templa
$bound = null;
}

if ($tokens->tryConsumeTokenValue('=')) {
$default = $this->typeParser->parse($tokens);
} else {
$default = null;
}

$description = $this->parseOptionalDescription($tokens);

return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description);
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description, $default);
}

private function parseExtendsTagValue(string $tagName, TokenIterator $tokens): Ast\PhpDoc\PhpDocTagValueNode
Expand Down
48 changes: 48 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3345,6 +3345,54 @@ public function provideTemplateTagsData(): Iterator
),
]),
];

yield [
'OK with default',
'/** @template T = string */',
new PhpDocNode([
new PhpDocTagNode(
'@template',
new TemplateTagValueNode(
'T',
null,
'',
new IdentifierTypeNode('string')
)
),
]),
];

yield [
'OK with default and description',
'/** @template T = string the value type */',
new PhpDocNode([
new PhpDocTagNode(
'@template',
new TemplateTagValueNode(
'T',
null,
'the value type',
new IdentifierTypeNode('string')
)
),
]),
];

yield [
'OK with bound and default and description',
'/** @template T of string = \'\' the value type */',
new PhpDocNode([
new PhpDocTagNode(
'@template',
new TemplateTagValueNode(
'T',
new IdentifierTypeNode('string'),
'the value type',
new ConstTypeNode(new ConstExprStringNode(''))
)
),
]),
];
}

public function provideExtendsTagsData(): Iterator
Expand Down