From a46da81e776e18aff722274d95bb639c5edc4a23 Mon Sep 17 00:00:00 2001 From: AhJ Date: Wed, 24 Jul 2024 12:09:41 +0330 Subject: [PATCH] Add Template tag --- src/DocBlock/Tags/Factory/TemplateFactory.php | 52 +++++++++++ src/DocBlock/Tags/Template.php | 87 +++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/DocBlock/Tags/Factory/TemplateFactory.php create mode 100644 src/DocBlock/Tags/Template.php diff --git a/src/DocBlock/Tags/Factory/TemplateFactory.php b/src/DocBlock/Tags/Factory/TemplateFactory.php new file mode 100644 index 0000000..63e0b03 --- /dev/null +++ b/src/DocBlock/Tags/Factory/TemplateFactory.php @@ -0,0 +1,52 @@ +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'; + } +} diff --git a/src/DocBlock/Tags/Template.php b/src/DocBlock/Tags/Template.php new file mode 100644 index 0000000..b2df710 --- /dev/null +++ b/src/DocBlock/Tags/Template.php @@ -0,0 +1,87 @@ +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 : ''); + } +}