Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#736)
by Bart
11:07 queued 07:49
created

AnnotationParser   F

Complexity

Total Complexity 203

Size/Duplication

Total Lines 987
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 10
Bugs 1 Features 0
Metric Value
wmc 203
eloc 479
c 10
b 1
f 0
dl 0
loc 987
ccs 443
cts 460
cp 0.963
rs 2

31 Methods

Rating   Name   Duplication   Size   Complexity  
A preParse() 0 3 1
B resolveTypeFromPhpType() 0 16 8
B unionAnnotationToGQLConfiguration() 0 38 8
A fullyQualifiedClassName() 0 7 3
F getGraphQLFieldsFromProviders() 0 69 18
B graphQLTypeConfigFromAnnotation() 0 52 11
C typeAnnotationToGQLConfiguration() 0 46 13
F classAnnotationsToGQLConfiguration() 0 107 25
A getGraphQLTypeFieldsFromAnnotations() 0 9 2
A guessArgs() 0 26 5
A formatArgsForExpression() 0 8 2
B enumAnnotationToGQLConfiguration() 0 26 7
F getTypeFieldConfigurationFromReflector() 0 123 32
A formatNamespaceForExpression() 0 3 1
B resolveTypeFromDoctrineType() 0 22 11
B resolveGraphQLTypeFromReflectionType() 0 16 7
A suffixName() 0 3 2
A processFile() 0 27 5
A searchClassesMapBy() 0 14 4
A typeInterfaceAnnotationToGQLConfiguration() 0 13 1
A getFirstAnnotationMatching() 0 15 5
A getGraphClass() 0 5 1
A inputAnnotationToGQLConfiguration() 0 7 2
A scalarAnnotationToGQLConfiguration() 0 17 2
A reset() 0 5 1
A formatExpression() 0 3 2
A getDescriptionConfiguration() 0 16 4
A parse() 0 3 1
A resolveTypeFromClass() 0 11 4
B guessType() 0 44 9
A getGraphQLInputFieldsFromAnnotations() 0 34 6

How to fix   Complexity   

Complex Class

