|
| 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\Extends_; |
| 13 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode; |
| 14 | +use phpDocumentor\Reflection\DocBlock\DescriptionFactory; |
| 15 | +use phpDocumentor\Reflection\DocBlock\Tags\TemplateExtends; |
| 16 | + |
| 17 | +/** |
| 18 | + * @internal This class is not part of the BC promise of this library. |
| 19 | + */ |
| 20 | +class ExtendsFactory implements PHPStanFactory |
| 21 | +{ |
| 22 | + private DescriptionFactory $descriptionFactory; |
| 23 | + private TypeResolver $typeResolver; |
| 24 | + |
| 25 | + public function __construct(TypeResolver $typeResolver, DescriptionFactory $descriptionFactory) |
| 26 | + { |
| 27 | + $this->descriptionFactory = $descriptionFactory; |
| 28 | + $this->typeResolver = $typeResolver; |
| 29 | + } |
| 30 | + |
| 31 | + public function create(PhpDocTagNode $node, Context $context): Tag |
| 32 | + { |
| 33 | + $tagValue = $node->value; |
| 34 | + Assert::isInstanceOf($tagValue, ExtendsTagValueNode::class); |
| 35 | + |
| 36 | + $description = $tagValue->getAttribute('description'); |
| 37 | + if (is_string($description) === false) { |
| 38 | + $description = $tagValue->description; |
| 39 | + } |
| 40 | + |
| 41 | + $class = $node->name === '@extends' ? Extends_::class : TemplateExtends::class; |
| 42 | + |
| 43 | + return new $class( |
| 44 | + $this->typeResolver->createType($tagValue->type, $context), |
| 45 | + $this->descriptionFactory->create($tagValue->description, $context) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function supports(PhpDocTagNode $node, Context $context): bool |
| 50 | + { |
| 51 | + return $node->value instanceof ExtendsTagValueNode && ($node->name === '@extends' || $node->name === '@template-extends'); |
| 52 | + } |
| 53 | +} |
0 commit comments