Debian Codegraph @

vendor/symfony/type-info/TypeResolver/PhpDocAwareReflectionTypeResolver.php

raw
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 PHPStan\PhpDocParser\ParserConfig;
23 use Symfony\Component\TypeInfo\Exception\UnsupportedException;
24 use Symfony\Component\TypeInfo\Type;
25 use Symfony\Component\TypeInfo\TypeContext\TypeContext;
26 use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
27
28 /**
29 * Resolves type on reflection prioriziting PHP documentation.
30 *
31 * @author Mathias Arlaud <[email protected]>
32 */
33 final class PhpDocAwareReflectionTypeResolver implements TypeResolverInterface
34 {
35 private readonly PhpDocParser $phpDocParser;
36 private readonly Lexer $lexer;
37
38 public function __construct(
39 private readonly TypeResolverInterface $reflectionTypeResolver,
40 private readonly TypeResolverInterface $stringTypeResolver,
41 private readonly TypeContextFactory $typeContextFactory,
42 ?PhpDocParser $phpDocParser = null,
43 ?Lexer $lexer = null,
44 ) {
45 if (class_exists(ParserConfig::class)) {
46 $this->lexer = $lexer ?? new Lexer(new ParserConfig([]));
47 $this->phpDocParser = $phpDocParser ?? new PhpDocParser(
48 $config = new ParserConfig([]),
49 new TypeParser($config, $constExprParser = new ConstExprParser($config)),
50 $constExprParser,
51 );
52 } else {
53 $this->lexer = $lexer ?? new Lexer();
54 $this->phpDocParser = $phpDocParser ?? new PhpDocParser(
55 new TypeParser($constExprParser = new ConstExprParser()),
56 $constExprParser,
57 );
58 }
59 }
60
61 public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type
62 {
63 if (!$subject instanceof \ReflectionProperty && !$subject instanceof \ReflectionParameter && !$subject instanceof \ReflectionFunctionAbstract) {
64 throw new UnsupportedException(\sprintf('Expected subject to be a "ReflectionProperty", a "ReflectionParameter" or a "ReflectionFunctionAbstract", "%s" given.', get_debug_type($subject)), $subject);
65 }
66
67 $typeContext ??= $this->typeContextFactory->createFromReflection($subject);
68
69 $docComments = match (true) {
70 $subject instanceof \ReflectionProperty => $subject->isPromoted()
71 ? ['@var' => $subject->getDocComment(), '@param' => $subject->getDeclaringClass()?->getConstructor()?->getDocComment()]
72 : ['@var' => $subject->getDocComment()],
73 $subject instanceof \ReflectionParameter => ['@param' => $subject->getDeclaringFunction()->getDocComment()],
74 $subject instanceof \ReflectionFunctionAbstract => ['@return' => $subject->getDocComment()],
75 };
76
77 foreach ($docComments as $tagName => $docComment) {
78 if (!$docComment) {
79 continue;
80 }
81
82 $tokens = new TokenIterator($this->lexer->tokenize($docComment));
83 $docNode = $this->phpDocParser->parse($tokens);
84
85 foreach ($docNode->getTagsByName($tagName) as $tag) {
86 $tagValue = $tag->value;
87
88 if ('@var' === $tagName && $tagValue instanceof VarTagValueNode) {
89 return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext);
90 }
91
92 if ('@param' === $tagName && $tagValue instanceof ParamTagValueNode && '$'.$subject->getName() === $tagValue->parameterName) {
93 return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext);
94 }
95
96 if ('@return' === $tagName && $tagValue instanceof ReturnTagValueNode) {
97 return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext);
98 }
99 }
100 }
101
102 return $this->reflectionTypeResolver->resolve($subject);
103 }
104 }