Complex classes like AnnotationParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AnnotationParser, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Config\Parser;
6
7
use Doctrine\Common\Annotations\AnnotationException;
8
use Doctrine\ORM\Mapping\Column;
9
use Doctrine\ORM\Mapping\JoinColumn;
10
use Doctrine\ORM\Mapping\ManyToMany;
11
use Doctrine\ORM\Mapping\ManyToOne;
12
use Doctrine\ORM\Mapping\OneToMany;
13
use Doctrine\ORM\Mapping\OneToOne;
14
use Exception;
15
use Overblog\GraphQLBundle\Annotation as GQL;
16
use Overblog\GraphQLBundle\Config\Parser\Annotation\GraphClass;
17
use Overblog\GraphQLBundle\Relay\Connection\ConnectionInterface;
18
use Overblog\GraphQLBundle\Relay\Connection\EdgeInterface;
19
use ReflectionException;
20
use ReflectionMethod;
21
use ReflectionNamedType;
22
use ReflectionProperty;
23
use Reflector;
24
use RuntimeException;
25
use SplFileInfo;
26
use Symfony\Component\Config\Resource\FileResource;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
29
use function array_filter;
30
use function array_keys;
31
use function array_map;
32
use function array_unshift;
33
use function current;
34
use function file_get_contents;
35
use function get_class;
36
use function implode;
37
use function in_array;
38
use function is_array;
39
use function is_string;
40
use function preg_match;
41
use function sprintf;
42
use function str_replace;
43
use function strlen;
44
use function strpos;
45
use function substr;
46
use function trim;
47
48
class AnnotationParser implements PreParserInterface
49
{
50
    private static array $classesMap = [];
51
    private static array $providers = [];
52
    private static array $doctrineMapping = [];
53
    private static array $graphClassCache = [];
54
55
    private const GQL_SCALAR = 'scalar';
56
    private const GQL_ENUM = 'enum';
57
    private const GQL_TYPE = 'type';
58
    private const GQL_INPUT = 'input';
59
    private const GQL_UNION = 'union';
60
    private const GQL_INTERFACE = 'interface';
61
62
    /**
63
     * @see https://facebook.github.io/graphql/draft/#sec-Input-and-Output-Types
64
     */
65
    private const VALID_INPUT_TYPES = [self::GQL_SCALAR, self::GQL_ENUM, self::GQL_INPUT];
66
    private const VALID_OUTPUT_TYPES = [self::GQL_SCALAR, self::GQL_TYPE, self::GQL_INTERFACE, self::GQL_UNION, self::GQL_ENUM];
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @throws InvalidArgumentException
72
     * @throws ReflectionException
73
     */
74 25
    public static function preParse(SplFileInfo $file, ContainerBuilder $container, array $configs = []): void
75
    {
76 25
        $container->setParameter('overblog_graphql_types.classes_map', self::processFile($file, $container, $configs, true));
77 25
    }
78
79
    /**
80
     * @throws InvalidArgumentException
81
     * @throws ReflectionException
82
     */
83 25
    public static function parse(SplFileInfo $file, ContainerBuilder $container, array $configs = []): array
84
    {
85 25
        return self::processFile($file, $container, $configs, false);
86
    }
87
88
    /**
89
     * @internal
90
     */
91 63
    public static function reset(): void
92
    {
93 63
        self::$classesMap = [];
94 63
        self::$providers = [];
95 63
        self::$graphClassCache = [];
96 63
    }
97
98
    /**
99
     * Process a file.
100
     *
101
     * @throws InvalidArgumentException|ReflectionException|AnnotationException
102
     */
103 25
    private static function processFile(SplFileInfo $file, ContainerBuilder $container, array $configs, bool $preProcess): array
104
    {
105 25
        self::$doctrineMapping = $configs['doctrine']['types_mapping'];
106 25
        $container->addResource(new FileResource($file->getRealPath()));
107
108
        try {
109 25
            $className = $file->getBasename('.php');
110 25
            if (preg_match('#namespace (.+);#', file_get_contents($file->getRealPath()), $matches)) {
111 25
                $className = trim($matches[1]).'\\'.$className;
112
            }
113
114 25
            $gqlTypes = [];
115 25
            $graphClass = self::getGraphClass($className);
116
117 25
            foreach ($graphClass->getAnnotations() as $classAnnotation) {
118 25
                $gqlTypes = self::classAnnotationsToGQLConfiguration(
119 25
                    $graphClass,
120
                    $classAnnotation,
121
                    $configs,
122
                    $gqlTypes,
123
                    $preProcess
124
                );
125
            }
126
127 25
            return $preProcess ? self::$classesMap : $gqlTypes;
128 10
        } catch (\InvalidArgumentException $e) {
129 10
            throw new InvalidArgumentException(sprintf('Failed to parse GraphQL annotations from file "%s".', $file), $e->getCode(), $e);
130
        }
131
    }
132
133 25
    private static function classAnnotationsToGQLConfiguration(
134
        GraphClass $graphClass,
135
        object $classAnnotation,
136
        array $configs,
137
        array $gqlTypes,
138
        bool $preProcess
139
    ): array {
140 25
        $gqlConfiguration = $gqlType = $gqlName = null;
141
142
        switch (true) {
143 25
            case $classAnnotation instanceof GQL\Type:
144 25
                $gqlType = self::GQL_TYPE;
145 25
                $gqlName = $classAnnotation->name ?? $graphClass->getShortName();
146 25
                if (!$preProcess) {
147 25
                    $gqlConfiguration = self::typeAnnotationToGQLConfiguration($graphClass, $classAnnotation, $gqlName, $configs);
148
149 25
                    if ($classAnnotation instanceof GQL\Relay\Connection) {
150 24
                        if (!$graphClass->implementsInterface(ConnectionInterface::class)) {
151
                            throw new InvalidArgumentException(sprintf('The annotation @Connection on class "%s" can only be used on class implementing the ConnectionInterface.', $graphClass->getName()));
152
                        }
153
154 24
                        if (!(isset($classAnnotation->edge) xor isset($classAnnotation->node))) {
155
                            throw new InvalidArgumentException(sprintf('The annotation @Connection on class "%s" is invalid. You must define either the "edge" OR the "node" attribute, but not both.', $graphClass->getName()));
156
                        }
157
158 24
                        $edgeType = $classAnnotation->edge ?? false;
159 24
                        if (!$edgeType) {
160 24
                            $edgeType = $gqlName.'Edge';
161 24
                            $gqlTypes[$edgeType] = [
162 24
                                'type' => 'object',
163
                                'config' => [
164
                                    'builders' => [
165 24
                                        ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]],
166
                                    ],
167
                                ],
168
                            ];
169
                        }
170
171 24
                        if (!isset($gqlConfiguration['config']['builders'])) {
172 24
                            $gqlConfiguration['config']['builders'] = [];
173
                        }
174
175 24
                        array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-connection', 'builderConfig' => ['edgeType' => $edgeType]]);
176
                    }
177
                }
178 25
                break;
179
180 24
            case $classAnnotation instanceof GQL\Input:
181 24
                $gqlType = self::GQL_INPUT;
182 24
                $gqlName = $classAnnotation->name ?? self::suffixName($graphClass->getShortName(), 'Input');
183 24
                if (!$preProcess) {
184 24
                    $gqlConfiguration = self::inputAnnotationToGQLConfiguration($graphClass, $classAnnotation);
185
                }
186 24
                break;
187
188 24
            case $classAnnotation instanceof GQL\Scalar:
189 24
                $gqlType = self::GQL_SCALAR;
190 24
                if (!$preProcess) {
191 24
                    $gqlConfiguration = self::scalarAnnotationToGQLConfiguration($graphClass, $classAnnotation);
192
                }
193 24
                break;
194
195 24
            case $classAnnotation instanceof GQL\Enum:
196 24
                $gqlType = self::GQL_ENUM;
197 24
                if (!$preProcess) {
198 24
                    $gqlConfiguration = self::enumAnnotationToGQLConfiguration($graphClass, $classAnnotation);
199
                }
200 24
                break;
201
202 24
            case $classAnnotation instanceof GQL\Union:
203 24
                $gqlType = self::GQL_UNION;
204 24
                if (!$preProcess) {
205 24
                    $gqlConfiguration = self::unionAnnotationToGQLConfiguration($graphClass, $classAnnotation);
206
                }
207 24
                break;
208
209 24
            case $classAnnotation instanceof GQL\TypeInterface:
210 24
                $gqlType = self::GQL_INTERFACE;
211 24
                if (!$preProcess) {
212 24
                    $gqlConfiguration = self::typeInterfaceAnnotationToGQLConfiguration($graphClass, $classAnnotation);
213
                }
214 24
                break;
215
216 24
            case $classAnnotation instanceof GQL\Provider:
217 24
                if ($preProcess) {
218 24
                    self::$providers[] = ['metadata' => $graphClass, 'annotation' => $classAnnotation];
219
                }
220
221 24
                return [];
222
        }
223
224 25
        if (null !== $gqlType) {
225 25
            if (!$gqlName) {
226 24
                $gqlName = isset($classAnnotation->name) ? $classAnnotation->name : $graphClass->getShortName();
227
            }
228
229 25
            if ($preProcess) {
230 25
                if (isset(self::$classesMap[$gqlName])) {
231 1
                    throw new InvalidArgumentException(sprintf('The GraphQL type "%s" has already been registered in class "%s"', $gqlName, self::$classesMap[$gqlName]['class']));
232
                }
233 25
                self::$classesMap[$gqlName] = ['type' => $gqlType, 'class' => $graphClass->getName()];
234
            } else {
235 25
                $gqlTypes = [$gqlName => $gqlConfiguration] + $gqlTypes;
236
            }
237
        }
