Skip to content

Commit c80c5c2

Browse files
committed
Refactor into smaller methods
1 parent 5d78276 commit c80c5c2

File tree

2 files changed

+71
-35
lines changed

2 files changed

+71
-35
lines changed

src/Factory/InputFieldsConfigurationFactory.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function methodToConfiguration(ReflectionMethod $method): ?array
4141

4242
if (!$field->type instanceof Type) {
4343
$this->convertTypeDeclarationsToInstances($method, $field);
44-
$this->completeField($method, $param, $field);
44+
$this->completeField($field, $method, $param);
4545
}
4646

4747
return $field->toArray();
@@ -61,11 +61,13 @@ private function convertTypeDeclarationsToInstances(ReflectionMethod $method, In
6161
/**
6262
* Complete field with info from doc blocks and type hints
6363
*
64+
* @param Input $field
6465
* @param ReflectionMethod $method
6566
* @param ReflectionParameter $param
66-
* @param Input $field
67+
*
68+
* @throws \GraphQL\Doctrine\Exception
6769
*/
68-
private function completeField(ReflectionMethod $method, ReflectionParameter $param, Input $field): void
70+
private function completeField(Input $field, ReflectionMethod $method, ReflectionParameter $param): void
6971
{
7072
$fieldName = lcfirst(preg_replace('~^set~', '', $method->getName()));
7173
if (!$field->name) {
@@ -77,14 +79,40 @@ private function completeField(ReflectionMethod $method, ReflectionParameter $pa
7779
$field->description = $docBlock->getMethodDescription();
7880
}
7981

82+
$this->completeFieldDefaultValue($field, $param, $fieldName);
83+
$this->completeFieldType($field, $method, $param, $docBlock);
84+
}
85+
86+
/**
87+
* Complete field default value from argument and property
88+
*
89+
* @param Input $field
90+
* @param ReflectionParameter $param
91+
* @param string $fieldName
92+
*/
93+
private function completeFieldDefaultValue(Input $field, ReflectionParameter $param, string $fieldName): void
94+
{
8095
if (!isset($field->defaultValue) && $param->isDefaultValueAvailable()) {
8196
$field->defaultValue = $param->getDefaultValue();
8297
}
8398

8499
if (!isset($field->defaultValue)) {
85100
$field->defaultValue = $this->getPropertyDefaultValue($fieldName);
86101
}
102+
}
87103

104+
/**
105+
* Complete field type from doc blocks and type hints
106+
*
107+
* @param Input $field
108+
* @param ReflectionMethod $method
109+
* @param ReflectionParameter $param
110+
* @param DocBlockReader $docBlock
111+
*
112+
* @throws \GraphQL\Doctrine\Exception
113+
*/
114+
private function completeFieldType(Input $field, ReflectionMethod $method, ReflectionParameter $param, DocBlockReader $docBlock): void
115+
{
88116
// If still no type, look for docblock
89117
if (!$field->type) {
90118
$typeDeclaration = $docBlock->getParameterType($param);

src/Factory/OutputFieldsConfigurationFactory.php

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function methodToConfiguration(ReflectionMethod $method): ?array
3636

3737
if (!$field->type instanceof Type) {
3838
$this->convertTypeDeclarationsToInstances($method, $field);
39-
$this->completeField($method, $field);
39+
$this->completeField($field, $method);
4040
}
4141

4242
return $field->toArray();
@@ -62,12 +62,12 @@ private function convertTypeDeclarationsToInstances(ReflectionMethod $method, Fi
6262
/**
6363
* Complete field with info from doc blocks and type hints
6464
*
65-
* @param ReflectionMethod $method
6665
* @param Field $field
66+
* @param ReflectionMethod $method
6767
*
6868
* @throws Exception
6969
*/
70-
private function completeField(ReflectionMethod $method, Field $field): void
70+
private function completeField(Field $field, ReflectionMethod $method): void
7171
{
7272
$fieldName = lcfirst(preg_replace('~^get~', '', $method->getName()));
7373
if (!$field->name) {
@@ -79,45 +79,23 @@ private function completeField(ReflectionMethod $method, Field $field): void
7979
$field->description = $docBlock->getMethodDescription();
8080
}
8181

82-
if ($this->isIdentityField($fieldName)) {
83-
$field->type = Type::nonNull(Type::id());
84-
}
85-
86-
// If still no type, look for docblock
87-
if (!$field->type) {
88-
$field->type = $this->getTypeFromDocBock($method, $docBlock);
89-
}
90-
91-
// If still no type, look for type hint
92-
if (!$field->type) {
93-
$field->type = $this->getTypeFromReturnTypeHint($method, $fieldName);
94-
}
95-
96-
// If still no args, look for type hint
97-
$field->args = $this->getArgumentsFromTypeHint($method, $field->args, $docBlock);
98-
99-
// If still no type, cannot continue
100-
if (!$field->type) {
101-
throw new Exception('Could not find type for method ' . $this->getMethodFullName($method) . '. Either type hint the return value, or specify the type with `@API\Field` annotation.');
102-
}
82+
$this->completeFieldArguments($field, $method, $docBlock);
83+
$this->completeFieldType($field, $method, $fieldName, $docBlock);
10384
}
10485

10586
/**
10687
* Complete arguments configuration from existing type hints
10788
*
89+
* @param Field $field
10890
* @param ReflectionMethod $method
109-
* @param Argument[] $argsFromAnnotations
11091
* @param DocBlockReader $docBlock
111-
*
112-
* @throws Exception
113-
*
114-
* @return array
11592
*/
116-
private function getArgumentsFromTypeHint(ReflectionMethod $method, array $argsFromAnnotations, DocBlockReader $docBlock): array
93+
private function completeFieldArguments(Field $field, ReflectionMethod $method, DocBlockReader $docBlock): void
11794
{
95+
$argsFromAnnotations = $field->args;
11896
$args = [];
11997
foreach ($method->getParameters() as $param) {
120-
//Either get existing, or create new argument
98+
// Either get existing, or create new argument
12199
$arg = $argsFromAnnotations[$param->getName()] ?? new Argument();
122100
$args[$param->getName()] = $arg;
123101

@@ -129,7 +107,7 @@ private function getArgumentsFromTypeHint(ReflectionMethod $method, array $argsF
129107
throw new Exception('The following arguments were declared via `@API\Argument` annotation but do not match actual parameter names on method ' . $this->getMethodFullName($method) . '. Either rename or remove the annotations: ' . implode(', ', $extraAnnotations));
130108
}
131109

132-
return $args;
110+
$field->args = $args;
133111
}
134112

135113
/**
@@ -193,4 +171,34 @@ private function getTypeFromDocBock(ReflectionMethod $method, DocBlockReader $do
193171

194172
return null;
195173
}
174+
175+
/**
176+
* Complete field type from doc blocks and type hints
177+
*
178+
* @param Field $field
179+
* @param ReflectionMethod $method
180+
* @param string $fieldName
181+
* @param DocBlockReader $docBlock
182+
*/
183+
private function completeFieldType(Field $field, ReflectionMethod $method, string $fieldName, DocBlockReader $docBlock): void
184+
{
185+
if ($this->isIdentityField($fieldName)) {
186+
$field->type = Type::nonNull(Type::id());
187+
}
188+
189+
// If still no type, look for docblock
190+
if (!$field->type) {
191+
$field->type = $this->getTypeFromDocBock($method, $docBlock);
192+
}
193+
194+
// If still no type, look for type hint
195+
if (!$field->type) {
196+
$field->type = $this->getTypeFromReturnTypeHint($method, $fieldName);
197+
}
198+
199+
// If still no type, cannot continue
200+
if (!$field->type) {
201+
throw new Exception('Could not find type for method ' . $this->getMethodFullName($method) . '. Either type hint the return value, or specify the type with `@API\Field` annotation.');
202+
}
203+
}
196204
}

0 commit comments

Comments
 (0)