Skip to content

Commit 3c43569

Browse files
committed
Implement template default syntax
1 parent 5eaedcd commit 3c43569

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/Ast/PhpDoc/TemplateTagValueNode.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ class TemplateTagValueNode implements PhpDocTagValueNode
1717
/** @var TypeNode|null */
1818
public $bound;
1919

20+
/** @var TypeNode|null */
21+
public $default;
22+
2023
/** @var string (may be empty) */
2124
public $description;
2225

23-
public function __construct(string $name, ?TypeNode $bound, string $description)
26+
public function __construct(string $name, ?TypeNode $bound, string $description, ?TypeNode $default = null)
2427
{
2528
$this->name = $name;
2629
$this->bound = $bound;
30+
$this->default = $default;
2731
$this->description = $description;
2832
}
2933

3034

3135
public function __toString(): string
3236
{
3337
$bound = $this->bound !== null ? " of {$this->bound}" : '';
34-
return trim("{$this->name}{$bound} {$this->description}");
38+
$default = $this->default !== null ? " = {$this->default}" : '';
39+
return trim("{$this->name}{$bound}{$default} {$this->description}");
3540
}
3641

3742
}

src/Parser/PhpDocParser.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,15 @@ private function parseTemplateTagValue(TokenIterator $tokens): Ast\PhpDoc\Templa
389389
$bound = null;
390390
}
391391

392+
if ($tokens->tryConsumeTokenValue('=')) {
393+
$default = $this->typeParser->parse($tokens);
394+
} else {
395+
$default = null;
396+
}
397+
392398
$description = $this->parseOptionalDescription($tokens);
393399

394-
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description);
400+
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description, $default);
395401
}
396402

397403
private function parseExtendsTagValue(string $tagName, TokenIterator $tokens): Ast\PhpDoc\PhpDocTagValueNode

tests/PHPStan/Parser/PhpDocParserTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,22 @@ public function provideTemplateTagsData(): Iterator
33413341
),
33423342
]),
33433343
];
3344+
3345+
yield [
3346+
'OK with default',
3347+
'/** @template T = string */',
3348+
new PhpDocNode([
3349+
new PhpDocTagNode(
3350+
'@template',
3351+
new TemplateTagValueNode(
3352+
'T',
3353+
null,
3354+
'',
3355+
new IdentifierTypeNode('string')
3356+
)
3357+
),
3358+
]),
3359+
];
33443360
}
33453361

33463362
public function provideExtendsTagsData(): Iterator

0 commit comments

Comments
 (0)