238
239 25
        return $gqlTypes;
240
    }
241
242
    /**
243
     * @throws ReflectionException
244
     */
245 25
    private static function getGraphClass(string $className): GraphClass
246
    {
247 25
        self::$graphClassCache[$className] ??= new GraphClass($className);
248
249 25
        return self::$graphClassCache[$className];
250
    }
251
252 25
    private static function typeAnnotationToGQLConfiguration(
253
        GraphClass $graphClass,
254
        GQL\Type $classAnnotation,
255
        string $gqlName,
256
        array $configs
257
    ): array {
258 25
        $isMutation = $isDefault = $isRoot = false;
259 25
        if (isset($configs['definitions']['schema'])) {
260 24
            $defaultSchemaName = isset($configs['definitions']['schema']['default']) ? 'default' : array_key_first($configs['definitions']['schema']);
261 24
            foreach ($configs['definitions']['schema'] as $schemaName => $schema) {
262 24
                $schemaQuery = $schema['query'] ?? null;
263 24
                $schemaMutation = $schema['mutation'] ?? null;
264
265 24
                if ($gqlName === $schemaQuery) {
266 24
                    $isRoot = true;
267 24
                    if ($defaultSchemaName === $schemaName) {
268 24
                        $isDefault = true;
269
                    }
270 24
                } elseif ($gqlName === $schemaMutation) {
271 24
                    $isMutation = true;
272 24
                    $isRoot = true;
273 24
                    if ($defaultSchemaName === $schemaName) {
274 24
                        $isDefault = true;
275
                    }
276
                }
277
            }
278
        }
279
280 25
        $currentValue = $isRoot ? sprintf("service('%s')", self::formatNamespaceForExpression($graphClass->getName())) : 'value';
281
282 25
        $gqlConfiguration = self::graphQLTypeConfigFromAnnotation($graphClass, $classAnnotation, $currentValue);
283
284 25
        $providerFields = self::getGraphQLFieldsFromProviders($graphClass, $isMutation ? GQL\Mutation::class : GQL\Query::class, $gqlName, $isDefault);
285 25
        $gqlConfiguration['config']['fields'] = array_merge($gqlConfiguration['config']['fields'], $providerFields);
286
287 25
        if ($classAnnotation instanceof GQL\Relay\Edge) {
288 24
            if (!$graphClass->implementsInterface(EdgeInterface::class)) {
289
                throw new InvalidArgumentException(sprintf('The annotation @Edge on class "%s" can only be used on class implementing the EdgeInterface.', $graphClass->getName()));
290
            }
291 24
            if (!isset($gqlConfiguration['config']['builders'])) {
292 24
                $gqlConfiguration['config']['builders'] = [];
293
            }
294 24
            array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]]);
295
        }
296
297 25
        return $gqlConfiguration;
298
    }
299
300 25
    private static function graphQLTypeConfigFromAnnotation(GraphClass $graphClass, GQL\Type $typeAnnotation, string $currentValue): array
301
    {
302 25
        $typeConfiguration = [];
303 25
        $fieldsFromProperties = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended(), GQL\Field::class, $currentValue);
304 25
        $fieldsFromMethods = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getMethods(), GQL\Field::class, $currentValue);
305
306 25
        $typeConfiguration['fields'] = array_merge($fieldsFromProperties, $fieldsFromMethods);
307 25
        $typeConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $typeConfiguration;
308
309 25
        if (isset($typeAnnotation->interfaces)) {
310 24
            $typeConfiguration['interfaces'] = $typeAnnotation->interfaces;
311
        } else {
312 25
            $interfaces = array_keys(self::searchClassesMapBy(function ($gqlType, $configuration) use ($graphClass) {
313 24
                ['class' => $interfaceClassName] = $configuration;
314
315 24
                $interfaceMetadata = self::getGraphClass($interfaceClassName);
316 24
                if ($interfaceMetadata->isInterface() && $graphClass->implementsInterface($interfaceMetadata->getName())) {
317 24
                    return true;
318
                }
319
320 24
                return $graphClass->isSubclassOf($interfaceClassName);
321 25
            }, self::GQL_INTERFACE));
322
323 25
            sort($interfaces);
324 25
            $typeConfiguration['interfaces'] = $interfaces;
325
        }
326
327 25
        if (isset($typeAnnotation->resolveField)) {
328 24
            $typeConfiguration['resolveField'] = self::formatExpression($typeAnnotation->resolveField);
329
        }
330
331 25
        if (isset($typeAnnotation->builders) && !empty($typeAnnotation->builders)) {
332 24
            $typeConfiguration['builders'] = array_map(function ($fieldsBuilderAnnotation) {
333 24
                return ['builder' => $fieldsBuilderAnnotation->builder, 'builderConfig' => $fieldsBuilderAnnotation->builderConfig];
334 24
            }, $typeAnnotation->builders);
335
        }
336
337 25
        if (isset($typeAnnotation->isTypeOf)) {
338 24
            $typeConfiguration['isTypeOf'] = $typeAnnotation->isTypeOf;
339
        }
340
341 25
        $publicAnnotation = self::getFirstAnnotationMatching($graphClass->getAnnotations(), GQL\IsPublic::class);
342 25
        if (null !== $publicAnnotation) {
343 24
            $typeConfiguration['fieldsDefaultPublic'] = self::formatExpression($publicAnnotation->value);
344
        }
345
346 25
        $accessAnnotation = self::getFirstAnnotationMatching($graphClass->getAnnotations(), GQL\Access::class);
347 25
        if (null !== $accessAnnotation) {
348 24
            $typeConfiguration['fieldsDefaultAccess'] = self::formatExpression($accessAnnotation->value);
349
        }
350
351 25
        return ['type' => $typeAnnotation->isRelay ? 'relay-mutation-payload' : 'object', 'config' => $typeConfiguration];
352
    }
353
354
    /**
355
     * Create a GraphQL Interface type configuration from annotations on properties.
356
     */
