We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 204 |
| Total Lines | 1030 |
| Duplicated Lines | 0 % |
| Coverage | 95.68% |
| 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 | 58 | public static function reset(): void |
|
| 69 | { |
||
| 70 | 58 | self::$classesMap = []; |
|
| 71 | 58 | self::$providers = []; |
|
| 72 | 58 | self::$classAnnotationsCache = []; |
|
| 73 | 58 | self::$annotationReader = null; |
|
| 74 | 58 | } |
|
| 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( |
|
| 134 | \ReflectionClass $reflectionEntity, |
||
| 135 | $classAnnotation, |
||
| 136 | array $configs, |
||
| 137 | array $classAnnotations, |
||
| 138 | array $properties, |
||
| 139 | array $methods, |
||
| 140 | array $gqlTypes, |
||
| 141 | bool $preProcess |
||
| 142 | ): array { |
||
| 143 | 20 | $gqlConfiguration = $gqlType = $gqlName = null; |
|
| 144 | |||
| 145 | switch (true) { |
||
| 146 | 20 | case $classAnnotation instanceof GQL\Type: |
|
| 147 | 20 | $gqlType = self::GQL_TYPE; |
|
| 148 | 20 | $gqlName = $classAnnotation->name ?: $reflectionEntity->getShortName(); |
|
| 149 | 20 | if (!$preProcess) { |
|
| 150 | 20 | $gqlConfiguration = self::typeAnnotationToGQLConfiguration( |
|
| 151 | 20 | $reflectionEntity, $classAnnotation, $gqlName, $classAnnotations, $properties, $methods, $configs |
|
| 152 | ); |
||
| 153 | |||
| 154 | 20 | if ($classAnnotation instanceof GQL\Relay\Connection) { |
|
| 155 | 19 | if (!$reflectionEntity->implementsInterface(ConnectionInterface::class)) { |
|
| 156 | throw new InvalidArgumentException(\sprintf('The annotation @Connection on class "%s" can only be used on class implementing the ConnectionInterface.', $reflectionEntity->getName())); |
||
| 157 | } |
||
| 158 | |||
| 159 | 19 | if (!($classAnnotation->edge xor $classAnnotation->node)) { |
|
| 160 | throw new InvalidArgumentException(\sprintf('The annotation @Connection on class "%s" is invalid. You must define the "edge" OR the "node" attribute.', $reflectionEntity->getName())); |
||
| 161 | } |
||
| 162 | |||
| 163 | 19 | $edgeType = $classAnnotation->edge; |
|
| 164 | 19 | if (!$edgeType) { |
|
| 165 | 19 | $edgeType = \sprintf('%sEdge', $gqlName); |
|
| 166 | 19 | $gqlTypes[$edgeType] = [ |
|
| 167 | 19 | 'type' => 'object', |
|
| 168 | 'config' => [ |
||
| 169 | 'builders' => [ |
||
| 170 | 19 | ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]], |
|
| 171 | ], |
||
| 172 | ], |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | 19 | if (!isset($gqlConfiguration['config']['builders'])) { |
|
| 176 | 19 | $gqlConfiguration['config']['builders'] = []; |
|
| 177 | } |
||
| 178 | 19 | \array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-connection', 'builderConfig' => ['edgeType' => $edgeType]]); |
|
| 179 | } |
||
| 180 | } |
||
| 181 | 20 | break; |
|
| 182 | |||
| 183 | 19 | case $classAnnotation instanceof GQL\Input: |
|
| 184 | 19 | $gqlType = self::GQL_INPUT; |
|
| 185 | 19 | $gqlName = $classAnnotation->name ?: self::suffixName($reflectionEntity->getShortName(), 'Input'); |
|
| 186 | 19 | if (!$preProcess) { |
|
| 187 | 19 | $gqlConfiguration = self::inputAnnotationToGQLConfiguration( |
|
| 188 | 19 | $classAnnotation, $classAnnotations, $properties, $reflectionEntity->getNamespaceName() |
|
| 189 | ); |
||
| 190 | } |
||
| 191 | 19 | break; |
|
| 192 | |||
| 193 | 19 | case $classAnnotation instanceof GQL\Scalar: |
|
| 194 | 19 | $gqlType = self::GQL_SCALAR; |
|
| 195 | 19 | if (!$preProcess) { |
|
| 196 | 19 | $gqlConfiguration = self::scalarAnnotationToGQLConfiguration( |
|
| 197 | 19 | $reflectionEntity->getName(), $classAnnotation, $classAnnotations |
|
| 198 | ); |
||
| 199 | } |
||
| 200 | 19 | break; |
|
| 201 | |||
| 202 | 19 | case $classAnnotation instanceof GQL\Enum: |
|
| 203 | 19 | $gqlType = self::GQL_ENUM; |
|
| 204 | 19 | if (!$preProcess) { |
|
| 205 | 19 | $gqlConfiguration = self::enumAnnotationToGQLConfiguration( |
|
| 206 | 19 | $classAnnotation, $classAnnotations, $reflectionEntity->getConstants() |
|
| 207 | ); |
||
| 208 | } |
||
| 209 | 19 | break; |
|
| 210 | |||
| 211 | 19 | case $classAnnotation instanceof GQL\Union: |
|
| 212 | 19 | $gqlType = self::GQL_UNION; |
|
| 213 | 19 | if (!$preProcess) { |
|
| 214 | 19 | $gqlConfiguration = self::unionAnnotationToGQLConfiguration( |
|
| 215 | 19 | $reflectionEntity->getName(), $classAnnotation, $classAnnotations, $methods |
|
| 216 | ); |
||
| 217 | } |
||
| 218 | 19 | break; |
|
| 219 | |||
| 220 | 19 | case $classAnnotation instanceof GQL\TypeInterface: |
|
| 221 | 19 | $gqlType = self::GQL_INTERFACE; |
|
| 222 | 19 | if (!$preProcess) { |
|
| 223 | 19 | $gqlConfiguration = self::typeInterfaceAnnotationToGQLConfiguration( |
|
| 224 | 19 | $classAnnotation, $classAnnotations, $properties, $methods, $reflectionEntity->getNamespaceName() |
|
| 225 | ); |
||
| 226 | } |
||
| 227 | 19 | break; |
|
| 228 | |||
| 229 | 19 | case $classAnnotation instanceof GQL\Provider: |
|
| 230 | 19 | if ($preProcess) { |
|
| 231 | 19 | self::$providers[$reflectionEntity->getName()] = ['annotation' => $classAnnotation, 'methods' => $methods, 'annotations' => $classAnnotations]; |
|
| 232 | } |
||
| 233 | 19 | break; |
|
| 234 | } |
||
| 235 | |||
| 236 | 20 | if (null !== $gqlType) { |
|
| 237 | 20 | if (!$gqlName) { |
|
| 238 | 19 | $gqlName = $classAnnotation->name ?: $reflectionEntity->getShortName(); |
|
|
|
|||
| 239 | } |
||
| 240 | |||
| 241 | 20 | if ($preProcess) { |
|
| 242 | 20 | if (isset(self::$classesMap[$gqlName])) { |
|
| 243 | 1 | throw new InvalidArgumentException(\sprintf('The GraphQL type "%s" has already been registered in class "%s"', $gqlName, self::$classesMap[$gqlName]['class'])); |
|
| 244 | } |
||
| 245 | 20 | self::$classesMap[$gqlName] = ['type' => $gqlType, 'class' => $reflectionEntity->getName()]; |
|
| 246 | } else { |
||
| 247 | 20 | $gqlTypes = [$gqlName => $gqlConfiguration] + $gqlTypes; |
|
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | 20 | return $gqlTypes; |
|
| 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 | foreach ($reflectionEntity->getProperties() as $property) { |
|
| 263 | 19 | $properties[$property->getName()] = ['property' => $property, 'annotations' => $annotationReader->getPropertyAnnotations($property)]; |
|
| 264 | } |
||
| 265 | |||
| 266 | 20 | $methods = []; |
|
| 267 | 20 | foreach ($reflectionEntity->getMethods() as $method) { |
|
| 268 | 19 | $methods[$method->getName()] = ['method' => $method, 'annotations' => $annotationReader->getMethodAnnotations($method)]; |
|
| 269 | } |
||
| 270 | |||
| 271 | 20 | self::$classAnnotationsCache[$className] = [$reflectionEntity, $classAnnotations, $properties, $methods]; |
|
| 272 | } |
||
| 273 | |||
| 274 | 20 | return self::$classAnnotationsCache[$className]; |
|
| 275 | } |
||
| 276 | |||
| 277 | 20 | private static function typeAnnotationToGQLConfiguration( |
|
| 278 | \ReflectionClass $reflectionEntity, |
||
| 279 | GQL\Type $classAnnotation, |
||
| 280 | string $gqlName, |
||
| 281 | array $classAnnotations, |
||
| 282 | array $properties, |
||
| 283 | array $methods, |
||
| 284 | array $configs |
||
| 285 | ): array { |
||
| 286 | 20 | $rootQueryType = $configs['definitions']['schema']['default']['query'] ?? null; |
|
| 287 | 20 | $rootMutationType = $configs['definitions']['schema']['default']['mutation'] ?? null; |
|
| 288 | 20 | $isRootQuery = ($rootQueryType && $gqlName === $rootQueryType); |
|
| 289 | 20 | $isRootMutation = ($rootMutationType && $gqlName === $rootMutationType); |
|
| 290 | 20 | $currentValue = ($isRootQuery || $isRootMutation) ? \sprintf("service('%s')", self::formatNamespaceForExpression($reflectionEntity->getName())) : 'value'; |
|
| 291 | |||
| 292 | 20 | $gqlConfiguration = self::graphQLTypeConfigFromAnnotation($classAnnotation, $classAnnotations, $properties, $methods, $reflectionEntity->getNamespaceName(), $currentValue); |
|
| 293 | 20 | $providerFields = self::getGraphQLFieldsFromProviders($reflectionEntity->getNamespaceName(), $isRootMutation ? 'Mutation' : 'Query', $gqlName, ($isRootQuery || $isRootMutation)); |
|
| 294 | 20 | $gqlConfiguration['config']['fields'] = $providerFields + $gqlConfiguration['config']['fields']; |
|
| 295 | |||
| 296 | 20 | if ($classAnnotation instanceof GQL\Relay\Edge) { |
|
| 297 | 19 | if (!$reflectionEntity->implementsInterface(EdgeInterface::class)) { |
|
| 298 | throw new InvalidArgumentException(\sprintf('The annotation @Edge on class "%s" can only be used on class implementing the EdgeInterface.', $reflectionEntity->getName())); |
||
| 299 | } |
||
| 300 | 19 | if (!isset($gqlConfiguration['config']['builders'])) { |
|
| 301 | 19 | $gqlConfiguration['config']['builders'] = []; |
|
| 302 | } |
||
| 303 | 19 | \array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]]); |
|
| 304 | } |
||
| 305 | |||
| 306 | 20 | return $gqlConfiguration; |
|
| 307 | } |
||
| 308 | |||
| 309 | 20 | private static function getAnnotationReader() |
|
| 310 | { |
||
| 311 | 20 | if (null === self::$annotationReader) { |
|
| 312 | 20 | if (!\class_exists('\\Doctrine\\Common\\Annotations\\AnnotationReader') || |
|
| 313 | 20 | !\class_exists('\\Doctrine\\Common\\Annotations\\AnnotationRegistry')) { |
|
| 314 | throw new \RuntimeException('In order to use graphql annotation, you need to require doctrine annotations'); |
||
| 315 | } |
||
| 316 | |||
| 317 | 20 | AnnotationRegistry::registerLoader('class_exists'); |
|
| 318 | 20 | self::$annotationReader = new AnnotationReader(); |
|
| 319 | } |
||
| 320 | |||
| 321 | 20 | return self::$annotationReader; |
|
| 322 | } |
||
| 323 | |||
| 324 | 20 | private static function graphQLTypeConfigFromAnnotation(GQL\Type $typeAnnotation, array $classAnnotations, array $properties, array $methods, string $namespace, string $currentValue): array |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Create a GraphQL Interface type configuration from annotations on properties. |
||
| 363 | * |
||
| 364 | * @param GQL\TypeInterface $interfaceAnnotation |
||
| 365 | * @param array $classAnnotations |
||
| 366 | * @param array $properties |
||
| 367 | * @param array $methods |
||
| 368 | * @param string $namespace |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | 19 | private static function typeInterfaceAnnotationToGQLConfiguration(GQL\TypeInterface $interfaceAnnotation, array $classAnnotations, array $properties, array $methods, string $namespace) |
|
| 373 | { |
||
| 374 | 19 | $interfaceConfiguration = []; |
|
| 375 | |||
| 376 | 19 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $properties); |
|
| 377 | 19 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $methods, false, true) + $fields; |
|
| 378 | |||
| 379 | 19 | $interfaceConfiguration['fields'] = $fields; |
|
| 380 | 19 | $interfaceConfiguration = self::getDescriptionConfiguration($classAnnotations) + $interfaceConfiguration; |
|
| 381 | |||
| 382 | 19 | $interfaceConfiguration['resolveType'] = self::formatExpression($interfaceAnnotation->resolveType); |
|
| 383 | |||
| 384 | 19 | return ['type' => 'interface', 'config' => $interfaceConfiguration]; |
|
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Create a GraphQL Input type configuration from annotations on properties. |
||
| 389 | * |
||
| 390 | * @param GQL\Input $inputAnnotation |
||
| 391 | * @param array $classAnnotations |
||
| 392 | * @param array $properties |
||
| 393 | * @param string $namespace |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | 19 | private static function inputAnnotationToGQLConfiguration(GQL\Input $inputAnnotation, array $classAnnotations, array $properties, string $namespace): array |
|
| 398 | { |
||
| 399 | 19 | $inputConfiguration = []; |
|
| 400 | 19 | $fields = self::getGraphQLFieldsFromAnnotations($namespace, $properties, true); |
|
| 401 | |||
| 402 | 19 | $inputConfiguration['fields'] = $fields; |
|
| 403 | 19 | $inputConfiguration = self::getDescriptionConfiguration($classAnnotations) + $inputConfiguration; |
|
| 404 | |||
| 405 | 19 | return ['type' => $inputAnnotation->isRelay ? 'relay-mutation-input' : 'input-object', 'config' => $inputConfiguration]; |
|
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get a GraphQL scalar configuration from given scalar annotation. |
||
| 410 | * |
||
| 411 | * @param string $className |
||
| 412 | * @param GQL\Scalar $scalarAnnotation |
||
| 413 | * @param array $classAnnotations |
||
| 414 | * |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | 19 | private static function scalarAnnotationToGQLConfiguration(string $className, GQL\Scalar $scalarAnnotation, array $classAnnotations): array |
|
| 418 | { |
||
| 419 | 19 | $scalarConfiguration = []; |
|
| 420 | |||
| 421 | 19 | if ($scalarAnnotation->scalarType) { |
|
| 422 | 19 | $scalarConfiguration['scalarType'] = self::formatExpression($scalarAnnotation->scalarType); |
|
| 423 | } else { |
||
| 424 | $scalarConfiguration = [ |
||
| 425 | 19 | 'serialize' => [$className, 'serialize'], |
|
| 426 | 19 | 'parseValue' => [$className, 'parseValue'], |
|
| 427 | 19 | 'parseLiteral' => [$className, 'parseLiteral'], |
|
| 428 | ]; |
||
| 429 | } |
||
| 430 | |||
| 431 | 19 | $scalarConfiguration = self::getDescriptionConfiguration($classAnnotations) + $scalarConfiguration; |
|
| 432 | |||
| 433 | 19 | return ['type' => 'custom-scalar', 'config' => $scalarConfiguration]; |
|
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get a GraphQL Enum configuration from given enum annotation. |
||
| 438 | * |
||
| 439 | * @param GQL\Enum $enumAnnotation |
||
| 440 | * @param array $classAnnotations |
||
| 441 | * @param array $constants |
||
| 442 | * |
||
| 443 | * @return array |
||
| 444 | */ |
||
| 445 | 19 | private static function enumAnnotationToGQLConfiguration(GQL\Enum $enumAnnotation, array $classAnnotations, array $constants): array |
|
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Get a GraphQL Union configuration from given union annotation. |
||
| 477 | * |
||
| 478 | * @param string $className |
||
| 479 | * @param GQL\Union $unionAnnotation |
||
| 480 | * @param array $classAnnotations |
||
| 481 | * @param array $methods |
||
| 482 | * |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | 19 | private static function unionAnnotationToGQLConfiguration(string $className, GQL\Union $unionAnnotation, array $classAnnotations, array $methods): array |
|
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Create GraphQL fields configuration based on annotations. |
||
| 511 | * |
||
| 512 | * @param string $namespace |
||
| 513 | * @param array $propertiesOrMethods |
||
| 514 | * @param bool $isInput |
||
| 515 | * @param bool $isMethod |
||
| 516 | * @param string $currentValue |
||
| 517 | * |
||
| 518 | * @return array |
||
| 519 | */ |
||
| 520 | 20 | private static function getGraphQLFieldsFromAnnotations(string $namespace, array $propertiesOrMethods, bool $isInput = false, bool $isMethod = false, string $currentValue = 'value', string $fieldAnnotationName = 'Field'): array |
|
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Return fields config from Provider methods. |
||
| 646 | * |
||
| 647 | * @param string $namespace |
||
| 648 | * @param string $annotationName |
||
| 649 | * @param string $targetType |
||
| 650 | * @param bool $isRoot |
||
| 651 | * |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | 20 | private static function getGraphQLFieldsFromProviders(string $namespace, string $annotationName, string $targetType, bool $isRoot = false) |
|
| 655 | { |
||
| 656 | 20 | $fields = []; |
|
| 657 | 20 | foreach (self::$providers as $className => $configuration) { |
|
| 658 | 19 | $providerMethods = $configuration['methods']; |
|
| 659 | 19 | $providerAnnotation = $configuration['annotation']; |
|
| 660 | 19 | $providerAnnotations = $configuration['annotations']; |
|
| 661 | |||
| 662 | 19 | $defaultAccessAnnotation = self::getFirstAnnotationMatching($providerAnnotations, GQL\Access::class); |
|
| 663 | 19 | $defaultIsPublicAnnotation = self::getFirstAnnotationMatching($providerAnnotations, GQL\IsPublic::class); |
|
| 664 | |||
| 665 | 19 | $defaultAccess = $defaultAccessAnnotation ? self::formatExpression($defaultAccessAnnotation->value) : false; |
|
| 666 | 19 | $defaultIsPublic = $defaultIsPublicAnnotation ? self::formatExpression($defaultIsPublicAnnotation->value) : false; |
|
| 667 | |||
| 668 | 19 | $filteredMethods = []; |
|
| 669 | 19 | foreach ($providerMethods as $methodName => $config) { |
|
| 670 | 19 | $annotations = $config['annotations']; |
|
| 671 | |||
| 672 | 19 | $annotation = self::getFirstAnnotationMatching($annotations, \sprintf('Overblog\\GraphQLBundle\\Annotation\\%s', $annotationName)); |
|
| 673 | 19 | if (!$annotation) { |
|
| 674 | 19 | continue; |
|
| 675 | } |
||
| 676 | |||
| 677 | 19 | $annotationTarget = 'Query' === $annotationName ? $annotation->targetType : null; |
|
| 678 | 19 | if (!$annotationTarget && $isRoot) { |
|
| 679 | 19 | $annotationTarget = $targetType; |
|
| 680 | } |
||
| 681 | |||
| 682 | 19 | if ($annotationTarget !== $targetType) { |
|
| 683 | 19 | continue; |
|
| 684 | } |
||
| 685 | |||
| 686 | 19 | $filteredMethods[$methodName] = $config; |
|
| 687 | } |
||
| 688 | |||
| 689 | 19 | $currentValue = \sprintf("service('%s')", self::formatNamespaceForExpression($className)); |
|
| 690 | 19 | $providerFields = self::getGraphQLFieldsFromAnnotations($namespace, $filteredMethods, false, true, $currentValue, $annotationName); |
|
| 691 | 19 | foreach ($providerFields as $fieldName => $fieldConfig) { |
|
| 692 | 19 | if ($providerAnnotation->prefix) { |
|
| 693 | 19 | $fieldName = \sprintf('%s%s', $providerAnnotation->prefix, $fieldName); |
|
| 694 | } |
||
| 695 | |||
| 696 | 19 | if ($defaultAccess && !isset($fieldConfig['access'])) { |
|
| 697 | 19 | $fieldConfig['access'] = $defaultAccess; |
|
| 698 | } |
||
| 699 | |||
| 700 | 19 | if ($defaultIsPublic && !isset($fieldConfig['public'])) { |
|
| 701 | 19 | $fieldConfig['public'] = $defaultIsPublic; |
|
| 702 | } |
||
| 703 | |||
| 704 | 19 | $fields[$fieldName] = $fieldConfig; |
|
| 705 | } |
||
| 706 | } |
||
| 707 | |||
| 708 | 20 | return $fields; |
|
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Get the config for description & deprecation reason. |
||
| 713 | * |
||
| 714 | * @param array $annotations |
||
| 715 | * @param bool $withDeprecation |
||
| 716 | * |
||
| 717 | * @return array |
||
| 718 | */ |
||
| 719 | 20 | private static function getDescriptionConfiguration(array $annotations, bool $withDeprecation = false): array |
|
| 720 | { |
||
| 721 | 20 | $config = []; |
|
| 722 | 20 | $descriptionAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Description::class); |
|
| 723 | 20 | if ($descriptionAnnotation) { |
|
| 724 | 19 | $config['description'] = $descriptionAnnotation->value; |
|
| 725 | } |
||
| 726 | |||
| 727 | 20 | if ($withDeprecation) { |
|
| 728 | 19 | $deprecatedAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Deprecated::class); |
|
| 729 | 19 | if ($deprecatedAnnotation) { |
|
| 730 | 19 | $config['deprecationReason'] = $deprecatedAnnotation->value; |
|
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | 20 | return $config; |
|
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Get args config from an array of @Arg annotation or by auto-guessing if a method is provided. |
||
| 739 | * |
||
| 740 | * @param array $args |
||
| 741 | * @param \ReflectionMethod $method |
||
| 742 | * |
||
| 743 | * @return array |
||
| 744 | */ |
||
| 745 | 19 | private static function getArgs(array $args = null, \ReflectionMethod $method = null): array |
|
| 746 | { |
||
| 747 | 19 | $config = []; |
|
| 748 | 19 | if ($args && !empty($args)) { |
|
| 749 | 19 | foreach ($args as $arg) { |
|
| 750 | 19 | $config[$arg->name] = ['type' => $arg->type] |
|
| 751 | 19 | + ($arg->description ? ['description' => $arg->description] : []) |
|
| 752 | 19 | + ($arg->default ? ['defaultValue' => $arg->default] : []); |
|
| 753 | } |
||
| 754 | 19 | } elseif ($method) { |
|
| 755 | 19 | $config = self::guessArgs($method); |
|
| 756 | } |
||
| 757 | |||
| 758 | 19 | return $config; |
|
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Format an array of args to a list of arguments in an expression. |
||
| 763 | * |
||
| 764 | * @param array $args |
||
| 765 | * |
||
| 766 | * @return string |
||
| 767 | */ |
||
| 768 | 19 | private static function formatArgsForExpression(array $args): string |
|
| 769 | { |
||
| 770 | 19 | $mapping = []; |
|
| 771 | 19 | foreach ($args as $name => $config) { |
|
| 772 | 19 | $mapping[] = \sprintf('%s: "%s"', $name, $config['type']); |
|
| 773 | } |
||
| 774 | |||
| 775 | 19 | return \sprintf('arguments({%s}, args)', \implode(', ', $mapping)); |
|
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Format a namespace to be used in an expression (double escape). |
||
| 780 | * |
||
| 781 | * @param string $namespace |
||
| 782 | * |
||
| 783 | * @return string |
||
| 784 | */ |
||
| 785 | 19 | private static function formatNamespaceForExpression(string $namespace): string |
|
| 786 | { |
||
| 787 | 19 | return \str_replace('\\', '\\\\', $namespace); |
|
| 788 | } |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Get the first annotation matching given class. |
||
| 792 | * |
||
| 793 | * @param array $annotations |
||
| 794 | * @param string|array $annotationClass |
||
| 795 | * |
||
| 796 | * @return mixed |
||
| 797 | */ |
||
| 798 | 20 | private static function getFirstAnnotationMatching(array $annotations, $annotationClass) |
|
| 799 | { |
||
| 800 | 20 | if (\is_string($annotationClass)) { |
|
| 801 | 20 | $annotationClass = [$annotationClass]; |
|
| 802 | } |
||
| 803 | |||
| 804 | 20 | foreach ($annotations as $annotation) { |
|
| 805 | 20 | foreach ($annotationClass as $class) { |
|
| 806 | 20 | if ($annotation instanceof $class) { |
|
| 807 | 19 | return $annotation; |
|
| 808 | } |
||
| 809 | } |
||
| 810 | } |
||
| 811 | |||
| 812 | 20 | return false; |
|
| 813 | } |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Format an expression (ie. add "@=" if not set). |
||
| 817 | * |
||
| 818 | * @param string $expression |
||
| 819 | * |
||
| 820 | * @return string |
||
| 821 | */ |
||
| 822 | 19 | private static function formatExpression(string $expression) |
|
| 823 | { |
||
| 824 | 19 | return '@=' === \substr($expression, 0, 2) ? $expression : \sprintf('@=%s', $expression); |
|
| 825 | } |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Suffix a name if it is not already. |
||
| 829 | * |
||
| 830 | * @param string $name |
||
| 831 | * @param string $suffix |
||
| 832 | * |
||
| 833 | * @return string |
||
| 834 | */ |
||
| 835 | 19 | private static function suffixName(string $name, string $suffix) |
|
| 836 | { |
||
| 837 | 19 | return \substr($name, -\strlen($suffix)) === $suffix ? $name : \sprintf('%s%s', $name, $suffix); |
|
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Try to guess a field type base on is annotations. |
||
| 842 | * |
||
| 843 | * @param string $namespace |
||
| 844 | * @param array $annotations |
||
| 845 | * |
||
| 846 | * @return string |
||
| 847 | * |
||
| 848 | * @throws \RuntimeException |
||
| 849 | */ |
||
| 850 | 19 | private static function guessType(string $namespace, array $annotations): string |
|
| 851 | { |
||
| 852 | 19 | $columnAnnotation = self::getFirstAnnotationMatching($annotations, Column::class); |
|
| 853 | 19 | if ($columnAnnotation) { |
|
| 854 | 19 | $type = self::resolveTypeFromDoctrineType($columnAnnotation->type); |
|
| 855 | 19 | if (!$type) { |
|
| 856 | 1 | throw new \RuntimeException(\sprintf('Unable to auto-guess GraphQL type from Doctrine type "%s"', $columnAnnotation->type)); |
|
| 857 | } |
||
| 858 | 19 | $nullable = $columnAnnotation->nullable; |
|
| 859 | |||
| 860 | 19 | return $nullable ? $type : \sprintf('%s!', $type); |
|
| 861 | } |
||
| 862 | |||
| 863 | $associationAnnotations = [ |
||
| 864 | 19 | OneToMany::class => true, |
|
| 865 | OneToOne::class => false, |
||
| 866 | ManyToMany::class => true, |
||
| 867 | ManyToOne::class => false, |
||
| 868 | ]; |
||
| 869 | |||
| 870 | 19 | $associationAnnotation = self::getFirstAnnotationMatching($annotations, \array_keys($associationAnnotations)); |
|
| 871 | 19 | if ($associationAnnotation) { |
|
| 872 | 19 | $target = self::fullyQualifiedClassName($associationAnnotation->targetEntity, $namespace); |
|
| 873 | 19 | $type = self::resolveTypeFromClass($target, ['type']); |
|
| 874 | |||
| 875 | 19 | if (!$type) { |
|
| 876 | 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)); |
|
| 877 | } |
||
| 878 | |||
| 879 | 19 | $isMultiple = $associationAnnotations[\get_class($associationAnnotation)]; |
|
| 880 | |||
| 881 | 19 | if ($isMultiple) { |
|
| 882 | 19 | return \sprintf('[%s]!', $type); |
|
| 883 | } |
||
| 884 | |||
| 885 | 19 | $isNullable = false; |
|
| 886 | 19 | $joinColumn = self::getFirstAnnotationMatching($annotations, JoinColumn::class); |
|
| 887 | 19 | if ($joinColumn) { |
|
| 888 | 19 | $isNullable = $joinColumn->nullable; |
|
| 889 | } |
||
| 890 | |||
| 891 | 19 | return \sprintf('%s%s', $type, $isNullable ? '' : '!'); |
|
| 892 | } |
||
| 893 | |||
| 894 | throw new InvalidArgumentException(\sprintf('No Doctrine ORM annotation found.')); |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Resolve a FQN from classname and namespace. |
||
| 899 | * |
||
| 900 | * @param string $className |
||
| 901 | * @param string $namespace |
||
| 902 | * |
||
| 903 | * @return string |
||
| 904 | * |
||
| 905 | * @internal |
||
| 906 | */ |
||
| 907 | 19 | public static function fullyQualifiedClassName(string $className, string $namespace): string |
|
| 914 | } |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Resolve a GraphQLType from a doctrine type. |
||
| 918 | * |
||
| 919 | * @param string $doctrineType |
||
| 920 | * |
||
| 921 | * @return string|null |
||
| 922 | */ |
||
| 923 | 19 | private static function resolveTypeFromDoctrineType(string $doctrineType): ?string |
|
| 924 | { |
||
| 925 | 19 | if (isset(self::$doctrineMapping[$doctrineType])) { |
|
| 926 | 19 | return self::$doctrineMapping[$doctrineType]; |
|
| 927 | } |
||
| 928 | |||
| 929 | 19 | switch ($doctrineType) { |
|
| 930 | 19 | case 'integer': |
|
| 931 | 19 | case 'smallint': |
|
| 932 | 19 | case 'bigint': |
|
| 933 | 19 | return 'Int'; |
|
| 934 | 19 | case 'string': |
|
| 935 | 1 | case 'text': |
|
| 936 | 19 | return 'String'; |
|
| 937 | 1 | case 'bool': |
|
| 938 | 1 | case 'boolean': |
|
| 939 | return 'Boolean'; |
||
| 940 | 1 | case 'float': |
|
| 941 | 1 | case 'decimal': |
|
| 942 | return 'Float'; |
||
| 943 | default: |
||
| 944 | 1 | return null; |
|
| 945 | } |
||
| 946 | } |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Transform a method arguments from reflection to a list of GraphQL argument. |
||
| 950 | */ |
||
| 951 | 19 | private static function guessArgs(\ReflectionMethod $method): array |
|
| 952 | { |
||
| 953 | 19 | $arguments = []; |
|
| 954 | 19 | foreach ($method->getParameters() as $index => $parameter) { |
|
| 955 | 19 | if (!$parameter->hasType()) { |
|
| 956 | 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())); |
|
| 957 | } |
||
| 958 | |||
| 959 | try { |
||
| 960 | 19 | $gqlType = self::resolveGraphQLTypeFromReflectionType($parameter->getType(), self::VALID_INPUT_TYPES, $parameter->isDefaultValueAvailable()); |
|
| 961 | } catch (\Exception $e) { |
||
| 962 | throw new InvalidArgumentException(\sprintf('Argument n°%s "$%s" on method "%s" cannot be auto-guessed : %s".', $index + 1, $parameter->getName(), $method->getName(), $e->getMessage())); |
||
| 963 | } |
||
| 964 | |||
| 965 | 19 | $argumentConfig = []; |
|
| 966 | 19 | if ($parameter->isDefaultValueAvailable()) { |
|
| 967 | 19 | $argumentConfig['defaultValue'] = $parameter->getDefaultValue(); |
|
| 968 | } |
||
| 969 | |||
| 970 | 19 | $argumentConfig['type'] = $gqlType; |
|
| 971 | |||
| 972 | 19 | $arguments[$parameter->getName()] = $argumentConfig; |
|
| 973 | } |
||
| 974 | |||
| 975 | 19 | return $arguments; |
|
| 976 | } |
||
| 977 | |||
| 978 | 19 | private static function resolveGraphQLTypeFromReflectionType(\ReflectionType $type, array $filterGraphQLTypes = null, bool $isOptional = false): string |
|
| 979 | { |
||
| 980 | 19 | $sType = (string) $type; |
|
| 981 | 19 | if ($type->isBuiltin()) { |
|
| 982 | 19 | $gqlType = self::resolveTypeFromPhpType($sType); |
|
| 983 | 19 | if (null === $gqlType) { |
|
| 984 | 19 | throw new \RuntimeException(\sprintf('No corresponding GraphQL type found for builtin type "%s"', $sType)); |
|
| 985 | } |
||
| 986 | } else { |
||
| 987 | 19 | $gqlType = self::resolveTypeFromClass($sType, $filterGraphQLTypes); |
|
| 988 | 19 | if (null === $gqlType) { |
|
| 989 | throw new \RuntimeException(\sprintf('No corresponding GraphQL %s found for class "%s"', $filterGraphQLTypes ? \implode(',', $filterGraphQLTypes) : 'object', $sType)); |
||
| 990 | } |
||
| 991 | } |
||
| 992 | |||
| 993 | 19 | return \sprintf('%s%s', $gqlType, ($type->allowsNull() || $isOptional) ? '' : '!'); |
|
| 994 | } |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Resolve a GraphQL Type from a class name. |
||
| 998 | * |
||
| 999 | * @param string $className |
||
| 1000 | * @param array $wantedTypes |
||
| 1001 | * |
||
| 1002 | * @return string|null |
||
| 1003 | */ |
||
| 1004 | 19 | private static function resolveTypeFromClass(string $className, array $wantedTypes = null): ?string |
|
| 1005 | { |
||
| 1006 | 19 | foreach (self::$classesMap as $gqlType => $config) { |
|
| 1007 | 19 | if ($config['class'] === $className) { |
|
| 1008 | 19 | if (!$wantedTypes || \in_array($config['type'], $wantedTypes)) { |
|
| 1009 | 19 | return $gqlType; |
|
| 1010 | } |
||
| 1011 | } |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | 1 | return null; |
|
| 1015 | } |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Resolve a PHP class from a GraphQL type. |
||
| 1019 | * |
||
| 1020 | * @param string $type |
||
| 1021 | * |
||
| 1022 | * @return string|array|null |
||
| 1023 | */ |
||
| 1024 | 19 | private static function resolveClassFromType(string $type) |
|
| 1025 | { |
||
| 1026 | 19 | return self::$classesMap[$type] ?? null; |
|
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Convert a PHP Builtin type to a GraphQL type. |
||
| 1031 | * |
||
| 1032 | * @param string $phpType |
||
| 1033 | * |
||
| 1034 | * @return string|null |
||
| 1035 | */ |
||
| 1036 | 19 | private static function resolveTypeFromPhpType(string $phpType): ?string |
|
| 1052 | } |
||
| 1053 | } |
||
| 1054 | } |
||
| 1055 |