|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\TypeInfo\TypeResolver; |
| 13 | + |
| 14 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; |
| 15 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; |
| 16 | +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
| 17 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 18 | +use PHPStan\PhpDocParser\Parser\ConstExprParser; |
| 19 | +use PHPStan\PhpDocParser\Parser\PhpDocParser; |
| 20 | +use PHPStan\PhpDocParser\Parser\TokenIterator; |
| 21 | +use PHPStan\PhpDocParser\Parser\TypeParser; |
| 22 | +use Symfony\Component\TypeInfo\Exception\UnsupportedException; |
| 23 | +use Symfony\Component\TypeInfo\Type; |
| 24 | +use Symfony\Component\TypeInfo\TypeContext\TypeContext; |
| 25 | +use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory; |
| 26 | +use Symfony\Component\TypeInfo\TypeResolver\TypeResolverInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * Resolves type on reflection prioriziting PHP documentation. |
| 30 | + * |
| 31 | + * @author Mathias Arlaud <[email protected]> |
| 32 | + * |
| 33 | + * @internal |
| 34 | + */ |
| 35 | +final readonly class PhpDocAwareReflectionTypeResolver implements TypeResolverInterface |
| 36 | +{ |
| 37 | + private PhpDocParser $phpDocParser; |
| 38 | + private Lexer $lexer; |
| 39 | + |
| 40 | + public function __construct( |
| 41 | + private TypeResolverInterface $reflectionTypeResolver, |
| 42 | + private TypeResolverInterface $stringTypeResolver, |
| 43 | + private TypeContextFactory $typeContextFactory, |
| 44 | + ) { |
| 45 | + $this->phpDocParser = new PhpDocParser(new TypeParser(), new ConstExprParser()); |
| 46 | + $this->lexer = new Lexer(); |
| 47 | + } |
| 48 | + |
| 49 | + public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type |
| 50 | + { |
| 51 | + if (!$subject instanceof \ReflectionProperty && !$subject instanceof \ReflectionParameter && !$subject instanceof \ReflectionFunctionAbstract) { |
| 52 | + throw new UnsupportedException(sprintf('Expected subject to be a "ReflectionProperty", a "ReflectionParameter" or a "ReflectionFunctionAbstract", "%s" given.', get_debug_type($subject)), $subject); |
| 53 | + } |
| 54 | + |
| 55 | + $docComment = match (true) { |
| 56 | + $subject instanceof \ReflectionProperty => $subject->getDocComment(), |
| 57 | + $subject instanceof \ReflectionParameter => $subject->getDeclaringFunction()->getDocComment(), |
| 58 | + $subject instanceof \ReflectionFunctionAbstract => $subject->getDocComment(), |
| 59 | + }; |
| 60 | + |
| 61 | + if (!$docComment) { |
| 62 | + return $this->reflectionTypeResolver->resolve($subject); |
| 63 | + } |
| 64 | + |
| 65 | + $typeContext ??= $this->typeContextFactory->createFromReflection($subject); |
| 66 | + |
| 67 | + $tagName = match (true) { |
| 68 | + $subject instanceof \ReflectionProperty => '@var', |
| 69 | + $subject instanceof \ReflectionParameter => '@param', |
| 70 | + $subject instanceof \ReflectionFunctionAbstract => '@return', |
| 71 | + }; |
| 72 | + |
| 73 | + $tokens = new TokenIterator($this->lexer->tokenize($docComment)); |
| 74 | + $docNode = $this->phpDocParser->parse($tokens); |
| 75 | + |
| 76 | + foreach ($docNode->getTagsByName($tagName) as $tag) { |
| 77 | + $tagValue = $tag->value; |
| 78 | + |
| 79 | + if ( |
| 80 | + $tagValue instanceof VarTagValueNode |
| 81 | + || $tagValue instanceof ParamTagValueNode && $tagName && '$'.$subject->getName() === $tagValue->parameterName |
| 82 | + || $tagValue instanceof ReturnTagValueNode |
| 83 | + ) { |
| 84 | + return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return $this->reflectionTypeResolver->resolve($subject); |
| 89 | + } |
| 90 | +} |
0 commit comments