357 24
    private static function typeInterfaceAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\TypeInterface $interfaceAnnotation): array
358
    {
359 24
        $interfaceConfiguration = [];
360
361 24
        $fieldsFromProperties = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended());
362 24
        $fieldsFromMethods = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getMethods());
363
364 24
        $interfaceConfiguration['fields'] = array_merge($fieldsFromProperties, $fieldsFromMethods);
365 24
        $interfaceConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $interfaceConfiguration;
366
367 24
        $interfaceConfiguration['resolveType'] = self::formatExpression($interfaceAnnotation->resolveType);
368
369 24
        return ['type' => 'interface', 'config' => $interfaceConfiguration];
370
    }
371
372
    /**
373
     * Create a GraphQL Input type configuration from annotations on properties.
374
     */
375 24
    private static function inputAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Input $inputAnnotation): array
376
    {
377 24
        $inputConfiguration = array_merge([
378 24
            'fields' => self::getGraphQLInputFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended()),
379 24
        ], self::getDescriptionConfiguration($graphClass->getAnnotations()));
380
381 24
        return ['type' => $inputAnnotation->isRelay ? 'relay-mutation-input' : 'input-object', 'config' => $inputConfiguration];
382
    }
383
384
    /**
385
     * Get a GraphQL scalar configuration from given scalar annotation.
386
     */
387 24
    private static function scalarAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Scalar $scalarAnnotation): array
388
    {
389 24
        $scalarConfiguration = [];
390
391 24
        if (isset($scalarAnnotation->scalarType)) {
392 24
            $scalarConfiguration['scalarType'] = self::formatExpression($scalarAnnotation->scalarType);
393
        } else {
394
            $scalarConfiguration = [
395 24
                'serialize' => [$graphClass->getName(), 'serialize'],
396 24
                'parseValue' => [$graphClass->getName(), 'parseValue'],
397 24
                'parseLiteral' => [$graphClass->getName(), 'parseLiteral'],
398
            ];
399
        }
400
401 24
        $scalarConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $scalarConfiguration;
402
403 24
        return ['type' => 'custom-scalar', 'config' => $scalarConfiguration];
404
    }
405
406
    /**
407
     * Get a GraphQL Enum configuration from given enum annotation.
408
     */
409 24
    private static function enumAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Enum $enumAnnotation): array
410
    {
411 24
        $enumValues = $enumAnnotation->values ? $enumAnnotation->values : [];
412
413 24
        $values = [];
414
415 24
        foreach ($graphClass->getConstants() as $name => $value) {
416 24
            $valueAnnotation = current(array_filter($enumValues, fn ($enumValueAnnotation) => $enumValueAnnotation->name == $name));
417 24
            $valueConfig = [];
418 24
            $valueConfig['value'] = $value;
419
420 24
            if ($valueAnnotation && isset($valueAnnotation->description)) {
421 24
                $valueConfig['description'] = $valueAnnotation->description;
422
            }
423
424 24
            if ($valueAnnotation && isset($valueAnnotation->deprecationReason)) {
425 24
                $valueConfig['deprecationReason'] = $valueAnnotation->deprecationReason;
426
            }
427
428 24
            $values[$name] = $valueConfig;
429
        }
430
431 24
        $enumConfiguration = ['values' => $values];
432 24
        $enumConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $enumConfiguration;
433
434 24
        return ['type' => 'enum', 'config' => $enumConfiguration];
435
    }
436
437
    /**
438
     * Get a GraphQL Union configuration from given union annotation.
439
     */
440 24
    private static function unionAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Union $unionAnnotation): array
441
    {
442 24
        $unionConfiguration = [];
443 24
        if (isset($unionAnnotation->types)) {
444 24
            $unionConfiguration['types'] = $unionAnnotation->types;
445
        } else {
446 24
            $types = array_keys(self::searchClassesMapBy(function ($gqlType, $configuration) use ($graphClass) {
447 24
                $typeClassName = $configuration['class'];
448 24
                $typeMetadata = self::getGraphClass($typeClassName);
449
450 24
                if ($graphClass->isInterface() && $typeMetadata->implementsInterface($graphClass->getName())) {
451 24
                    return true;
452
                }
453
454 24
                return $typeMetadata->isSubclassOf($graphClass->getName());
455 24
            }, self::GQL_TYPE));
456 24
            sort($types);
457 24
            $unionConfiguration['types'] = $types;
458
        }
459
460 24
        $unionConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $unionConfiguration;
461
462 24
        if (isset($unionAnnotation->resolveType)) {
463 24
            $unionConfiguration['resolveType'] = self::formatExpression($unionAnnotation->resolveType);
464
        } else {
465 24
            if ($graphClass->hasMethod('resolveType')) {
466 24
                $method = $graphClass->getMethod('resolveType');
467 24
                if ($method->isStatic() && $method->isPublic()) {
468 24
                    $unionConfiguration['resolveType'] = self::formatExpression(sprintf("@=call('%s::%s', [service('overblog_graphql.type_resolver'), value], true)", self::formatNamespaceForExpression($graphClass->getName()), 'resolveType'));
469
                } else {
470 24
                    throw new InvalidArgumentException(sprintf('The "resolveType()" method on class must be static and public. Or you must define a "resolveType" attribute on the @Union annotation.'));
471
                }
472
            } else {
473 1
                throw new InvalidArgumentException(sprintf('The annotation @Union has no "resolveType" attribute and the related class has no "resolveType()" public static method. You need to define of them.'));
474
            }
475
        }
476
477 24
        return ['type' => 'union', 'config' => $unionConfiguration];
478
    }
479
480
    /**
481
     * @phpstan-param ReflectionMethod|ReflectionProperty $reflector
482
     * @phpstan-param class-string<GQL\Field> $fieldAnnotationName
483
     *
484
     * @throws AnnotationException
485
     */
