-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathInputTypeUtils.php
More file actions
153 lines (129 loc) · 4.69 KB
/
InputTypeUtils.php
File metadata and controls
153 lines (129 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InputType;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Object_;
use phpDocumentor\Reflection\Types\Self_;
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use RuntimeException;
use TheCodingMachine\GraphQLite\Parameters\ExpandsInputTypeParameters;
use TheCodingMachine\GraphQLite\Parameters\InputTypeParameterInterface;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
use function array_map;
use function assert;
use function ltrim;
/**
* @phpstan-import-type FieldConfig from InputObjectType
* @phpstan-import-type ArgumentConfig from Argument
* @phpstan-import-type InputObjectFieldConfig from InputObjectField
*/
class InputTypeUtils
{
public function __construct(
private readonly AnnotationReader $annotationReader,
private readonly NamingStrategyInterface $namingStrategy,
)
{
}
/**
* Returns an array with 2 elements: [ $inputName, $className ]
*
* @return array{0: string, 1:class-string<object>}
*/
public function getInputTypeNameAndClassName(ReflectionMethod $method): array
{
/** @var class-string<object> $fqsen */
$fqsen = ltrim((string) $this->validateReturnType($method), '\\');
$factory = $this->annotationReader->getFactoryAnnotation($method);
if ($factory === null) {
throw new RuntimeException($method->getDeclaringClass()->getName() . '::' . $method->getName() . ' has no @Factory annotation.');
}
return [$this->namingStrategy->getInputTypeName($fqsen, $factory), $fqsen];
}
private function validateReturnType(ReflectionMethod $refMethod): Fqsen
{
$returnType = $refMethod->getReturnType();
if ($returnType === null) {
throw MissingTypeHintRuntimeException::missingReturnType($refMethod);
}
assert($returnType instanceof ReflectionNamedType);
if ($returnType->allowsNull()) {
throw MissingTypeHintRuntimeException::nullableReturnType($refMethod);
}
$type = $returnType->getName();
$typeResolver = new TypeResolver();
$phpdocType = $typeResolver->resolve($type);
$phpdocType = $this->resolveSelf($phpdocType, $refMethod->getDeclaringClass());
if (! $phpdocType instanceof Object_) {
throw MissingTypeHintRuntimeException::invalidReturnType($refMethod);
}
$fqsen = $phpdocType->getFqsen();
assert($fqsen !== null);
return $fqsen;
}
/**
* Resolves "self" types into the class type.
*
* @param ReflectionClass<object> $reflectionClass
*/
private function resolveSelf(Type $type, ReflectionClass $reflectionClass): Type
{
if ($type instanceof Self_) {
return new Object_(new Fqsen('\\' . $reflectionClass->getName()));
}
return $type;
}
/**
* @param array<string, ParameterInterface> $parameters
*
* @return array<string, InputTypeParameterInterface>
*/
public static function toInputParameters(array $parameters): array
{
$result = [];
foreach ($parameters as $name => $parameter) {
if ($parameter instanceof InputTypeParameterInterface) {
$result[$name] = $parameter;
}
if (! ($parameter instanceof ExpandsInputTypeParameters)) {
continue;
}
$result = [
...$result,
...$parameter->toInputTypeParameters(),
];
}
return $result;
}
/**
* Maps an array of ParameterInterface to an array of field descriptors as accepted by Webonyx.
*
* @param ParameterInterface[] $args
*
* @return array{defaultValue?:mixed,type:\GraphQL\Type\Definition\Type&InputType}[]
*/
public static function getInputTypeArgs(array $args): array
{
$inputTypeArgs = self::toInputParameters($args);
return array_map(static function (InputTypeParameterInterface $parameter): array {
$desc = [
'type' => $parameter->getType(),
];
if ($parameter->hasDefaultValue()) {
$desc['defaultValue'] = $parameter->getDefaultValue();
}
if ($parameter->getDescription()) {
$desc['description'] = $parameter->getDescription();
}
return $desc;
}, $inputTypeArgs);
}
}