We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 200 |
| Total Lines | 1018 |
| Duplicated Lines | 0 % |
| Coverage | 95.86% |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 22 | class AnnotationParser implements PreParserInterface |
||
| 23 | { |
||
| 24 | private static $annotationReader = null; |
||
| 25 | private static $classesMap = []; |
||
| 26 | private static $providers = []; |
||
| 27 | private static $doctrineMapping = []; |
||
| 28 | private static $classAnnotationsCache = []; |
||
| 29 | |||
| 30 | private const GQL_SCALAR = 'scalar'; |
||
| 31 | private const GQL_ENUM = 'enum'; |
||
| 32 | private const GQL_TYPE = 'type'; |
||
| 33 | private const GQL_INPUT = 'input'; |
||
| 34 | private const GQL_UNION = 'union'; |
||
| 35 | private const GQL_INTERFACE = 'interface'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @see https://facebook.github.io/graphql/draft/#sec-Input-and-Output-Types |
||
| 39 | */ |
||
| 40 | private const VALID_INPUT_TYPES = [self::GQL_SCALAR, self::GQL_ENUM, self::GQL_INPUT]; |
||
| 41 | private const VALID_OUTPUT_TYPES = [self::GQL_SCALAR, self::GQL_TYPE, self::GQL_INTERFACE, self::GQL_UNION, self::GQL_ENUM]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | * |
||
| 46 | * @throws \ReflectionException |
||
| 47 | * @throws InvalidArgumentException |
||
| 48 | */ |
||
| 49 | 20 | public static function preParse(\SplFileInfo $file, ContainerBuilder $container, array $configs = []): void |
|
| 52 | 20 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | * |
||
| 57 | * @throws \ReflectionException |
||
| 58 | * @throws InvalidArgumentException |
||
| 59 | */ |
||
| 60 | 20 | public static function parse(\SplFileInfo $file, ContainerBuilder $container, array $configs = []): array |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @internal |
||
| 67 | */ |
||
| 68 | 54 | public static function reset(): void |
|
| 69 | { |
||
| 70 | 54 | self::$classesMap = []; |
|
| 71 | 54 | self::$providers = []; |
|
| 72 | 54 | self::$classAnnotationsCache = []; |
|
| 73 | 54 | self::$annotationReader = null; |
|
| 74 | 54 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Process a file. |
||
| 78 | * |
||
| 79 | * @param \SplFileInfo $file |
||
| 80 | * @param ContainerBuilder $container |
||
| 81 | * @param array $configs |
||
| 82 | * @param bool $preProcess |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | * |
||
| 86 | * @throws \ReflectionException |
||
| 87 | * @throws InvalidArgumentException |
||
| 88 | */ |
||
| 89 | 20 | private static function processFile(\SplFileInfo $file, ContainerBuilder $container, array $configs, bool $preProcess): array |
|
| 90 | { |
||
| 91 | 20 | self::$doctrineMapping = $configs['doctrine']['types_mapping']; |
|
| 92 | 20 | $container->addResource(new FileResource($file->getRealPath())); |
|
| 93 | |||
| 94 | try { |
||
| 95 | 20 | $className = $file->getBasename('.php'); |
|
| 96 | 20 | if (\preg_match('#namespace (.+);#', \file_get_contents($file->getRealPath()), $matches)) { |
|
| 97 | 20 | $className = \trim($matches[1]).'\\'.$className; |
|
| 98 | } |
||
| 99 | 20 | [$reflectionEntity, $classAnnotations, $properties, $methods] = self::extractClassAnnotations($className); |
|
| 100 | 20 | $gqlTypes = []; |
|
| 101 | |||
| 102 | 20 | foreach ($classAnnotations as $classAnnotation) { |
|
| 103 | 20 | $gqlTypes = self::classAnnotationsToGQLConfiguration( |
|
| 104 | 20 | $reflectionEntity, |
|
| 105 | 20 | $classAnnotation, |
|
| 106 | 20 | $configs, |
|
| 107 | 20 | $classAnnotations, |
|
| 108 | 20 | $properties, |
|
| 109 | 20 | $methods, |
|
| 110 | 20 | $gqlTypes, |
|
| 111 | 20 | $preProcess |
|
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | 20 | return $preProcess ? self::$classesMap : $gqlTypes; |
|
| 116 | 8 | } catch (\InvalidArgumentException $e) { |
|
| 117 | 8 | throw new InvalidArgumentException(\sprintf('Failed to parse GraphQL annotations from file "%s".', $file), $e->getCode(), $e); |
|
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param \ReflectionClass $reflectionEntity |
||
| 123 | * @param array $configs |
||
| 124 | * @param object $classAnnotation |
||
| 125 | * @param array $classAnnotations |
||
| 126 | * @param array $properties |
||
| 127 | * @param array $methods |
||
| 128 | * @param array $gqlTypes |
||
| 129 | * @param bool $preProcess |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | 20 | private static function classAnnotationsToGQLConfiguration( |
|
| 252 | } |
||
| 253 | |||
| 254 | 20 | private static function extractClassAnnotations(string $className): array |
|
| 255 | { |
||
| 256 | 20 | if (!isset(self::$classAnnotationsCache[$className])) { |
|
| 257 | 20 | $annotationReader = self::getAnnotationReader(); |
|
| 258 | 20 | $reflectionEntity = new \ReflectionClass($className); |
|
| 259 | 20 | $classAnnotations = $annotationReader->getClassAnnotations($reflectionEntity); |
|
| 260 | |||
| 261 | 20 | $properties = []; |
|
| 262 | 20 | $reflectionClass = new \ReflectionClass($className); |
|
| 263 | do { |
||
| 264 | 20 | foreach ($reflectionClass->getProperties() as $property) { |
|
| 265 | 19 | if (isset($properties[$property->getName()])) { |
|
| 266 | 19 | continue; |
|
| 267 | } |
||
| 268 | 19 | $properties[$property->getName()] = ['property' => $property, 'annotations' => $annotationReader->getPropertyAnnotations($property)]; |
|
| 269 | } |
||
| 270 | 20 | } while ($reflectionClass = $reflectionClass->getParentClass()); |
|
| 271 | |||
| 272 | 20 | $methods = []; |
|
| 273 | 20 | foreach ($reflectionEntity->getMethods() as $method) { |
|
| 274 | 19 | $methods[$method->getName()] = ['method' => $method, 'annotations' => $annotationReader->getMethodAnnotations($method)]; |
|
| 275 | } |
||
| 276 | |||
| 277 | 20 | self::$classAnnotationsCache[$className] = [$reflectionEntity, $classAnnotations, $properties, $methods]; |
|
| 278 | } |
||
| 279 | |||
| 280 | 20 | return self::$classAnnotationsCache[$className]; |
|
| 281 | } |
||
| 282 | |||
| 283 | 20 | private static function typeAnnotationToGQLConfiguration( |
|
| 284 | \ReflectionClass $reflectionEntity, |
||
| 285 | GQL\Type $classAnnotation, |
||
| 286 | string $gqlName, |
||
| 287 | array $classAnnotations, |
||
| 288 | array $properties, |
||
| 289 | array $methods, |
||
| 290 | array $configs |
||
| 291 | ): array { |
||
| 292 | 20 | $rootQueryType = $configs['definitions']['schema']['default']['query'] ?? null; |
|
| 293 | 20 | $rootMutationType = $configs['definitions']['schema']['default']['mutation'] ?? null; |
|
| 294 | 20 | $isRootQuery = ($rootQueryType && $gqlName === $rootQueryType); |
|
| 295 | 20 | $isRootMutation = ($rootMutationType && $gqlName === $rootMutationType); |
|
| 296 | 20 | $currentValue = ($isRootQuery || $isRootMutation) ? \sprintf("service('%s')", self::formatNamespaceForExpression($reflectionEntity->getName())) : 'value'; |
|
| 297 | |||
| 298 | 20 | $gqlConfiguration = self::graphQLTypeConfigFromAnnotation($classAnnotation, $classAnnotations, $properties, $methods, $reflectionEntity->getNamespaceName(), $currentValue); |
|
| 299 | 20 | $providerFields = self::getGraphQLFieldsFromProviders($reflectionEntity->getNamespaceName(), $isRootMutation ? 'Mutation' : 'Query', $gqlName, ($isRootQuery || $isRootMutation)); |
|
| 300 | 20 | $gqlConfiguration['config']['fields'] = $providerFields + $gqlConfiguration['config']['fields']; |
|
| 301 | |||
| 302 | 20 | if ($classAnnotation instanceof GQL\Relay\Edge) { |
|
| 303 | 19 | if (!$reflectionEntity->implementsInterface(EdgeInterface::class)) { |
|
| 304 | throw new InvalidArgumentException(\sprintf('The annotation @Edge on class "%s" can only be used on class implementing the EdgeInterface.', $reflectionEntity->getName())); |
||
| 305 | } |
||
| 306 | 19 | if (!isset($gqlConfiguration['config']['builders'])) { |
|
| 307 | 19 | $gqlConfiguration['config']['builders'] = []; |
|
| 308 | } |
||
| 309 | 19 | \array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]]); |
|
| 310 | } |
||
| 311 | |||
| 312 | 20 | return $gqlConfiguration; |
|
| 313 | } |
||
| 314 | |||
| 315 | 20 | private static function getAnnotationReader() |
|
| 316 | { |
||
| 317 | 20 | if (null === self::$annotationReader) { |
|
| 318 | 20 | if (!\class_exists('\\Doctrine\\Common\\Annotations\\AnnotationReader') || |
|
| 319 | 20 | !\class_exists('\\Doctrine\\Common\\Annotations\\AnnotationRegistry')) { |
|
| 320 | throw new \RuntimeException('In order to use graphql annotation, you need to require doctrine annotations'); |
||
| 321 | } |
||
| 322 | |||
| 323 | 20 | AnnotationRegistry::registerLoader('class_exists'); |
|
| 324 | 20 | self::$annotationReader = new AnnotationReader(); |
|
| 325 | } |
||
| 326 | |||
| 327 | 20 | return self::$annotationReader; |
|
| 328 | } |
||
| 329 | |||
| 330 | 20 | private static function graphQLTypeConfigFromAnnotation(GQL\Type $typeAnnotation, array $classAnnotations, array $properties, array $methods, string $namespace, string $currentValue): array |
|
| 331 | { |
||
| 332 | 20 | $typeConfiguration = []; |
|
| 333 | |||
| 334 | 20 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $properties, false, false, $currentValue); |
|
| 335 | 20 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $methods, false, true, $currentValue) + $fields; |
|
| 336 | |||
| 337 | 20 | $typeConfiguration['fields'] = $fields; |
|
| 338 | 20 | $typeConfiguration = self::getDescriptionConfiguration($classAnnotations) + $typeConfiguration; |
|
| 339 | |||
| 340 | 20 | if ($typeAnnotation->interfaces) { |
|
| 341 | 19 | $typeConfiguration['interfaces'] = $typeAnnotation->interfaces; |
|
| 342 | } |
||
| 343 | |||
| 344 | 20 | if ($typeAnnotation->resolveField) { |
|
| 345 | 19 | $typeConfiguration['resolveField'] = self::formatExpression($typeAnnotation->resolveField); |
|
| 346 | } |
||
| 347 | |||
| 348 | 20 | if ($typeAnnotation->builders && !empty($typeAnnotation->builders)) { |
|
| 349 | $typeConfiguration['builders'] = \array_map(function ($fieldsBuilderAnnotation) { |
||
| 350 | 19 | return ['builder' => $fieldsBuilderAnnotation->builder, 'builderConfig' => $fieldsBuilderAnnotation->builderConfig]; |
|
| 351 | 19 | }, $typeAnnotation->builders); |
|
| 352 | } |
||
| 353 | |||
| 354 | 20 | $publicAnnotation = self::getFirstAnnotationMatching($classAnnotations, GQL\IsPublic::class); |
|
| 355 | 20 | if ($publicAnnotation) { |
|
| 356 | 19 | $typeConfiguration['fieldsDefaultPublic'] = self::formatExpression($publicAnnotation->value); |
|
| 357 | } |
||
| 358 | |||
| 359 | 20 | $accessAnnotation = self::getFirstAnnotationMatching($classAnnotations, GQL\Access::class); |
|
| 360 | 20 | if ($accessAnnotation) { |
|
| 361 | 19 | $typeConfiguration['fieldsDefaultAccess'] = self::formatExpression($accessAnnotation->value); |
|
| 362 | } |
||
| 363 | |||
| 364 | 20 | return ['type' => $typeAnnotation->isRelay ? 'relay-mutation-payload' : 'object', 'config' => $typeConfiguration]; |
|
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Create a GraphQL Interface type configuration from annotations on properties. |
||
| 369 | * |
||
| 370 | * @param GQL\TypeInterface $interfaceAnnotation |
||
| 371 | * @param array $classAnnotations |
||
| 372 | * @param array $properties |
||
| 373 | * @param array $methods |
||
| 374 | * @param string $namespace |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | 19 | private static function typeInterfaceAnnotationToGQLConfiguration(GQL\TypeInterface $interfaceAnnotation, array $classAnnotations, array $properties, array $methods, string $namespace) |
|
| 379 | { |
||
| 380 | 19 | $interfaceConfiguration = []; |
|
| 381 | |||
| 382 | 19 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $properties); |
|
| 383 | 19 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $methods, false, true) + $fields; |
|
| 384 | |||
| 385 | 19 | $interfaceConfiguration['fields'] = $fields; |
|
| 386 | 19 | $interfaceConfiguration = self::getDescriptionConfiguration($classAnnotations) + $interfaceConfiguration; |
|
| 387 | |||
| 388 | 19 | $interfaceConfiguration['resolveType'] = self::formatExpression($interfaceAnnotation->resolveType); |
|
| 389 | |||
| 390 | 19 | return ['type' => 'interface', 'config' => $interfaceConfiguration]; |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Create a GraphQL Input type configuration from annotations on properties. |
||
| 395 | * |
||
| 396 | * @param GQL\Input $inputAnnotation |
||
| 397 | * @param array $classAnnotations |
||
| 398 | * @param array $properties |
||
| 399 | * @param string $namespace |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | 19 | private static function inputAnnotationToGQLConfiguration(GQL\Input $inputAnnotation, array $classAnnotations, array $properties, string $namespace): array |
|
| 404 | { |
||
| 405 | 19 | $inputConfiguration = []; |
|
| 406 | 19 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $properties, true); |
|
| 407 | |||
| 408 | 19 | $inputConfiguration['fields'] = $fields; |
|
| 409 | 19 | $inputConfiguration = self::getDescriptionConfiguration($classAnnotations) + $inputConfiguration; |
|
| 410 | |||
| 411 | 19 | return ['type' => $inputAnnotation->isRelay ? 'relay-mutation-input' : 'input-object', 'config' => $inputConfiguration]; |
|
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get a GraphQL scalar configuration from given scalar annotation. |
||
| 416 | * |
||
| 417 | * @param string $className |
||
| 418 | * @param GQL\Scalar $scalarAnnotation |
||
| 419 | * @param array $classAnnotations |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | 19 | private static function scalarAnnotationToGQLConfiguration(string $className, GQL\Scalar $scalarAnnotation, array $classAnnotations): array |
|
| 424 | { |
||
| 425 | 19 | $scalarConfiguration = []; |
|
| 426 | |||
| 427 | 19 | if ($scalarAnnotation->scalarType) { |
|
| 428 | 19 | $scalarConfiguration['scalarType'] = self::formatExpression($scalarAnnotation->scalarType); |
|
| 429 | } else { |
||
| 430 | $scalarConfiguration = [ |
||
| 431 | 19 | 'serialize' => [$className, 'serialize'], |
|
| 432 | 19 | 'parseValue' => [$className, 'parseValue'], |
|
| 433 | 19 | 'parseLiteral' => [$className, 'parseLiteral'], |
|
| 434 | ]; |
||
| 435 | } |
||
| 436 | |||
| 437 | 19 | $scalarConfiguration = self::getDescriptionConfiguration($classAnnotations) + $scalarConfiguration; |
|
| 438 | |||
| 439 | 19 | return ['type' => 'custom-scalar', 'config' => $scalarConfiguration]; |
|
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get a GraphQL Enum configuration from given enum annotation. |
||
| 444 | * |
||
| 445 | * @param GQL\Enum $enumAnnotation |
||
| 446 | * @param array $classAnnotations |
||
| 447 | * @param array $constants |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | 19 | private static function enumAnnotationToGQLConfiguration(GQL\Enum $enumAnnotation, array $classAnnotations, array $constants): array |
|
| 452 | { |
||
| 453 | 19 | $enumValues = $enumAnnotation->values ? $enumAnnotation->values : []; |
|
| 454 | |||
| 455 | 19 | $values = []; |
|
| 456 | |||
| 457 | 19 | foreach ($constants as $name => $value) { |
|
| 458 | $valueAnnotation = \current(\array_filter($enumValues, function ($enumValueAnnotation) use ($name) { |
||
| 459 | 19 | return $enumValueAnnotation->name == $name; |
|
| 460 | 19 | })); |
|
| 461 | 19 | $valueConfig = []; |
|
| 462 | 19 | $valueConfig['value'] = $value; |
|
| 463 | |||
| 464 | 19 | if ($valueAnnotation && $valueAnnotation->description) { |
|
| 465 | 19 | $valueConfig['description'] = $valueAnnotation->description; |
|
| 466 | } |
||
| 467 | |||
| 468 | 19 | if ($valueAnnotation && $valueAnnotation->deprecationReason) { |
|
| 469 | 19 | $valueConfig['deprecationReason'] = $valueAnnotation->deprecationReason; |
|
| 470 | } |
||
| 471 | |||
| 472 | 19 | $values[$name] = $valueConfig; |
|
| 473 | } |
||
| 474 | |||
| 475 | 19 | $enumConfiguration = ['values' => $values]; |
|
| 476 | 19 | $enumConfiguration = self::getDescriptionConfiguration($classAnnotations) + $enumConfiguration; |
|
| 477 | |||
| 478 | 19 | return ['type' => 'enum', 'config' => $enumConfiguration]; |
|
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get a GraphQL Union configuration from given union annotation. |
||
| 483 | * |
||
| 484 | * @param string $className |
||
| 485 | * @param GQL\Union $unionAnnotation |
||
| 486 | * @param array $classAnnotations |
||
| 487 | * @param array $methods |
||
| 488 | * |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | 19 | private static function unionAnnotationToGQLConfiguration(string $className, GQL\Union $unionAnnotation, array $classAnnotations, array $methods): array |
|
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Create GraphQL fields configuration based on annotations. |
||
| 516 | * |
||
| 517 | * @param string $namespace |
||
| 518 | * @param array $propertiesOrMethods |
||
| 519 | * @param bool $isInput |
||
| 520 | * @param bool $isMethod |
||
| 521 | * @param string $currentValue |
||
| 522 | * |
||
| 523 | * @return array |
||
| 524 | */ |
||
| 525 | 20 | private static function getGraphQLFieldsFromAnnotations(string $namespace, array $propertiesOrMethods, bool $isInput = false, bool $isMethod = false, string $currentValue = 'value', string $fieldAnnotationName = 'Field'): array |
|
| 647 | } |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Return fields config from Provider methods. |
||
| 651 | * |
||
| 652 | * @param string $namespace |
||
| 653 | * @param string $annotationName |
||
| 654 | * @param string $targetType |
||
| 655 | * @param bool $isRoot |
||
| 656 | * |
||
| 657 | * @return array |
||
| 658 | */ |
||
| 659 | 20 | private static function getGraphQLFieldsFromProviders(string $namespace, string $annotationName, string $targetType, bool $isRoot = false) |
|
| 660 | { |
||
| 661 | 20 | $fields = []; |
|
| 662 | 20 | foreach (self::$providers as $className => $configuration) { |
|
| 663 | 19 | $providerMethods = $configuration['methods']; |
|
| 664 | 19 | $providerAnnotation = $configuration['annotation']; |
|
| 665 | |||
| 666 | 19 | $filteredMethods = []; |
|
| 667 | 19 | foreach ($providerMethods as $methodName => $config) { |
|
| 668 | 19 | $annotations = $config['annotations']; |
|
| 669 | |||
| 670 | 19 | $annotation = self::getFirstAnnotationMatching($annotations, \sprintf('Overblog\\GraphQLBundle\\Annotation\\%s', $annotationName)); |
|
| 671 | 19 | if (!$annotation) { |
|
| 672 | 19 | continue; |
|
| 673 | } |
||
| 674 | |||
| 675 | 19 | $annotationTarget = 'Query' === $annotationName ? $annotation->targetType : null; |
|
| 676 | 19 | if (!$annotationTarget && $isRoot) { |
|
| 677 | 19 | $annotationTarget = $targetType; |
|
| 678 | } |
||
| 679 | |||
| 680 | 19 | if ($annotationTarget !== $targetType) { |
|
| 681 | 19 | continue; |
|
| 682 | } |
||
| 683 | |||
| 684 | 19 | $filteredMethods[$methodName] = $config; |
|
| 685 | } |
||
| 686 | |||
| 687 | 19 | $currentValue = \sprintf("service('%s')", self::formatNamespaceForExpression($className)); |
|
| 688 | 19 | $providerFields = self::getGraphQLFieldsFromAnnotations($namespace, $filteredMethods, false, true, $currentValue, $annotationName); |
|
| 689 | 19 | foreach ($providerFields as $fieldName => $fieldConfig) { |
|
| 690 | 19 | if ($providerAnnotation->prefix) { |
|
| 691 | 19 | $fieldName = \sprintf('%s%s', $providerAnnotation->prefix, $fieldName); |
|
| 692 | } |
||
| 693 | 19 | $fields[$fieldName] = $fieldConfig; |
|
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | 20 | return $fields; |
|
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Get the config for description & deprecation reason. |
||
| 702 | * |
||
| 703 | * @param array $annotations |
||
| 704 | * @param bool $withDeprecation |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | 20 | private static function getDescriptionConfiguration(array $annotations, bool $withDeprecation = false): array |
|
| 709 | { |
||
| 710 | 20 | $config = []; |
|
| 711 | 20 | $descriptionAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Description::class); |
|
| 712 | 20 | if ($descriptionAnnotation) { |
|
| 713 | 19 | $config['description'] = $descriptionAnnotation->value; |
|
| 714 | } |
||
| 715 | |||
| 716 | 20 | if ($withDeprecation) { |
|
| 717 | 19 | $deprecatedAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Deprecated::class); |
|
| 718 | 19 | if ($deprecatedAnnotation) { |
|
| 719 | 19 | $config['deprecationReason'] = $deprecatedAnnotation->value; |
|
| 720 | } |
||
| 721 | } |
||
| 722 | |||
| 723 | 20 | return $config; |
|
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Get args config from an array of @Arg annotation or by auto-guessing if a method is provided. |
||
| 728 | * |
||
| 729 | * @param array $args |
||
| 730 | * @param \ReflectionMethod $method |
||
| 731 | * |
||
| 732 | * @return array |
||
| 733 | */ |
||
| 734 | 19 | private static function getArgs(array $args = null, \ReflectionMethod $method = null): array |
|
| 735 | { |
||
| 736 | 19 | $config = []; |
|
| 737 | 19 | if ($args && !empty($args)) { |
|
| 738 | 19 | foreach ($args as $arg) { |
|
| 739 | 19 | $config[$arg->name] = ['type' => $arg->type] |
|
| 740 | 19 | + ($arg->description ? ['description' => $arg->description] : []) |
|
| 741 | 19 | + ($arg->default ? ['defaultValue' => $arg->default] : []); |
|
| 742 | } |
||
| 743 | 19 | } elseif ($method) { |
|
| 744 | 19 | $config = self::guessArgs($method); |
|
| 745 | } |
||
| 746 | |||
| 747 | 19 | return $config; |
|
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Format an array of args to a list of arguments in an expression. |
||
| 752 | * |
||
| 753 | * @param array $args |
||
| 754 | * |
||
| 755 | * @return string |
||
| 756 | */ |
||
| 757 | 19 | private static function formatArgsForExpression(array $args): string |
|
| 758 | { |
||
| 759 | 19 | $mapping = []; |
|
| 760 | 19 | foreach ($args as $name => $config) { |
|
| 761 | 19 | $mapping[] = \sprintf('%s: "%s"', $name, $config['type']); |
|
| 762 | } |
||
| 763 | |||
| 764 | 19 | return \sprintf('arguments({%s}, args)', \implode(', ', $mapping)); |
|
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Format a namespace to be used in an expression (double escape). |
||
| 769 | * |
||
| 770 | * @param string $namespace |
||
| 771 | * |
||
| 772 | * @return string |
||
| 773 | */ |
||
| 774 | 19 | private static function formatNamespaceForExpression(string $namespace): string |
|
| 775 | { |
||
| 776 | 19 | return \str_replace('\\', '\\\\', $namespace); |
|
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Get the first annotation matching given class. |
||
| 781 | * |
||
| 782 | * @param array $annotations |
||
| 783 | * @param string|array $annotationClass |
||
| 784 | * |
||
| 785 | * @return mixed |
||
| 786 | */ |
||
| 787 | 20 | private static function getFirstAnnotationMatching(array $annotations, $annotationClass) |
|
| 788 | { |
||
| 789 | 20 | if (\is_string($annotationClass)) { |
|
| 790 | 20 | $annotationClass = [$annotationClass]; |
|
| 791 | } |
||
| 792 | |||
| 793 | 20 | foreach ($annotations as $annotation) { |
|
| 794 | 20 | foreach ($annotationClass as $class) { |
|
| 795 | 20 | if ($annotation instanceof $class) { |
|
| 796 | 19 | return $annotation; |
|
| 797 | } |
||
| 798 | } |
||
| 799 | } |
||
| 800 | |||
| 801 | 20 | return false; |
|
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Format an expression (ie. add "@=" if not set). |
||
| 806 | * |
||
| 807 | * @param string $expression |
||
| 808 | * |
||
| 809 | * @return string |
||
| 810 | */ |
||
| 811 | 19 | private static function formatExpression(string $expression) |
|
| 812 | { |
||
| 813 | 19 | return '@=' === \substr($expression, 0, 2) ? $expression : \sprintf('@=%s', $expression); |
|
| 814 | } |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Suffix a name if it is not already. |
||
| 818 | * |
||
| 819 | * @param string $name |
||
| 820 | * @param string $suffix |
||
| 821 | * |
||
| 822 | * @return string |
||
| 823 | */ |
||
| 824 | 19 | private static function suffixName(string $name, string $suffix) |
|
| 825 | { |
||
| 826 | 19 | return \substr($name, -\strlen($suffix)) === $suffix ? $name : \sprintf('%s%s', $name, $suffix); |
|
| 827 | } |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Try to guess a field type base on is annotations. |
||
| 831 | * |
||
| 832 | * @param string $namespace |
||
| 833 | * @param array $annotations |
||
| 834 | * |
||
| 835 | * @return string |
||
| 836 | * |
||
| 837 | * @throws \RuntimeException |
||
| 838 | */ |
||
| 839 | 19 | private static function guessType(string $namespace, array $annotations): string |
|
| 840 | { |
||
| 841 | 19 | $columnAnnotation = self::getFirstAnnotationMatching($annotations, Column::class); |
|
| 842 | 19 | if ($columnAnnotation) { |
|
| 843 | 19 | $type = self::resolveTypeFromDoctrineType($columnAnnotation->type); |
|
| 844 | 19 | $nullable = $columnAnnotation->nullable; |
|
| 845 | 19 | if ($type) { |
|
| 846 | 19 | return $nullable ? $type : \sprintf('%s!', $type); |
|
| 847 | } else { |
||
| 848 | 1 | throw new \RuntimeException(\sprintf('Unable to auto-guess GraphQL type from Doctrine type "%s"', $columnAnnotation->type)); |
|
| 849 | } |
||
| 850 | } |
||
| 851 | |||
| 852 | $associationAnnotations = [ |
||
| 853 | 19 | OneToMany::class => true, |
|
| 854 | OneToOne::class => false, |
||
| 855 | ManyToMany::class => true, |
||
| 856 | ManyToOne::class => false, |
||
| 857 | ]; |
||
| 858 | |||
| 859 | 19 | $associationAnnotation = self::getFirstAnnotationMatching($annotations, \array_keys($associationAnnotations)); |
|
| 860 | 19 | if ($associationAnnotation) { |
|
| 861 | 19 | $target = self::fullyQualifiedClassName($associationAnnotation->targetEntity, $namespace); |
|
| 862 | 19 | $type = self::resolveTypeFromClass($target, ['type']); |
|
| 863 | |||
| 864 | 19 | if ($type) { |
|
| 865 | 19 | $isMultiple = $associationAnnotations[\get_class($associationAnnotation)]; |
|
| 866 | 19 | if ($isMultiple) { |
|
| 867 | 19 | return \sprintf('[%s]!', $type); |
|
| 868 | } else { |
||
| 869 | 19 | $isNullable = false; |
|
| 870 | 19 | $joinColumn = self::getFirstAnnotationMatching($annotations, JoinColumn::class); |
|
| 871 | 19 | if ($joinColumn) { |
|
| 872 | 19 | $isNullable = $joinColumn->nullable; |
|
| 873 | } |
||
| 874 | |||
| 875 | 19 | return \sprintf('%s%s', $type, $isNullable ? '' : '!'); |
|
| 876 | } |
||
| 877 | } else { |
||
| 878 | 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)); |
|
| 879 | } |
||
| 880 | } |
||
| 881 | |||
| 882 | throw new InvalidArgumentException(\sprintf('No Doctrine ORM annotation found.')); |
||
| 883 | } |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Resolve a FQN from classname and namespace. |
||
| 887 | * |
||
| 888 | * @param string $className |
||
| 889 | * @param string $namespace |
||
| 890 | * |
||
| 891 | * @return string |
||
| 892 | * |
||
| 893 | * @internal |
||
| 894 | */ |
||
| 895 | 19 | public static function fullyQualifiedClassName(string $className, string $namespace): string |
|
| 902 | } |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Resolve a GraphQLType from a doctrine type. |
||
| 906 | * |
||
| 907 | * @param string $doctrineType |
||
| 908 | * |
||
| 909 | * @return string|null |
||
| 910 | */ |
||
| 911 | 19 | private static function resolveTypeFromDoctrineType(string $doctrineType): ?string |
|
| 912 | { |
||
| 913 | 19 | if (isset(self::$doctrineMapping[$doctrineType])) { |
|
| 914 | 19 | return self::$doctrineMapping[$doctrineType]; |
|
| 915 | } |
||
| 916 | |||
| 917 | 19 | switch ($doctrineType) { |
|
| 918 | 19 | case 'integer': |
|
| 919 | 19 | case 'smallint': |
|
| 920 | 19 | case 'bigint': |
|
| 921 | 19 | return 'Int'; |
|
| 922 | 19 | case 'string': |
|
| 923 | 1 | case 'text': |
|
| 924 | 19 | return 'String'; |
|
| 925 | 1 | case 'bool': |
|
| 926 | 1 | case 'boolean': |
|
| 927 | return 'Boolean'; |
||
| 928 | 1 | case 'float': |
|
| 929 | 1 | case 'decimal': |
|
| 930 | return 'Float'; |
||
| 931 | default: |
||
| 932 | 1 | return null; |
|
| 933 | } |
||
| 934 | } |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Transform a method arguments from reflection to a list of GraphQL argument. |
||
| 938 | */ |
||
| 939 | 19 | private static function guessArgs(\ReflectionMethod $method): array |
|
| 940 | { |
||
| 941 | 19 | $arguments = []; |
|
| 942 | 19 | foreach ($method->getParameters() as $index => $parameter) { |
|
| 943 | 19 | if (!$parameter->hasType()) { |
|
| 944 | 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())); |
|
| 945 | } |
||
| 946 | |||
| 947 | try { |
||
| 948 | 19 | $gqlType = self::resolveGraphQLTypeFromReflectionType($parameter->getType(), self::VALID_INPUT_TYPES, $parameter->isDefaultValueAvailable()); |
|
| 949 | } catch (\Exception $e) { |
||
| 950 | throw new InvalidArgumentException(\sprintf('Argument n°%s "$%s" on method "%s" cannot be auto-guessed : %s".', $index + 1, $parameter->getName(), $method->getName(), $e->getMessage())); |
||
| 951 | } |
||
| 952 | |||
| 953 | 19 | $argumentConfig = []; |
|
| 954 | 19 | if ($parameter->isDefaultValueAvailable()) { |
|
| 955 | 19 | $argumentConfig['defaultValue'] = $parameter->getDefaultValue(); |
|
| 956 | } |
||
| 957 | |||
| 958 | 19 | $argumentConfig['type'] = $gqlType; |
|
| 959 | |||
| 960 | 19 | $arguments[$parameter->getName()] = $argumentConfig; |
|
| 961 | } |
||
| 962 | |||
| 963 | 19 | return $arguments; |
|
| 964 | } |
||
| 965 | |||
| 966 | 19 | private static function resolveGraphQLTypeFromReflectionType(\ReflectionType $type, array $filterGraphQLTypes = null, bool $isOptional = false): string |
|
| 967 | { |
||
| 968 | 19 | $sType = $type->getName(); |
|
| 969 | 19 | if ($type->isBuiltin()) { |
|
| 970 | 19 | $gqlType = self::resolveTypeFromPhpType($sType); |
|
| 971 | 19 | if (null === $gqlType) { |
|
| 972 | 19 | throw new \RuntimeException(\sprintf('No corresponding GraphQL type found for builtin type "%s"', $sType)); |
|
| 973 | } |
||
| 974 | } else { |
||
| 975 | 19 | $gqlType = self::resolveTypeFromClass($sType, $filterGraphQLTypes); |
|
| 976 | 19 | if (null === $gqlType) { |
|
| 977 | throw new \RuntimeException(\sprintf('No corresponding GraphQL %s found for class "%s"', $filterGraphQLTypes ? \implode(',', $filterGraphQLTypes) : 'object', $sType)); |
||
| 978 | } |
||
| 979 | } |
||
| 980 | |||
| 981 | 19 | return \sprintf('%s%s', $gqlType, ($type->allowsNull() || $isOptional) ? '' : '!'); |
|
| 982 | } |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Resolve a GraphQL Type from a class name. |
||
| 986 | * |
||
| 987 | * @param string $className |
||
| 988 | * @param array $wantedTypes |
||
| 989 | * |
||
| 990 | * @return string|null |
||
| 991 | */ |
||
| 992 | 19 | private static function resolveTypeFromClass(string $className, array $wantedTypes = null): ?string |
|
| 993 | { |
||
| 994 | 19 | foreach (self::$classesMap as $gqlType => $config) { |
|
| 995 | 19 | if ($config['class'] === $className) { |
|
| 996 | 19 | if (!$wantedTypes || \in_array($config['type'], $wantedTypes)) { |
|
| 997 | 19 | return $gqlType; |
|
| 998 | } |
||
| 999 | } |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | 1 | return null; |
|
| 1003 | } |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Resolve a PHP class from a GraphQL type. |
||
| 1007 | * |
||
| 1008 | * @param string $type |
||
| 1009 | * |
||
| 1010 | * @return string|array|null |
||
| 1011 | */ |
||
| 1012 | 19 | private static function resolveClassFromType(string $type) |
|
| 1013 | { |
||
| 1014 | 19 | return self::$classesMap[$type] ?? null; |
|
| 1015 | } |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Convert a PHP Builtin type to a GraphQL type. |
||
| 1019 | * |
||
| 1020 | * @param string $phpType |
||
| 1021 | * |
||
| 1022 | * @return string|null |
||
| 1023 | */ |
||
| 1024 | 19 | private static function resolveTypeFromPhpType(string $phpType): ?string |
|
| 1040 | } |
||
| 1041 | } |
||
| 1042 | } |
||
| 1043 |