486 24
    private static function getTypeFieldConfigurationFromReflector(GraphClass $graphClass, Reflector $reflector, string $fieldAnnotationName, string $currentValue = 'value'): array
487
    {
488 24
        $annotations = $graphClass->getAnnotations($reflector);
489
490 24
        $fieldAnnotation = self::getFirstAnnotationMatching($annotations, $fieldAnnotationName);
491 24
        $accessAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Access::class);
492 24
        $publicAnnotation = self::getFirstAnnotationMatching($annotations, GQL\IsPublic::class);
493
494 24
        if (null === $fieldAnnotation) {
495 24
            if (null !== $accessAnnotation || null !== $publicAnnotation) {
496 1
                throw new InvalidArgumentException(sprintf('The annotations "@Access" and/or "@Visible" defined on "%s" are only usable in addition of annotation "@Field"', $reflector->getName()));
497
            }
498
499 24
            return [];
500
        }
501
502 24
        if ($reflector instanceof ReflectionMethod && !$reflector->isPublic()) {
503 1
            throw new InvalidArgumentException(sprintf('The Annotation "@Field" can only be applied to public method. The method "%s" is not public.', $reflector->getName()));
504
        }
505
506 24
        $fieldName = $reflector->getName();
507 24
        $fieldConfiguration = [];
508
509 24
        if (isset($fieldAnnotation->type)) {
510 24
            $fieldConfiguration['type'] = $fieldAnnotation->type;
511
        }
512
513 24
        $fieldConfiguration = self::getDescriptionConfiguration($annotations, true) + $fieldConfiguration;
514
515 24
        $args = [];
516
517 24
        foreach ($fieldAnnotation->args as $arg) {
518 24
            $args[$arg->name] = ['type' => $arg->type];
519
520 24
            if (isset($arg->description)) {
521 24
                $args[$arg->name]['description'] = $arg->description;
522
            }
523
524 24
            if (isset($arg->default)) {
525 24
                $args[$arg->name]['defaultValue'] = $arg->default;
526
            }
527
        }
528
529 24
        if (empty($fieldAnnotation->args) && $reflector instanceof ReflectionMethod) {
530 24
            $args = self::guessArgs($reflector);
531
        }
532
533 24
        if (!empty($args)) {
534 24
            $fieldConfiguration['args'] = $args;
535
        }
536
537 24
        $fieldName = $fieldAnnotation->name ?? $fieldName;
538
539 24
        if (isset($fieldAnnotation->resolve)) {
540 24
            $fieldConfiguration['resolve'] = self::formatExpression($fieldAnnotation->resolve);
541
        } else {
542 24
            if ($reflector instanceof ReflectionMethod) {
543 24
                $fieldConfiguration['resolve'] = self::formatExpression(sprintf('call(%s.%s, %s)', $currentValue, $reflector->getName(), self::formatArgsForExpression($args)));
544
            } else {
545 24
                if ($fieldName !== $reflector->getName() || 'value' !== $currentValue) {
546
                    $fieldConfiguration['resolve'] = self::formatExpression(sprintf('%s.%s', $currentValue, $reflector->getName()));
547
                }
548
            }
549
        }
550
551 24
        if ($fieldAnnotation->argsBuilder) {
552 24
            if (is_string($fieldAnnotation->argsBuilder)) {
553
                $fieldConfiguration['argsBuilder'] = $fieldAnnotation->argsBuilder;
554 24
            } elseif (is_array($fieldAnnotation->argsBuilder)) {
555 24
                list($builder, $builderConfig) = $fieldAnnotation->argsBuilder;
556 24
                $fieldConfiguration['argsBuilder'] = ['builder' => $builder, 'config' => $builderConfig];
557
            } else {
558
                throw new InvalidArgumentException(sprintf('The attribute "argsBuilder" on GraphQL annotation "@%s" defined on "%s" must be a string or an array where first index is the builder name and the second is the config.', $fieldAnnotationName, $reflector->getName()));
559
            }
560
        }
561
562 24
        if ($fieldAnnotation->fieldBuilder) {
563 24
            if (is_string($fieldAnnotation->fieldBuilder)) {
564
                $fieldConfiguration['builder'] = $fieldAnnotation->fieldBuilder;
565 24
            } elseif (is_array($fieldAnnotation->fieldBuilder)) {
566 24
                list($builder, $builderConfig) = $fieldAnnotation->fieldBuilder;
567 24
                $fieldConfiguration['builder'] = $builder;
568 24
                $fieldConfiguration['builderConfig'] = $builderConfig ?: [];
569
            } else {
570 24
                throw new InvalidArgumentException(sprintf('The attribute "fieldBuilder" on GraphQL annotation "@%s" defined on "%s" must be a string or an array where first index is the builder name and the second is the config.', $fieldAnnotationName, $reflector->getName()));
571
            }
572
        } else {
573 24
            if (!isset($fieldAnnotation->type)) {
574 24
                if ($reflector instanceof ReflectionMethod) {
575
                    /** @var ReflectionMethod $reflector */
576 24
                    if ($reflector->hasReturnType()) {
577
                        try {
578
                            // @phpstan-ignore-next-line
579 24
                            $fieldConfiguration['type'] = self::resolveGraphQLTypeFromReflectionType($reflector->getReturnType(), self::VALID_OUTPUT_TYPES);
580
                        } catch (Exception $e) {
581 24
                            throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on method "%s" and cannot be auto-guessed from type hint "%s"', $fieldAnnotationName, $reflector->getName(), (string) $reflector->getReturnType()));
582
                        }
583
                    } else {
584 24
                        throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on method "%s" and cannot be auto-guessed as there is not return type hint.', $fieldAnnotationName, $reflector->getName()));
585
                    }
586
                } else {
587
                    try {
588 24
                        $fieldConfiguration['type'] = self::guessType($graphClass, $annotations);
589 2
                    } catch (Exception $e) {
590 2
                        throw new InvalidArgumentException(sprintf('The attribute "type" on "@%s" defined on "%s" is required and cannot be auto-guessed : %s.', $fieldAnnotationName, $reflector->getName(), $e->getMessage()));
591
                    }
592
                }
593
            }
594
        }
