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