Skip to content

Commit ecb475b

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Implement template default syntax
1 parent 19d4770 commit ecb475b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

Diff for: 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
}

Diff for: src/Parser/PhpDocParser.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,15 @@ private function parseTemplateTagValue(TokenIterator $tokens): Ast\PhpDoc\Templa
402402
$bound = null;
403403
}
404404

405+
if ($tokens->tryConsumeTokenValue('=')) {
406+
$default = $this->typeParser->parse($tokens);
407+
} else {
408+
$default = null;
409+
}
410+
405411
$description = $this->parseOptionalDescription($tokens);
406412

407-
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description);
413+
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description, $default);
408414
}
409415

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

Diff for: tests/PHPStan/Parser/PhpDocParserTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -3345,6 +3345,22 @@ public function provideTemplateTagsData(): Iterator
33453345
),
33463346
]),
33473347
];
3348+
3349+
yield [
3350+
'OK with default',
3351+
'/** @template T = string */',
3352+
new PhpDocNode([
3353+
new PhpDocTagNode(
3354+
'@template',
3355+
new TemplateTagValueNode(
3356+
'T',
3357+
null,
3358+
'',
3359+
new IdentifierTypeNode('string')
3360+
)
3361+
),
3362+
]),
3363+
];
33483364
}
33493365

33503366
public function provideExtendsTagsData(): Iterator

0 commit comments

Comments
 (0)