595
596 24
        if ($accessAnnotation) {
597 24
            $fieldConfiguration['access'] = self::formatExpression($accessAnnotation->value);
598
        }
599
600 24
        if ($publicAnnotation) {
601 24
            $fieldConfiguration['public'] = self::formatExpression($publicAnnotation->value);
602
        }
603
604 24
        if ($fieldAnnotation->complexity) {
605 24
            $fieldConfiguration['complexity'] = self::formatExpression($fieldAnnotation->complexity);
606
        }
607
608 24
        return [$fieldName => $fieldConfiguration];
609
    }
610
611
    /**
612
     * Create GraphQL input fields configuration based on annotations.
613
     *
614
     * @param ReflectionProperty[] $reflectors
615
     *
616
     * @throws AnnotationException
617
     */
618 24
    private static function getGraphQLInputFieldsFromAnnotations(GraphClass $graphClass, array $reflectors): array
619
    {
620 24
        $fields = [];
621
622 24
        foreach ($reflectors as $reflector) {
623 24
            $annotations = $graphClass->getAnnotations($reflector);
624
625
            /** @var GQL\Field $fieldAnnotation */
626 24
            $fieldAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Field::class);
627
628
            // Ignore field with resolver when the type is an Input
629 24
            if (isset($fieldAnnotation->resolve)) {
630 24
                continue;
631
            }
632
633 24
            $fieldName = $reflector->getName();
634 24
            $fieldType = $fieldAnnotation->type;
635 24
            $fieldConfiguration = [];
636 24
            if ($fieldType) {
637
                // Resolve a PHP class from a GraphQL type
638 24
                $resolvedType = self::$classesMap[$fieldType] ?? null;
639
                // We found a type but it is not allowed
640 24
                if (null !== $resolvedType && !in_array($resolvedType['type'], self::VALID_INPUT_TYPES)) {
641
                    throw new InvalidArgumentException(sprintf('The type "%s" on "%s" is a "%s" not valid on an Input @Field. Only Input, Scalar and Enum are allowed.', $fieldType, $reflector->getName(), $resolvedType['type']));
642
                }
643
644 24
                $fieldConfiguration['type'] = $fieldType;
645
            }
646
647 24
            $fieldConfiguration = array_merge(self::getDescriptionConfiguration($annotations, true), $fieldConfiguration);
648 24
            $fields[$fieldName] = $fieldConfiguration;
649
        }
650
651 24
        return $fields;
652
    }
653
654
    /**
655
     * Create GraphQL type fields configuration based on annotations.
656
     *
657
     * @phpstan-param class-string<GQL\Field> $fieldAnnotationName
658
     *
659
     * @param ReflectionProperty[]|ReflectionMethod[] $reflectors
660
     *
661
     * @throws AnnotationException
662
     */
663 25
    private static function getGraphQLTypeFieldsFromAnnotations(GraphClass $graphClass, array $reflectors, string $fieldAnnotationName = GQL\Field::class, string $currentValue = 'value'): array
664
    {
665 25
        $fields = [];
666
667 25
        foreach ($reflectors as $reflector) {
668 24
            $fields = array_merge($fields, self::getTypeFieldConfigurationFromReflector($graphClass, $reflector, $fieldAnnotationName, $currentValue));
669
        }
670
671 25
        return $fields;
672
    }
673
674
    /**
675
     * @phpstan-param class-string<GQL\Query|GQL\Mutation> $expectedAnnotation
676
     *
677
     * Return fields config from Provider methods.
678
     * Loop through configured provider and extract fields targeting the targetType.
679
     */
680 25
    private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, string $expectedAnnotation, string $targetType, bool $isDefaultTarget = false): array
681
    {
682 25
        $fields = [];
683 25
        foreach (self::$providers as ['metadata' => $providerMetadata, 'annotation' => $providerAnnotation]) {
684 24
            $defaultAccessAnnotation = self::getFirstAnnotationMatching($providerMetadata->getAnnotations(), GQL\Access::class);
685 24
            $defaultIsPublicAnnotation = self::getFirstAnnotationMatching($providerMetadata->getAnnotations(), GQL\IsPublic::class);
686
687 24
            $defaultAccess = $defaultAccessAnnotation ? self::formatExpression($defaultAccessAnnotation->value) : false;
688 24
            $defaultIsPublic = $defaultIsPublicAnnotation ? self::formatExpression($defaultIsPublicAnnotation->value) : false;
689
690 24
            $methods = [];
691
            // First found the methods matching the targeted type
692 24
            foreach ($providerMetadata->getMethods() as $method) {
693 24
                $annotations = $providerMetadata->getAnnotations($method);
694
695 24
                $annotation = self::getFirstAnnotationMatching($annotations, [GQL\Mutation::class, GQL\Query::class]);
696 24
                if (null === $annotation) {
697
                    continue;
698
                }
699
700 24
                $annotationTargets = $annotation->targetType ?? null;
701
702 24
                if (null === $annotationTargets) {
703 24
                    if ($isDefaultTarget) {
704 24
                        $annotationTargets = [$targetType];
705 24
                        if (!$annotation instanceof $expectedAnnotation) {
706 24
                            continue;
707
                        }
708
                    } else {
709 24
                        continue;
710
                    }
711
                }
712
713 24
                if (!in_array($targetType, $annotationTargets)) {
714 24
                    continue;
715
                }
716
717 24
                if (!$annotation instanceof $expectedAnnotation) {
718 2
                    if (GQL\Mutation::class == $expectedAnnotation) {
719 1
                        $message = sprintf('The provider "%s" try to add a query field on type "%s" (through @Query on method "%s") but "%s" is a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType);
720
                    } else {
721 1
                        $message = sprintf('The provider "%s" try to add a mutation on type "%s" (through @Mutation on method "%s") but "%s" is not a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType);
722
                    }
723
724 2
                    throw new InvalidArgumentException($message);
725
                }
726 24
                $methods[$method->getName()] = $method;
727
            }
728
729 24
            $currentValue = sprintf("service('%s')", self::formatNamespaceForExpression($providerMetadata->getName()));
730 24
            $providerFields = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $methods, $expectedAnnotation, $currentValue);
731 24
            foreach ($providerFields as $fieldName => $fieldConfig) {
732 24
                if ($providerAnnotation->prefix) {
733 24
                    $fieldName = sprintf('%s%s', $providerAnnotation->prefix, $fieldName);
734
                }
735
736 24
                if ($defaultAccess && !isset($fieldConfig['access'])) {
737 24
                    $fieldConfig['access'] = $defaultAccess;
738
                }
739
740 24
                if ($defaultIsPublic && !isset($fieldConfig['public'])) {
741 24
                    $fieldConfig['public'] = $defaultIsPublic;
742
                }
743
744 24
                $fields[$fieldName] = $fieldConfig;
745
            }
746
        }
747
748 25
        return $fields;
749
    }
750
751
    /**
752
     * Get the config for description & deprecation reason.
753
     */
754 25
    private static function getDescriptionConfiguration(array $annotations, bool $withDeprecation = false): array
755
    {
756 25
        $config = [];
757 25
        $descriptionAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Description::class);
758 25
        if (null !== $descriptionAnnotation) {
759 24
            $config['description'] = $descriptionAnnotation->value;
760
        }
761
762 25
        if ($withDeprecation) {
763 24
            $deprecatedAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Deprecated::class);
764 24
            if (null !== $deprecatedAnnotation) {
765 24
                $config['deprecationReason'] = $deprecatedAnnotation->value;
766
            }
767
        }
