Add Template tag

This commit is contained in:
AhJ
2024-07-24 12:09:41 +03:30
parent 6908c99a87
commit a46da81e77
2 changed files with 139 additions and 0 deletions
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use Webmozart\Assert\Assert;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use phpDocumentor\Reflection\DocBlock\Tags\Template;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
/**
* @internal This class is not part of the BC promise of this library.
*/
final class TemplateFactory implements PHPStanFactory
{
private DescriptionFactory $descriptionFactory;
private TypeResolver $typeResolver;
public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory)
{
$this->descriptionFactory = $descriptionFactory;
$this->typeResolver = $typeResolver;
}
public function create(PhpDocTagNode $node, Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, TemplateTagValueNode::class);
$description = $tagValue->getAttribute('description');
if (is_string($description) === false) {
$description = $tagValue->description;
}
return new Template(
$tagValue->name,
$this->typeResolver->createType($tagValue->bound, $context),
$this->typeResolver->createType($tagValue->default, $context),
$this->descriptionFactory->create($tagValue->description, $context)
);
}
public function supports(PhpDocTagNode $node, Context $context): bool
{
return $node->value instanceof TemplateTagValueNode && $node->name === '@template';
}
}
+87
View File
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags;
use Doctrine\Deprecations\Deprecation;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
/**
* Reflection class for a {@}template tag in a Docblock.
*/
final class Template extends TagWithType
{
/** @var non-empty-string */
private $templateName;
/** @var ?Type */
private $bound;
/** @var ?Type */
private $default;
public function __construct(string $templateName, ?Type $bound = null, ?Type $default = null, ?Description $description = null)
{
$this->name = 'template';
$this->templateName = $templateName;
$this->bound = $bound;
$this->default = $default;
$this->description = $description;
}
/**
* @deprecated Create using static factory is deprecated,
* this method should not be called directly by library consumers
*/
public static function create(string $body)
{
Deprecation::trigger(
'phpdocumentor/reflection-docblock',
'https://github.com/phpDocumentor/ReflectionDocBlock/issues/361',
'Create using static factory is deprecated, this method should not be called directly
by library consumers',
);
return null;
}
public function getTemplateName(): string
{
return $this->templateName;
}
public function getBound(): ?Type
{
return $this->bound;
}
public function getDefault(): ?Type
{
return $this->default;
}
public function __toString(): string
{
$bound = $this->bound !== null ? " of {$this->bound}" : '';
$default = $this->default !== null ? " = {$this->default}" : '';
if ($this->description) {
$description = $this->description->render();
} else {
$description = '';
}
return $this->templateName . $bound . $default . ($description !== '' ? ' ' . $description : '');
}
}