1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Andi\GraphQL\ArgumentResolver\Middleware; |
||
6 | |||
7 | use Andi\GraphQL\ArgumentResolver\ArgumentResolverInterface; |
||
8 | use Andi\GraphQL\Attribute\Argument; |
||
9 | use Andi\GraphQL\Common\LazyParserType; |
||
10 | use Andi\GraphQL\Common\LazyTypeByReflectionParameter; |
||
11 | use Andi\GraphQL\Exception\CantResolveGraphQLTypeException; |
||
12 | use Andi\GraphQL\TypeRegistryInterface; |
||
13 | use phpDocumentor\Reflection\DocBlock\Tags\Param; |
||
14 | use phpDocumentor\Reflection\DocBlockFactory; |
||
15 | use ReflectionParameter; |
||
16 | use Spiral\Attributes\ReaderInterface; |
||
17 | |||
18 | final class ReflectionParameterMiddleware implements MiddlewareInterface |
||
19 | { |
||
20 | public const PRIORITY = 3072; |
||
21 | |||
22 | 11 | public function __construct( |
|
23 | private readonly ReaderInterface $reader, |
||
24 | private readonly TypeRegistryInterface $typeRegistry, |
||
25 | ) { |
||
26 | 11 | } |
|
27 | |||
28 | 10 | public function process(mixed $argument, ArgumentResolverInterface $argumentResolver): array |
|
29 | { |
||
30 | 10 | if (! $argument instanceof ReflectionParameter) { |
|
31 | 1 | return $argumentResolver->resolve($argument); |
|
32 | } |
||
33 | |||
34 | 9 | $attribute = $this->reader->firstParameterMetadata($argument, Argument::class); |
|
35 | |||
36 | 9 | $config = [ |
|
37 | 9 | 'name' => $this->getArgumentName($argument, $attribute), |
|
38 | 9 | 'description' => $this->getArgumentDescription($argument, $attribute), |
|
39 | 9 | 'type' => $this->getArgumentType($argument, $attribute), |
|
40 | 9 | 'deprecationReason' => $this->getArgumentDeprecationReason($argument, $attribute), |
|
41 | 9 | ]; |
|
42 | |||
43 | 8 | if ($this->hasDefaultValue($argument, $attribute)) { |
|
44 | 4 | $config['defaultValue'] = $this->getArgumentDefaultValue($argument, $attribute); |
|
45 | } |
||
46 | |||
47 | 8 | return $config; |
|
48 | } |
||
49 | |||
50 | 9 | private function getArgumentName(ReflectionParameter $parameter, ?Argument $attribute): string |
|
51 | { |
||
52 | 9 | return $attribute?->name |
|
53 | 9 | ?? $parameter->getName(); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param ReflectionParameter $parameter |
||
58 | * @param Argument|null $attribute |
||
59 | * |
||
60 | * @return string|null |
||
61 | */ |
||
62 | 9 | private function getArgumentDescription(ReflectionParameter $parameter, ?Argument $attribute): ?string |
|
63 | { |
||
64 | 9 | if ($attribute?->description) { |
|
65 | 1 | return $attribute->description; |
|
66 | } |
||
67 | |||
68 | 8 | if ($docComment = $parameter->getDeclaringFunction()->getDocComment()) { |
|
69 | 3 | $docBlock = DocBlockFactory::createInstance(['psalm-param' => Param::class])->create($docComment); |
|
70 | 3 | foreach ($docBlock->getTags() as $tag) { |
|
71 | 3 | if ($tag instanceof Param && $tag->getVariableName() === $parameter->getName()) { |
|
72 | 3 | return (string) $tag->getDescription() ?: null; |
|
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | 5 | return null; |
|
78 | } |
||
79 | |||
80 | 9 | private function getArgumentType(ReflectionParameter $parameter, ?Argument $attribute): callable |
|
81 | { |
||
82 | 9 | if ($attribute?->type) { |
|
83 | 4 | return new LazyParserType($attribute->type, $attribute->mode ?? 0, $this->typeRegistry); |
|
84 | } |
||
85 | |||
86 | 5 | if (! $parameter->hasType()) { |
|
87 | 1 | throw new CantResolveGraphQLTypeException(\sprintf( |
|
88 | 1 | 'Can\'t resolve GraphQL type for argument "%s"', |
|
89 | 1 | $parameter->getName() |
|
90 | 1 | )); |
|
91 | } |
||
92 | |||
93 | 4 | return new LazyTypeByReflectionParameter($parameter, $this->typeRegistry); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param ReflectionParameter $parameter |
||
98 | * @param Argument|null $attribute |
||
99 | * |
||
100 | * @return string|null |
||
101 | */ |
||
102 | 8 | private function getArgumentDeprecationReason(ReflectionParameter $parameter, ?Argument $attribute): ?string |
|
0 ignored issues
–
show
|
|||
103 | { |
||
104 | 8 | return $attribute?->deprecationReason; |
|
105 | } |
||
106 | |||
107 | 8 | private function hasDefaultValue(ReflectionParameter $parameter, ?Argument $attribute): bool |
|
108 | { |
||
109 | 8 | return $attribute?->hasDefaultValue() || $parameter->isDefaultValueAvailable(); |
|
110 | } |
||
111 | |||
112 | 4 | private function getArgumentDefaultValue(ReflectionParameter $parameter, ?Argument $attribute): mixed |
|
113 | { |
||
114 | 4 | if ($attribute?->hasDefaultValue()) { |
|
115 | 2 | return $attribute->defaultValue; |
|
116 | } |
||
117 | |||
118 | 2 | return $parameter->getDefaultValue(); |
|
119 | } |
||
120 | } |
||
121 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.