Skip to content

Commit a46da81

Browse files
committed
Add Template tag
1 parent 6908c99 commit a46da81

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
6+
7+
use Webmozart\Assert\Assert;
8+
use phpDocumentor\Reflection\DocBlock\Tag;
9+
use phpDocumentor\Reflection\TypeResolver;
10+
use phpDocumentor\Reflection\Types\Context;
11+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
12+
use phpDocumentor\Reflection\DocBlock\Tags\Template;
13+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
14+
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
15+
16+
/**
17+
* @internal This class is not part of the BC promise of this library.
18+
*/
19+
final class TemplateFactory implements PHPStanFactory
20+
{
21+
private DescriptionFactory $descriptionFactory;
22+
private TypeResolver $typeResolver;
23+
24+
public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
25+
{
26+
$this->descriptionFactory = $descriptionFactory;
27+
$this->typeResolver = $typeResolver;
28+
}
29+
30+
public function create(PhpDocTagNode $node, Context $context): Tag
31+
{
32+
$tagValue = $node->value;
33+
Assert::isInstanceOf($tagValue, TemplateTagValueNode::class);
34+
35+
$description = $tagValue->getAttribute('description');
36+
if (is_string($description) === false) {
37+
$description = $tagValue->description;
38+
}
39+
40+
return new Template(
41+
$tagValue->name,
42+
$this->typeResolver->createType($tagValue->bound, $context),
43+
$this->typeResolver->createType($tagValue->default, $context),
44+
$this->descriptionFactory->create($tagValue->description, $context)
45+
);
46+
}
47+
48+
public function supports(PhpDocTagNode $node, Context $context): bool
49+
{
50+
return $node->value instanceof TemplateTagValueNode && $node->name === '@template';
51+
}
52+
}

src/DocBlock/Tags/Template.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\DocBlock\Tags;
15+
16+
use Doctrine\Deprecations\Deprecation;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\DocBlock\Description;
19+
use phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
20+
21+
/**
22+
* Reflection class for a {@}template tag in a Docblock.
23+
*/
24+
final class Template extends TagWithType
25+
{
26+
/** @var non-empty-string */
27+
private $templateName;
28+
29+
/** @var ?Type */
30+
private $bound;
31+
32+
/** @var ?Type */
33+
private $default;
34+
35+
public function __construct(string $templateName, ?Type $bound = null, ?Type $default = null, ?Description $description = null)
36+
{
37+
$this->name = 'template';
38+
$this->templateName = $templateName;
39+
$this->bound = $bound;
40+
$this->default = $default;
41+
$this->description = $description;
42+
}
43+
44+
/**
45+
* @deprecated Create using static factory is deprecated,
46+
* this method should not be called directly by library consumers
47+
*/
48+
public static function create(string $body)
49+
{
50+
Deprecation::trigger(
51+
'phpdocumentor/reflection-docblock',
52+
'https://github.com/phpDocumentor/ReflectionDocBlock/issues/361',
53+
'Create using static factory is deprecated, this method should not be called directly
54+
by library consumers',
55+
);
56+
return null;
57+
}
58+
59+
public function getTemplateName(): string
60+
{
61+
return $this->templateName;
62+
}
63+
64+
public function getBound(): ?Type
65+
{
66+
return $this->bound;
67+
}
68+
69+
public function getDefault(): ?Type
70+
{
71+
return $this->default;
72+
}
73+
74+
public function __toString(): string
75+
{
76+
$bound = $this->bound !== null ? " of {$this->bound}" : '';
77+
$default = $this->default !== null ? " = {$this->default}" : '';
78+
79+
if ($this->description) {
80+
$description = $this->description->render();
81+
} else {
82+
$description = '';
83+
}
84+
85+
return $this->templateName . $bound . $default . ($description !== '' ? ' ' . $description : '');
86+
}
87+
}

0 commit comments

Comments
 (0)