768
769 25
        return $config;
770
    }
771
772
    /**
773
     * Format an array of args to a list of arguments in an expression.
774
     */
775 24
    private static function formatArgsForExpression(array $args): string
776
    {
777 24
        $mapping = [];
778 24
        foreach ($args as $name => $config) {
779 24
            $mapping[] = sprintf('%s: "%s"', $name, $config['type']);
780
        }
781
782 24
        return sprintf('arguments({%s}, args)', implode(', ', $mapping));
783
    }
784
785
    /**
786
     * Format a namespace to be used in an expression (double escape).
787
     */
788 24
    private static function formatNamespaceForExpression(string $namespace): string
789
    {
790 24
        return str_replace('\\', '\\\\', $namespace);
791
    }
792
793
    /**
794
     * Get the first annotation matching given class.
795
     *
796
     * @phpstan-template T of object
797
     * @phpstan-param class-string<T>|class-string<T>[] $annotationClass
798
     * @phpstan-return T|null
799
     *
800
     * @param string|array $annotationClass
801
     *
802
     * @return object|null
803
     */
804 25
    private static function getFirstAnnotationMatching(array $annotations, $annotationClass)
805
    {
806 25
        if (is_string($annotationClass)) {
807 25
            $annotationClass = [$annotationClass];
808
        }
809
810 25
        foreach ($annotations as $annotation) {
811 25
            foreach ($annotationClass as $class) {
812 25
                if ($annotation instanceof $class) {
813 24
                    return $annotation;
814
                }
815
            }
816
        }
817
818 25
        return null;
819
    }
820
821
    /**
822
     * Format an expression (ie. add "@=" if not set).
823
     */
824 24
    private static function formatExpression(string $expression): string
825
    {
826 24
        return '@=' === substr($expression, 0, 2) ? $expression : sprintf('@=%s', $expression);
827
    }
828
829
    /**
830
     * Suffix a name if it is not already.
831
     */
832 24
    private static function suffixName(string $name, string $suffix): string
833
    {
834 24
        return substr($name, -strlen($suffix)) === $suffix ? $name : sprintf('%s%s', $name, $suffix);
835
    }
836
837
    /**
838
     * Try to guess a field type base on his annotations.
839
     *
840
     * @throws RuntimeException
841
     */
842 24
    private static function guessType(GraphClass $graphClass, array $annotations): string
843
    {
844 24
        $columnAnnotation = self::getFirstAnnotationMatching($annotations, Column::class);
845 24
        if (null !== $columnAnnotation) {
846 24
            $type = self::resolveTypeFromDoctrineType($columnAnnotation->type);
847 24
            $nullable = $columnAnnotation->nullable;
848 24
            if ($type) {
849 24
                return $nullable ? $type : sprintf('%s!', $type);
850
            } else {
851 1
                throw new RuntimeException(sprintf('Unable to auto-guess GraphQL type from Doctrine type "%s"', $columnAnnotation->type));
852
            }
853
        }
854
855
        $associationAnnotations = [
856 24
            OneToMany::class => true,
857
            OneToOne::class => false,
858
            ManyToMany::class => true,
859
            ManyToOne::class => false,
860
        ];
861
862 24
        $associationAnnotation = self::getFirstAnnotationMatching($annotations, array_keys($associationAnnotations));
863 24
        if (null !== $associationAnnotation) {
864 24
            $target = self::fullyQualifiedClassName($associationAnnotation->targetEntity, $graphClass->getNamespaceName());
865 24
            $type = self::resolveTypeFromClass($target, ['type']);
866
867 24
            if ($type) {
868 24
                $isMultiple = $associationAnnotations[get_class($associationAnnotation)];
869 24
                if ($isMultiple) {
870 24
                    return sprintf('[%s]!', $type);
871
                } else {
872 24
                    $isNullable = false;
873 24
                    $joinColumn = self::getFirstAnnotationMatching($annotations, JoinColumn::class);
874 24
                    if (null !== $joinColumn) {
875 24
                        $isNullable = $joinColumn->nullable;
876
                    }
877
878 24
                    return sprintf('%s%s', $type, $isNullable ? '' : '!');
879
                }
880
            } else {
881 1
                throw new RuntimeException(sprintf('Unable to auto-guess GraphQL type from Doctrine target class "%s" (check if the target class is a GraphQL type itself (with a @GQL\Type annotation).', $target));
882
            }
883
        }
884
885
        throw new InvalidArgumentException(sprintf('No Doctrine ORM annotation found.'));
886
    }
887
888
    /**
889
     * Resolve a FQN from classname and namespace.
890
     *
891
     * @internal
892
     */
893 24
    public static function fullyQualifiedClassName(string $className, string $namespace): string
894
    {
895 24
        if (false === strpos($className, '\\') && $namespace) {
896 24
            return $namespace.'\\'.$className;
897
        }
898
899 1
        return $className;
900
    }
901
902
    /**
903
     * Resolve a GraphQLType from a doctrine type.
904
     */
905 24
    private static function resolveTypeFromDoctrineType(string $doctrineType): ?string
906
    {
907 24
        if (isset(self::$doctrineMapping[$doctrineType])) {
908 24
            return self::$doctrineMapping[$doctrineType];
909
        }
910
911
        switch ($doctrineType) {
912 24
            case 'integer':
913 24
            case 'smallint':
914 24
            case 'bigint':
915 24
                return 'Int';
916 24
            case 'string':
917 1
            case 'text':
918 24
                return 'String';
919 1
            case 'bool':
920 1
            case 'boolean':
921
                return 'Boolean';
922 1
            case 'float':
923 1
            case 'decimal':
924
                return 'Float';
925
            default:
926 1
                return null;
927
        }
928
    }
929
930
    /**
931
     * Transform a method arguments from reflection to a list of GraphQL argument.
932
     */
933 24
    private static function guessArgs(ReflectionMethod $method): array
934
    {
935 24
        $arguments = [];
936 24
        foreach ($method->getParameters() as $index => $parameter) {
937 24
            if (!$parameter->hasType()) {
938 1
                throw new InvalidArgumentException(sprintf('Argument n°%s "$%s" on method "%s" cannot be auto-guessed as there is not type hint.', $index + 1, $parameter->getName(), $method->getName()));
939
            }
940
941
            try {
942
                // @phpstan-ignore-next-line
943 24
                $gqlType = self::resolveGraphQLTypeFromReflectionType($parameter->getType(), self::VALID_INPUT_TYPES, $parameter->isDefaultValueAvailable());
944
            } catch (Exception $e) {
945
                throw new InvalidArgumentException(sprintf('Argument n°%s "$%s" on method "%s" cannot be auto-guessed : %s".', $index + 1, $parameter->getName(), $method->getName(), $e->getMessage()));
946
            }
947
948 24
            $argumentConfig = [];
949 24
            if ($parameter->isDefaultValueAvailable()) {
950 24
                $argumentConfig['defaultValue'] = $parameter->getDefaultValue();
951
            }
952
953 24
            $argumentConfig['type'] = $gqlType;
954
955 24
            $arguments[$parameter->getName()] = $argumentConfig;
956
        }
957
958 24
        return $arguments;
959
    }
960
961 24
    private static function resolveGraphQLTypeFromReflectionType(ReflectionNamedType $type, array $filterGraphQLTypes = [], bool $isOptional = false): string
962
    {
963 24
        $sType = $type->getName();
964 24
        if ($type->isBuiltin()) {
965 24
            $gqlType = self::resolveTypeFromPhpType($sType);
966 24
            if (null === $gqlType) {
967 24
                throw new RuntimeException(sprintf('No corresponding GraphQL type found for builtin type "%s"', $sType));
968
            }
969
        } else {
970 24
            $gqlType = self::resolveTypeFromClass($sType, $filterGraphQLTypes);
971 24
            if (null === $gqlType) {
972
                throw new RuntimeException(sprintf('No corresponding GraphQL %s found for class "%s"', $filterGraphQLTypes ? implode(',', $filterGraphQLTypes) : 'object', $sType));
973
            }
974
        }
975
976 24
        return sprintf('%s%s', $gqlType, ($type->allowsNull() || $isOptional) ? '' : '!');
977
    }
978
979
    /**
980
     * Resolve a GraphQL Type from a class name.
981
     */
982 24
    private static function resolveTypeFromClass(string $className, array $wantedTypes = []): ?string
983
    {
984 24
        foreach (self::$classesMap as $gqlType => $config) {
985 24
            if ($config['class'] === $className) {
986 24
                if (in_array($config['type'], $wantedTypes)) {
987 24
                    return $gqlType;
988
                }
989
            }
990
        }
991
992 1
        return null;
993
    }
994
995
    /**
996
     * Search the classes map for class by predicate.
997
     *
998
     * @return array
999
     */
1000 25
    private static function searchClassesMapBy(callable $predicate, string $type)
1001
    {
1002 25
        $classNames = [];
1003 25
        foreach (self::$classesMap as $gqlType => $config) {
1004 25
            if ($config['type'] !== $type) {
1005 25
                continue;
1006
            }
1007
1008 24
            if ($predicate($gqlType, $config)) {
1009 24
                $classNames[$gqlType] = $config;
1010
            }
1011
        }
1012
1013 25
        return $classNames;
1014
    }
1015
1016
    /**
1017
     * Convert a PHP Builtin type to a GraphQL type.
1018
     */
1019 24
    private static function resolveTypeFromPhpType(string $phpType): ?string
1020
    {
1021
        switch ($phpType) {
1022 24
            case 'boolean':
1023 24
            case 'bool':
1024 24
                return 'Boolean';
1025 24
            case 'integer':
1026 24
            case 'int':
1027 24
                return 'Int';
1028 24
            case 'float':
1029 24
            case 'double':
1030 24
                return 'Float';
1031 24
            case 'string':
1032 24
                return 'String';
1033
            default:
1034
                return null;
1035
        }
1036
    }
1037
}
1038