|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of phpDocumentor. |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + * |
| 8 | + * @link http://phpdoc.org |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace phpDocumentor\Reflection; |
| 15 | + |
| 16 | +use phpDocumentor\Reflection\DocBlock\DescriptionFactory; |
| 17 | +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; |
| 18 | +use phpDocumentor\Reflection\DocBlock\Tags\Param; |
| 19 | +use phpDocumentor\Reflection\DocBlock\Tags\Return_; |
| 20 | +use phpDocumentor\Reflection\PhpStan\TagFactory; |
| 21 | +use phpDocumentor\Reflection\PseudoTypes\ArrayShape; |
| 22 | +use phpDocumentor\Reflection\PseudoTypes\ArrayShapeItem; |
| 23 | +use phpDocumentor\Reflection\Types\Context; |
| 24 | +use PHPStan\PhpDoc\Tag\ParamTag; |
| 25 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; |
| 26 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
| 27 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode; |
| 28 | +use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode; |
| 29 | +use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; |
| 30 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 31 | +use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 32 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 33 | +use PHPStan\PhpDocParser\Parser\ConstExprParser; |
| 34 | +use PHPStan\PhpDocParser\Parser\PhpDocParser; |
| 35 | +use PHPStan\PhpDocParser\Parser\TokenIterator; |
| 36 | +use PHPStan\PhpDocParser\Parser\TypeParser; |
| 37 | +use Webmozart\Assert\Assert; |
| 38 | +use InvalidArgumentException; |
| 39 | +use LogicException; |
| 40 | + |
| 41 | +final class PhpStanDocblockFactory implements DocBlockFactoryInterface |
| 42 | +{ |
| 43 | + private PhpDocParser $parser; |
| 44 | + private Lexer $lexer; |
| 45 | + private DescriptionFactory $descriptionFactory; |
| 46 | + private TypeResolver $typeResolver; |
| 47 | + |
| 48 | + private function __construct() |
| 49 | + { |
| 50 | + } |
| 51 | + |
| 52 | + public static function createInstance(array $additionalTags = []): DocBlockFactoryInterface |
| 53 | + { |
| 54 | + $fqsenResolver = new FqsenResolver(); |
| 55 | + $constExprParser = new ConstExprParser(); |
| 56 | + $self = new self(); |
| 57 | + $self->lexer = new Lexer(); |
| 58 | + $self->parser = new PhpDocParser( |
| 59 | + new TypeParser($constExprParser), |
| 60 | + $constExprParser |
| 61 | + ); |
| 62 | + |
| 63 | + $self->descriptionFactory = new DescriptionFactory(new TagFactory()); |
| 64 | + $self->typeResolver = new TypeResolver($fqsenResolver); |
| 65 | + |
| 66 | + return $self; |
| 67 | + } |
| 68 | + |
| 69 | + public function create($docblock, ?Types\Context $context = null, ?Location $location = null): DocBlock |
| 70 | + { |
| 71 | + if (is_object($docblock)) { |
| 72 | + if (!method_exists($docblock, 'getDocComment')) { |
| 73 | + $exceptionMessage = 'Invalid object passed; the given object must support the getDocComment method'; |
| 74 | + |
| 75 | + throw new InvalidArgumentException($exceptionMessage); |
| 76 | + } |
| 77 | + |
| 78 | + $docblock = $docblock->getDocComment(); |
| 79 | + Assert::string($docblock); |
| 80 | + } |
| 81 | + |
| 82 | + Assert::stringNotEmpty($docblock); |
| 83 | + |
| 84 | + $tokens = $this->lexer->tokenize($docblock); |
| 85 | + $ast = $this->parser->parse(new TokenIterator($tokens)); |
| 86 | + |
| 87 | + $textNodes = []; |
| 88 | + |
| 89 | + foreach ($ast->children as $child) { |
| 90 | + if ($child instanceof PhpDocTextNode) { |
| 91 | + $textNodes[] = $child->text; |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + //If node is not a text node this is the end of description; |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + $tags = []; |
| 100 | + foreach ($ast->getTags() as $node) { |
| 101 | + switch ($node->name) { |
| 102 | + case '@param': |
| 103 | + $tag = $node->value; |
| 104 | + $tags[] = new Param( |
| 105 | + ltrim($tag->parameterName, '$'), |
| 106 | + $this->createType($tag->type, $context), |
| 107 | + $tag->isVariadic, |
| 108 | + $this->descriptionFactory->create($tag->description), |
| 109 | + $tag->isReference |
| 110 | + ); |
| 111 | + break; |
| 112 | + case '@return': |
| 113 | + $tag = $node->value; |
| 114 | + $tags[] = new Return_( |
| 115 | + $this->createType($tag->type, $context), |
| 116 | + $this->descriptionFactory->create($tag->description) |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return new DocBlock( |
| 122 | + '', |
| 123 | + $this->descriptionFactory->create( |
| 124 | + implode("\n", $textNodes) |
| 125 | + ), |
| 126 | + $tags, |
| 127 | + null, |
| 128 | + $location |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + private function createType(TypeNode $type, Context $context) |
| 133 | + { |
| 134 | + switch (get_class($type)) { |
| 135 | + case IdentifierTypeNode::class: |
| 136 | + return $this->typeResolver->resolve($type->name, $context); |
| 137 | + case ArrayShapeNode::class: |
| 138 | + return new ArrayShape( |
| 139 | + ... array_map( |
| 140 | + fn(ArrayShapeItemNode $item) => new ArrayShapeItem( |
| 141 | + (string) $item->keyName, |
| 142 | + $this->createType($item->valueType, $context), |
| 143 | + $item->optional |
| 144 | + ), |
| 145 | + $type->items |
| 146 | + ) |
| 147 | + ); |
| 148 | + default: |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments