We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 212 |
| Total Lines | 973 |
| Duplicated Lines | 0 % |
| Coverage | 95.86% |
| Changes | 6 | ||
| 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 |
||
| 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 |
|
| 73 | { |
||
| 74 | 25 | $container->setParameter('overblog_graphql_types.classes_map', self::processFile($file, $container, $configs, true)); |
|
| 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 |
|
| 102 | { |
||
| 103 | 25 | self::$doctrineMapping = $configs['doctrine']['types_mapping']; |
|
| 104 | 25 | $container->addResource(new FileResource($file->getRealPath())); |
|
| 105 | |||
| 106 | try { |
||
| 107 | 25 | $className = $file->getBasename('.php'); |
|
| 108 | 25 | if (preg_match('#namespace (.+);#', file_get_contents($file->getRealPath()), $matches)) { |
|
| 109 | 25 | $className = trim($matches[1]).'\\'.$className; |
|
| 110 | } |
||
| 111 | |||
| 112 | 25 | $gqlTypes = []; |
|
| 113 | 25 | $graphClass = self::getGraphClass($className); |
|
| 114 | |||
| 115 | 25 | foreach ($graphClass->getAnnotations() as $classAnnotation) { |
|
| 116 | 25 | $gqlTypes = self::classAnnotationsToGQLConfiguration( |
|
| 117 | 25 | $graphClass, |
|
| 118 | $classAnnotation, |
||
| 119 | $configs, |
||
| 120 | $gqlTypes, |
||
| 121 | $preProcess |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | 25 | return $preProcess ? self::$classesMap : $gqlTypes; |
|
| 126 | 10 | } catch (\InvalidArgumentException $e) { |
|
| 127 | 10 | throw new InvalidArgumentException(sprintf('Failed to parse GraphQL annotations from file "%s".', $file), $e->getCode(), $e); |
|
| 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 | 24 | 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 |
|
| 242 | { |
||
| 243 | 25 | self::$graphClassCache[$className] ??= new GraphClass($className); |
|
| 244 | |||
| 245 | 25 | return self::$graphClassCache[$className]; |
|
| 246 | } |
||
| 247 | |||
| 248 | 25 | private static function typeAnnotationToGQLConfiguration( |
|
| 249 | GraphClass $graphClass, |
||
| 250 | GQL\Type $classAnnotation, |
||
| 251 | string $gqlName, |
||
| 252 | array $configs |
||
| 253 | ): array { |
||
| 254 | 25 | $isMutation = $isDefault = $isRoot = false; |
|
| 255 | 25 | if (isset($configs['definitions']['schema'])) { |
|
| 256 | 24 | foreach ($configs['definitions']['schema'] as $schemaName => $schema) { |
|
| 257 | 24 | $schemaQuery = $schema['query'] ?? null; |
|
| 258 | 24 | $schemaMutation = $schema['mutation'] ?? null; |
|
| 259 | |||
| 260 | 24 | if ($schemaQuery && $gqlName === $schemaQuery) { |
|
| 261 | 24 | $isRoot = true; |
|
| 262 | 24 | if ('default' == $schemaName) { |
|
| 263 | 24 | $isDefault = true; |
|
| 264 | } |
||
| 265 | 24 | } elseif ($schemaMutation && $gqlName === $schemaMutation) { |
|
| 266 | 24 | $isMutation = true; |
|
| 267 | 24 | $isRoot = true; |
|
| 268 | 24 | if ('default' == $schemaName) { |
|
| 269 | 24 | $isDefault = true; |
|
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | 25 | $currentValue = $isRoot ? sprintf("service('%s')", self::formatNamespaceForExpression($graphClass->getName())) : 'value'; |
|
| 276 | |||
| 277 | 25 | $gqlConfiguration = self::graphQLTypeConfigFromAnnotation($graphClass, $classAnnotation, $currentValue); |
|
| 278 | |||
| 279 | 25 | $providerFields = self::getGraphQLFieldsFromProviders($graphClass, $isMutation ? GQL\Mutation::class : GQL\Query::class, $gqlName, $isDefault); |
|
| 280 | 25 | $gqlConfiguration['config']['fields'] = array_merge($gqlConfiguration['config']['fields'], $providerFields); |
|
| 281 | |||
| 282 | 25 | if ($classAnnotation instanceof GQL\Relay\Edge) { |
|
| 283 | 24 | if (!$graphClass->implementsInterface(EdgeInterface::class)) { |
|
| 284 | throw new InvalidArgumentException(sprintf('The annotation @Edge on class "%s" can only be used on class implementing the EdgeInterface.', $graphClass->getName())); |
||
| 285 | } |
||
| 286 | 24 | if (!isset($gqlConfiguration['config']['builders'])) { |
|
| 287 | 24 | $gqlConfiguration['config']['builders'] = []; |
|
| 288 | } |
||
| 289 | 24 | array_unshift($gqlConfiguration['config']['builders'], ['builder' => 'relay-edge', 'builderConfig' => ['nodeType' => $classAnnotation->node]]); |
|
| 290 | } |
||
| 291 | |||
| 292 | 25 | return $gqlConfiguration; |
|
| 293 | } |
||
| 294 | |||
| 295 | 25 | private static function graphQLTypeConfigFromAnnotation(GraphClass $graphClass, GQL\Type $typeAnnotation, string $currentValue): array |
|
| 296 | { |
||
| 297 | 25 | $typeConfiguration = []; |
|
| 298 | 25 | $fieldsFromProperties = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended(), GQL\Field::class, $currentValue); |
|
| 299 | 25 | $fieldsFromMethods = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getMethods(), GQL\Field::class, $currentValue); |
|
| 300 | |||
| 301 | 25 | $typeConfiguration['fields'] = array_merge($fieldsFromProperties, $fieldsFromMethods); |
|
| 302 | 25 | $typeConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $typeConfiguration; |
|
| 303 | |||
| 304 | 25 | if (null !== $typeAnnotation->interfaces) { |
|
| 305 | 24 | $typeConfiguration['interfaces'] = $typeAnnotation->interfaces; |
|
| 306 | } else { |
||
| 307 | $typeConfiguration['interfaces'] = array_keys(self::searchClassesMapBy(function ($gqlType, $configuration) use ($graphClass) { |
||
| 308 | 24 | ['class' => $interfaceClassName] = $configuration; |
|
| 309 | |||
| 310 | 24 | $interfaceMetadata = self::getGraphClass($interfaceClassName); |
|
| 311 | 24 | if ($interfaceMetadata->isInterface() && $graphClass->implementsInterface($interfaceMetadata->getName())) { |
|
| 312 | return true; |
||
| 313 | } |
||
| 314 | |||
| 315 | 24 | return $graphClass->isSubclassOf($interfaceClassName); |
|
| 316 | 25 | }, self::GQL_INTERFACE)); |
|
| 317 | } |
||
| 318 | |||
| 319 | 25 | if ($typeAnnotation->resolveField) { |
|
| 320 | 24 | $typeConfiguration['resolveField'] = self::formatExpression($typeAnnotation->resolveField); |
|
| 321 | } |
||
| 322 | |||
| 323 | 25 | if ($typeAnnotation->builders && !empty($typeAnnotation->builders)) { |
|
|
|
|||
| 324 | $typeConfiguration['builders'] = array_map(function ($fieldsBuilderAnnotation) { |
||
| 325 | 24 | return ['builder' => $fieldsBuilderAnnotation->builder, 'builderConfig' => $fieldsBuilderAnnotation->builderConfig]; |
|
| 326 | 24 | }, $typeAnnotation->builders); |
|
| 327 | } |
||
| 328 | |||
| 329 | 25 | if ($typeAnnotation->isTypeOf) { |
|
| 330 | 24 | $typeConfiguration['isTypeOf'] = $typeAnnotation->isTypeOf; |
|
| 331 | } |
||
| 332 | |||
| 333 | 25 | $publicAnnotation = self::getFirstAnnotationMatching($graphClass->getAnnotations(), GQL\IsPublic::class); |
|
| 334 | 25 | if ($publicAnnotation) { |
|
| 335 | 24 | $typeConfiguration['fieldsDefaultPublic'] = self::formatExpression($publicAnnotation->value); |
|
| 336 | } |
||
| 337 | |||
| 338 | 25 | $accessAnnotation = self::getFirstAnnotationMatching($graphClass->getAnnotations(), GQL\Access::class); |
|
| 339 | 25 | if ($accessAnnotation) { |
|
| 340 | 24 | $typeConfiguration['fieldsDefaultAccess'] = self::formatExpression($accessAnnotation->value); |
|
| 341 | } |
||
| 342 | |||
| 343 | 25 | return ['type' => $typeAnnotation->isRelay ? 'relay-mutation-payload' : 'object', 'config' => $typeConfiguration]; |
|
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Create a GraphQL Interface type configuration from annotations on properties. |
||
| 348 | */ |
||
| 349 | 24 | private static function typeInterfaceAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\TypeInterface $interfaceAnnotation): array |
|
| 350 | { |
||
| 351 | 24 | $interfaceConfiguration = []; |
|
| 352 | |||
| 353 | 24 | $fieldsFromProperties = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended()); |
|
| 354 | 24 | $fieldsFromMethods = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $graphClass->getMethods()); |
|
| 355 | |||
| 356 | 24 | $interfaceConfiguration['fields'] = array_merge($fieldsFromProperties, $fieldsFromMethods); |
|
| 357 | 24 | $interfaceConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $interfaceConfiguration; |
|
| 358 | |||
| 359 | 24 | $interfaceConfiguration['resolveType'] = self::formatExpression($interfaceAnnotation->resolveType); |
|
| 360 | |||
| 361 | 24 | return ['type' => 'interface', 'config' => $interfaceConfiguration]; |
|
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Create a GraphQL Input type configuration from annotations on properties. |
||
| 366 | */ |
||
| 367 | 24 | private static function inputAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Input $inputAnnotation): array |
|
| 368 | { |
||
| 369 | 24 | $inputConfiguration = array_merge([ |
|
| 370 | 24 | 'fields' => self::getGraphQLInputFieldsFromAnnotations($graphClass, $graphClass->getPropertiesExtended()), |
|
| 371 | 24 | ], self::getDescriptionConfiguration($graphClass->getAnnotations())); |
|
| 372 | |||
| 373 | 24 | return ['type' => $inputAnnotation->isRelay ? 'relay-mutation-input' : 'input-object', 'config' => $inputConfiguration]; |
|
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get a GraphQL scalar configuration from given scalar annotation. |
||
| 378 | */ |
||
| 379 | 24 | private static function scalarAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Scalar $scalarAnnotation): array |
|
| 380 | { |
||
| 381 | 24 | $scalarConfiguration = []; |
|
| 382 | |||
| 383 | 24 | if ($scalarAnnotation->scalarType) { |
|
| 384 | 24 | $scalarConfiguration['scalarType'] = self::formatExpression($scalarAnnotation->scalarType); |
|
| 385 | } else { |
||
| 386 | $scalarConfiguration = [ |
||
| 387 | 24 | 'serialize' => [$graphClass->getName(), 'serialize'], |
|
| 388 | 24 | 'parseValue' => [$graphClass->getName(), 'parseValue'], |
|
| 389 | 24 | 'parseLiteral' => [$graphClass->getName(), 'parseLiteral'], |
|
| 390 | ]; |
||
| 391 | } |
||
| 392 | |||
| 393 | 24 | $scalarConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $scalarConfiguration; |
|
| 394 | |||
| 395 | 24 | return ['type' => 'custom-scalar', 'config' => $scalarConfiguration]; |
|
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get a GraphQL Enum configuration from given enum annotation. |
||
| 400 | */ |
||
| 401 | 24 | private static function enumAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Enum $enumAnnotation): array |
|
| 402 | { |
||
| 403 | 24 | $enumValues = $enumAnnotation->values ? $enumAnnotation->values : []; |
|
| 404 | |||
| 405 | 24 | $values = []; |
|
| 406 | |||
| 407 | 24 | foreach ($graphClass->getConstants() as $name => $value) { |
|
| 408 | $valueAnnotation = current(array_filter($enumValues, function ($enumValueAnnotation) use ($name) { |
||
| 409 | 24 | return $enumValueAnnotation->name == $name; |
|
| 410 | 24 | })); |
|
| 411 | 24 | $valueConfig = []; |
|
| 412 | 24 | $valueConfig['value'] = $value; |
|
| 413 | |||
| 414 | 24 | if ($valueAnnotation && $valueAnnotation->description) { |
|
| 415 | 24 | $valueConfig['description'] = $valueAnnotation->description; |
|
| 416 | } |
||
| 417 | |||
| 418 | 24 | if ($valueAnnotation && $valueAnnotation->deprecationReason) { |
|
| 419 | 24 | $valueConfig['deprecationReason'] = $valueAnnotation->deprecationReason; |
|
| 420 | } |
||
| 421 | |||
| 422 | 24 | $values[$name] = $valueConfig; |
|
| 423 | } |
||
| 424 | |||
| 425 | 24 | $enumConfiguration = ['values' => $values]; |
|
| 426 | 24 | $enumConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $enumConfiguration; |
|
| 427 | |||
| 428 | 24 | return ['type' => 'enum', 'config' => $enumConfiguration]; |
|
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get a GraphQL Union configuration from given union annotation. |
||
| 433 | */ |
||
| 434 | 24 | private static function unionAnnotationToGQLConfiguration(GraphClass $graphClass, GQL\Union $unionAnnotation): array |
|
| 435 | { |
||
| 436 | 24 | $unionConfiguration = []; |
|
| 437 | 24 | if (null !== $unionAnnotation->types) { |
|
| 438 | 24 | $unionConfiguration['types'] = $unionAnnotation->types; |
|
| 439 | } else { |
||
| 440 | $types = array_keys(self::searchClassesMapBy(function ($gqlType, $configuration) use ($graphClass) { |
||
| 441 | 24 | $typeClassName = $configuration['class']; |
|
| 442 | 24 | $typeMetadata = self::getGraphClass($typeClassName); |
|
| 443 | |||
| 444 | 24 | if ($graphClass->isInterface() && $typeMetadata->implementsInterface($graphClass->getName())) { |
|
| 445 | 24 | return true; |
|
| 446 | } |
||
| 447 | |||
| 448 | 24 | return $typeMetadata->isSubclassOf($graphClass->getName()); |
|
| 449 | 24 | }, self::GQL_TYPE)); |
|
| 450 | 24 | sort($types); |
|
| 451 | 24 | $unionConfiguration['types'] = $types; |
|
| 452 | } |
||
| 453 | |||
| 454 | 24 | $unionConfiguration = self::getDescriptionConfiguration($graphClass->getAnnotations()) + $unionConfiguration; |
|
| 455 | |||
| 456 | 24 | if ($unionAnnotation->resolveType) { |
|
| 457 | 24 | $unionConfiguration['resolveType'] = self::formatExpression($unionAnnotation->resolveType); |
|
| 458 | } else { |
||
| 459 | 24 | if ($graphClass->hasMethod('resolveType')) { |
|
| 460 | 24 | $method = $graphClass->getMethod('resolveType'); |
|
| 461 | 24 | if ($method->isStatic() && $method->isPublic()) { |
|
| 462 | 24 | $unionConfiguration['resolveType'] = self::formatExpression(sprintf("@=call('%s::%s', [service('overblog_graphql.type_resolver'), value], true)", self::formatNamespaceForExpression($graphClass->getName()), 'resolveType')); |
|
| 463 | } else { |
||
| 464 | 24 | throw new InvalidArgumentException(sprintf('The "resolveType()" method on class must be static and public. Or you must define a "resolveType" attribute on the @Union annotation.')); |
|
| 465 | } |
||
| 466 | } else { |
||
| 467 | 1 | throw new InvalidArgumentException(sprintf('The annotation @Union has no "resolveType" attribute and the related class has no "resolveType()" public static method. You need to define of them.')); |
|
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | 24 | return ['type' => 'union', 'config' => $unionConfiguration]; |
|
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param ReflectionMethod|ReflectionProperty $reflector |
||
| 476 | */ |
||
| 477 | 24 | private static function getTypeFieldConfigurationFromReflector(GraphClass $graphClass, Reflector $reflector, string $fieldAnnotationName = GQL\Field::class, string $currentValue = 'value'): array |
|
| 478 | { |
||
| 479 | 24 | $annotations = $graphClass->getAnnotations($reflector); |
|
| 480 | |||
| 481 | 24 | $fieldAnnotation = self::getFirstAnnotationMatching($annotations, $fieldAnnotationName); |
|
| 482 | 24 | $accessAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Access::class); |
|
| 483 | 24 | $publicAnnotation = self::getFirstAnnotationMatching($annotations, GQL\IsPublic::class); |
|
| 484 | |||
| 485 | 24 | $isMethod = $reflector instanceof ReflectionMethod; |
|
| 486 | |||
| 487 | 24 | if (!$fieldAnnotation) { |
|
| 488 | 24 | if ($accessAnnotation || $publicAnnotation) { |
|
| 489 | 1 | throw new InvalidArgumentException(sprintf('The annotations "@Access" and/or "@Visible" defined on "%s" are only usable in addition of annotation "@Field"', $reflector->getName())); |
|
| 490 | } |
||
| 491 | |||
| 492 | 24 | return []; |
|
| 493 | } |
||
| 494 | |||
| 495 | 24 | if ($isMethod && !$reflector->isPublic()) { |
|
| 496 | 1 | throw new InvalidArgumentException(sprintf('The Annotation "@Field" can only be applied to public method. The method "%s" is not public.', $reflector->getName())); |
|
| 497 | } |
||
| 498 | |||
| 499 | 24 | $fieldName = $reflector->getName(); |
|
| 500 | 24 | $fieldType = $fieldAnnotation->type; |
|
| 501 | 24 | $fieldConfiguration = []; |
|
| 502 | 24 | if ($fieldType) { |
|
| 503 | 24 | $fieldConfiguration['type'] = $fieldType; |
|
| 504 | } |
||
| 505 | |||
| 506 | 24 | $fieldConfiguration = self::getDescriptionConfiguration($annotations, true) + $fieldConfiguration; |
|
| 507 | |||
| 508 | 24 | $args = self::getArgs($fieldAnnotation->args, $isMethod && !$fieldAnnotation->argsBuilder ? $reflector : null); |
|
| 509 | |||
| 510 | 24 | if (!empty($args)) { |
|
| 511 | 24 | $fieldConfiguration['args'] = $args; |
|
| 512 | } |
||
| 513 | |||
| 514 | 24 | $fieldName = $fieldAnnotation->name ?: $fieldName; |
|
| 515 | |||
| 516 | 24 | if ($fieldAnnotation->resolve) { |
|
| 517 | 24 | $fieldConfiguration['resolve'] = self::formatExpression($fieldAnnotation->resolve); |
|
| 518 | } else { |
||
| 519 | 24 | if ($isMethod) { |
|
| 520 | 24 | $fieldConfiguration['resolve'] = self::formatExpression(sprintf('call(%s.%s, %s)', $currentValue, $reflector->getName(), self::formatArgsForExpression($args))); |
|
| 521 | } else { |
||
| 522 | 24 | if ($fieldName !== $reflector->getName() || 'value' !== $currentValue) { |
|
| 523 | $fieldConfiguration['resolve'] = self::formatExpression(sprintf('%s.%s', $currentValue, $reflector->getName())); |
||
| 524 | } |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 528 | 24 | if ($fieldAnnotation->argsBuilder) { |
|
| 529 | 24 | if (is_string($fieldAnnotation->argsBuilder)) { |
|
| 530 | $fieldConfiguration['argsBuilder'] = $fieldAnnotation->argsBuilder; |
||
| 531 | 24 | } elseif (is_array($fieldAnnotation->argsBuilder)) { |
|
| 532 | 24 | list($builder, $builderConfig) = $fieldAnnotation->argsBuilder; |
|
| 533 | 24 | $fieldConfiguration['argsBuilder'] = ['builder' => $builder, 'config' => $builderConfig]; |
|
| 534 | } else { |
||
| 535 | 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())); |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | 24 | if ($fieldAnnotation->fieldBuilder) { |
|
| 540 | 24 | if (is_string($fieldAnnotation->fieldBuilder)) { |
|
| 541 | $fieldConfiguration['builder'] = $fieldAnnotation->fieldBuilder; |
||
| 542 | 24 | } elseif (is_array($fieldAnnotation->fieldBuilder)) { |
|
| 543 | 24 | list($builder, $builderConfig) = $fieldAnnotation->fieldBuilder; |
|
| 544 | 24 | $fieldConfiguration['builder'] = $builder; |
|
| 545 | 24 | $fieldConfiguration['builderConfig'] = $builderConfig ?: []; |
|
| 546 | } else { |
||
| 547 | 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())); |
|
| 548 | } |
||
| 549 | } else { |
||
| 550 | 24 | if (!$fieldType) { |
|
| 551 | 24 | if ($isMethod) { |
|
| 552 | /** var ReflectionMethod $reflector */ |
||
| 553 | 24 | if ($reflector->hasReturnType()) { |
|
| 554 | try { |
||
| 555 | 24 | $fieldConfiguration['type'] = self::resolveGraphQLTypeFromReflectionType($reflector->getReturnType(), self::VALID_OUTPUT_TYPES); |
|
| 556 | } catch (Exception $e) { |
||
| 557 | 24 | throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on method "%s" and cannot be auto-guessed from type hint "%s"', $fieldAnnotationName, $reflector->getName(), (string) $reflector->getReturnType())); |
|
| 558 | } |
||
| 559 | } else { |
||
| 560 | 24 | throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on method "%s" and cannot be auto-guessed as there is not return type hint.', $fieldAnnotationName, $reflector->getName())); |
|
| 561 | } |
||
| 562 | } else { |
||
| 563 | try { |
||
| 564 | 24 | $fieldConfiguration['type'] = self::guessType($graphClass, $annotations); |
|
| 565 | 2 | } catch (Exception $e) { |
|
| 566 | 2 | throw new InvalidArgumentException(sprintf('The attribute "type" on "@%s" defined on "%s" is required and cannot be auto-guessed : %s.', $fieldAnnotationName, $reflector->getName(), $e->getMessage())); |
|
| 567 | } |
||
| 568 | } |
||
| 569 | } |
||
| 570 | } |
||
| 571 | |||
| 572 | 24 | if ($accessAnnotation) { |
|
| 573 | 24 | $fieldConfiguration['access'] = self::formatExpression($accessAnnotation->value); |
|
| 574 | } |
||
| 575 | |||
| 576 | 24 | if ($publicAnnotation) { |
|
| 577 | 24 | $fieldConfiguration['public'] = self::formatExpression($publicAnnotation->value); |
|
| 578 | } |
||
| 579 | |||
| 580 | 24 | if ($fieldAnnotation->complexity) { |
|
| 581 | 24 | $fieldConfiguration['complexity'] = self::formatExpression($fieldAnnotation->complexity); |
|
| 582 | } |
||
| 583 | |||
| 584 | 24 | return [$fieldName => $fieldConfiguration]; |
|
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Create GraphQL input fields configuration based on annotations. |
||
| 589 | * |
||
| 590 | * @param ReflectionProperty[] $reflectors |
||
| 591 | */ |
||
| 592 | 24 | private static function getGraphQLInputFieldsFromAnnotations(GraphClass $graphClass, array $reflectors): array |
|
| 593 | { |
||
| 594 | 24 | $fields = []; |
|
| 595 | |||
| 596 | 24 | foreach ($reflectors as $reflector) { |
|
| 597 | 24 | $annotations = $graphClass->getAnnotations($reflector); |
|
| 598 | 24 | $fieldAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Field::class); |
|
| 599 | |||
| 600 | // Ignore field with resolver when the type is an Input |
||
| 601 | 24 | if ($fieldAnnotation->resolve) { |
|
| 602 | return []; |
||
| 603 | } |
||
| 604 | |||
| 605 | 24 | $fieldName = $reflector->getName(); |
|
| 606 | 24 | $fieldType = $fieldAnnotation->type; |
|
| 607 | 24 | $fieldConfiguration = []; |
|
| 608 | 24 | if ($fieldType) { |
|
| 609 | 24 | $resolvedType = self::resolveClassFromType($fieldType); |
|
| 610 | // We found a type but it is not allowed |
||
| 611 | 24 | if (null !== $resolvedType && !in_array($resolvedType['type'], self::VALID_INPUT_TYPES)) { |
|
| 612 | 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'])); |
||
| 613 | } |
||
| 614 | |||
| 615 | 24 | $fieldConfiguration['type'] = $fieldType; |
|
| 616 | } |
||
| 617 | |||
| 618 | 24 | $fieldConfiguration = array_merge(self::getDescriptionConfiguration($annotations, true), $fieldConfiguration); |
|
| 619 | 24 | $fields[$fieldName] = $fieldConfiguration; |
|
| 620 | } |
||
| 621 | |||
| 622 | 24 | return $fields; |
|
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Create GraphQL type fields configuration based on annotations. |
||
| 627 | * |
||
| 628 | * @param ReflectionProperty[]|ReflectionMethod[] $reflectors |
||
| 629 | */ |
||
| 630 | 25 | private static function getGraphQLTypeFieldsFromAnnotations(GraphClass $graphClass, array $reflectors, string $fieldAnnotationName = GQL\Field::class, string $currentValue = 'value'): array |
|
| 631 | { |
||
| 632 | 25 | $fields = []; |
|
| 633 | |||
| 634 | 25 | foreach ($reflectors as $reflector) { |
|
| 635 | 24 | $fields = array_merge($fields, self::getTypeFieldConfigurationFromReflector($graphClass, $reflector, $fieldAnnotationName, $currentValue)); |
|
| 636 | } |
||
| 637 | |||
| 638 | 25 | return $fields; |
|
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Return fields config from Provider methods. |
||
| 643 | * Loop through configured provider and extract fields targeting the targetType. |
||
| 644 | */ |
||
| 645 | 25 | private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, string $expectedAnnotation, string $targetType, bool $isDefaultTarget = false): array |
|
| 646 | { |
||
| 647 | 25 | $fields = []; |
|
| 648 | 25 | foreach (self::$providers as ['metadata' => $providerMetadata, 'annotation' => $providerAnnotation]) { |
|
| 649 | 24 | $defaultAccessAnnotation = self::getFirstAnnotationMatching($providerMetadata->getAnnotations(), GQL\Access::class); |
|
| 650 | 24 | $defaultIsPublicAnnotation = self::getFirstAnnotationMatching($providerMetadata->getAnnotations(), GQL\IsPublic::class); |
|
| 651 | |||
| 652 | 24 | $defaultAccess = $defaultAccessAnnotation ? self::formatExpression($defaultAccessAnnotation->value) : false; |
|
| 653 | 24 | $defaultIsPublic = $defaultIsPublicAnnotation ? self::formatExpression($defaultIsPublicAnnotation->value) : false; |
|
| 654 | |||
| 655 | 24 | $methods = []; |
|
| 656 | // First found the methods matching the targeted type |
||
| 657 | 24 | foreach ($providerMetadata->getMethods() as $method) { |
|
| 658 | 24 | $annotations = $providerMetadata->getAnnotations($method); |
|
| 659 | |||
| 660 | 24 | $annotation = self::getFirstAnnotationMatching($annotations, [GQL\Mutation::class, GQL\Query::class]); |
|
| 661 | 24 | if (!$annotation) { |
|
| 662 | continue; |
||
| 663 | } |
||
| 664 | |||
| 665 | 24 | $annotationTarget = $annotation->targetType; |
|
| 666 | 24 | if (!$annotationTarget && $isDefaultTarget) { |
|
| 667 | 24 | $annotationTarget = $targetType; |
|
| 668 | 24 | if (!($annotation instanceof $expectedAnnotation)) { |
|
| 669 | 24 | continue; |
|
| 670 | } |
||
| 671 | } |
||
| 672 | |||
| 673 | 24 | if ($annotationTarget !== $targetType) { |
|
| 674 | 24 | continue; |
|
| 675 | } |
||
| 676 | |||
| 677 | 24 | if (!($annotation instanceof $expectedAnnotation)) { |
|
| 678 | 2 | if (GQL\Mutation::class == $expectedAnnotation) { |
|
| 679 | 1 | $message = sprintf('The provider "%s" try to add a query field on type "%s" (through @Query on method "%s") but "%s" is a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType); |
|
| 680 | } else { |
||
| 681 | 1 | $message = sprintf('The provider "%s" try to add a mutation on type "%s" (through @Mutation on method "%s") but "%s" is not a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType); |
|
| 682 | } |
||
| 683 | |||
| 684 | 2 | throw new InvalidArgumentException($message); |
|
| 685 | } |
||
| 686 | 24 | $methods[$method->getName()] = $method; |
|
| 687 | } |
||
| 688 | |||
| 689 | 24 | $currentValue = sprintf("service('%s')", self::formatNamespaceForExpression($providerMetadata->getName())); |
|
| 690 | 24 | $providerFields = self::getGraphQLTypeFieldsFromAnnotations($graphClass, $methods, $expectedAnnotation, $currentValue); |
|
| 691 | 24 | foreach ($providerFields as $fieldName => $fieldConfig) { |
|
| 692 | 24 | if ($providerAnnotation->prefix) { |
|
| 693 | 24 | $fieldName = sprintf('%s%s', $providerAnnotation->prefix, $fieldName); |
|
| 694 | } |
||
| 695 | |||
| 696 | 24 | if ($defaultAccess && !isset($fieldConfig['access'])) { |
|
| 697 | 24 | $fieldConfig['access'] = $defaultAccess; |
|
| 698 | } |
||
| 699 | |||
| 700 | 24 | if ($defaultIsPublic && !isset($fieldConfig['public'])) { |
|
| 701 | 24 | $fieldConfig['public'] = $defaultIsPublic; |
|
| 702 | } |
||
| 703 | |||
| 704 | 24 | $fields[$fieldName] = $fieldConfig; |
|
| 705 | } |
||
| 706 | } |
||
| 707 | |||
| 708 | 25 | return $fields; |
|
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Get the config for description & deprecation reason. |
||
| 713 | */ |
||
| 714 | 25 | private static function getDescriptionConfiguration(array $annotations, bool $withDeprecation = false): array |
|
| 715 | { |
||
| 716 | 25 | $config = []; |
|
| 717 | 25 | $descriptionAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Description::class); |
|
| 718 | 25 | if ($descriptionAnnotation) { |
|
| 719 | 24 | $config['description'] = $descriptionAnnotation->value; |
|
| 720 | } |
||
| 721 | |||
| 722 | 25 | if ($withDeprecation) { |
|
| 723 | 24 | $deprecatedAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Deprecated::class); |
|
| 724 | 24 | if ($deprecatedAnnotation) { |
|
| 725 | 24 | $config['deprecationReason'] = $deprecatedAnnotation->value; |
|
| 726 | } |
||
| 727 | } |
||
| 728 | |||
| 729 | 25 | return $config; |
|
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Get args config from an array of @Arg annotation or by auto-guessing if a method is provided. |
||
| 734 | */ |
||
| 735 | 24 | private static function getArgs(?array $args, ReflectionMethod $method = null): array |
|
| 736 | { |
||
| 737 | 24 | $config = []; |
|
| 738 | 24 | if (!empty($args)) { |
|
| 739 | 24 | foreach ($args as $arg) { |
|
| 740 | 24 | $config[$arg->name] = ['type' => $arg->type] |
|
| 741 | 24 | + ($arg->description ? ['description' => $arg->description] : []) |
|
| 742 | 24 | + ($arg->default ? ['defaultValue' => $arg->default] : []); |
|
| 743 | } |
||
| 744 | 24 | } elseif ($method) { |
|
| 745 | 24 | $config = self::guessArgs($method); |
|
| 746 | } |
||
| 747 | |||
| 748 | 24 | return $config; |
|
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Format an array of args to a list of arguments in an expression. |
||
| 753 | */ |
||
| 754 | 24 | private static function formatArgsForExpression(array $args): string |
|
| 755 | { |
||
| 756 | 24 | $mapping = []; |
|
| 757 | 24 | foreach ($args as $name => $config) { |
|
| 758 | 24 | $mapping[] = sprintf('%s: "%s"', $name, $config['type']); |
|
| 759 | } |
||
| 760 | |||
| 761 | 24 | return sprintf('arguments({%s}, args)', implode(', ', $mapping)); |
|
| 762 | } |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Format a namespace to be used in an expression (double escape). |
||
| 766 | */ |
||
| 767 | 24 | private static function formatNamespaceForExpression(string $namespace): string |
|
| 768 | { |
||
| 769 | 24 | return str_replace('\\', '\\\\', $namespace); |
|
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Get the first annotation matching given class. |
||
| 774 | * |
||
| 775 | * @param string|array $annotationClass |
||
| 776 | * |
||
| 777 | * @return mixed |
||
| 778 | */ |
||
| 779 | 25 | private static function getFirstAnnotationMatching(array $annotations, $annotationClass) |
|
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Format an expression (ie. add "@=" if not set). |
||
| 798 | */ |
||
| 799 | 24 | private static function formatExpression(string $expression): string |
|
| 800 | { |
||
| 801 | 24 | return '@=' === substr($expression, 0, 2) ? $expression : sprintf('@=%s', $expression); |
|
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Suffix a name if it is not already. |
||
| 806 | */ |
||
| 807 | 24 | private static function suffixName(string $name, string $suffix): string |
|
| 808 | { |
||
| 809 | 24 | return substr($name, -strlen($suffix)) === $suffix ? $name : sprintf('%s%s', $name, $suffix); |
|
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Try to guess a field type base on his annotations. |
||
| 814 | * |
||
| 815 | * @throws RuntimeException |
||
| 816 | */ |
||
| 817 | 24 | private static function guessType(GraphClass $graphClass, array $annotations): string |
|
| 818 | { |
||
| 819 | 24 | $columnAnnotation = self::getFirstAnnotationMatching($annotations, Column::class); |
|
| 820 | 24 | if ($columnAnnotation) { |
|
| 821 | 24 | $type = self::resolveTypeFromDoctrineType($columnAnnotation->type); |
|
| 822 | 24 | $nullable = $columnAnnotation->nullable; |
|
| 823 | 24 | if ($type) { |
|
| 824 | 24 | return $nullable ? $type : sprintf('%s!', $type); |
|
| 825 | } else { |
||
| 826 | 1 | throw new RuntimeException(sprintf('Unable to auto-guess GraphQL type from Doctrine type "%s"', $columnAnnotation->type)); |
|
| 827 | } |
||
| 828 | } |
||
| 829 | |||
| 830 | $associationAnnotations = [ |
||
| 831 | 24 | OneToMany::class => true, |
|
| 832 | OneToOne::class => false, |
||
| 833 | ManyToMany::class => true, |
||
| 834 | ManyToOne::class => false, |
||
| 835 | ]; |
||
| 836 | |||
| 837 | 24 | $associationAnnotation = self::getFirstAnnotationMatching($annotations, array_keys($associationAnnotations)); |
|
| 838 | 24 | if ($associationAnnotation) { |
|
| 839 | 24 | $target = self::fullyQualifiedClassName($associationAnnotation->targetEntity, $graphClass->getNamespaceName()); |
|
| 840 | 24 | $type = self::resolveTypeFromClass($target, ['type']); |
|
| 841 | |||
| 842 | 24 | if ($type) { |
|
| 843 | 24 | $isMultiple = $associationAnnotations[get_class($associationAnnotation)]; |
|
| 844 | 24 | if ($isMultiple) { |
|
| 845 | 24 | return sprintf('[%s]!', $type); |
|
| 846 | } else { |
||
| 847 | 24 | $isNullable = false; |
|
| 848 | 24 | $joinColumn = self::getFirstAnnotationMatching($annotations, JoinColumn::class); |
|
| 849 | 24 | if ($joinColumn) { |
|
| 850 | 24 | $isNullable = $joinColumn->nullable; |
|
| 851 | } |
||
| 852 | |||
| 853 | 24 | return sprintf('%s%s', $type, $isNullable ? '' : '!'); |
|
| 854 | } |
||
| 855 | } else { |
||
| 856 | 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)); |
|
| 857 | } |
||
| 858 | } |
||
| 859 | |||
| 860 | throw new InvalidArgumentException(sprintf('No Doctrine ORM annotation found.')); |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Resolve a FQN from classname and namespace. |
||
| 865 | * |
||
| 866 | * @internal |
||
| 867 | */ |
||
| 868 | 24 | public static function fullyQualifiedClassName(string $className, string $namespace): string |
|
| 875 | } |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Resolve a GraphQLType from a doctrine type. |
||
| 879 | */ |
||
| 880 | 24 | private static function resolveTypeFromDoctrineType(string $doctrineType): ?string |
|
| 881 | { |
||
| 882 | 24 | if (isset(self::$doctrineMapping[$doctrineType])) { |
|
| 883 | 24 | return self::$doctrineMapping[$doctrineType]; |
|
| 884 | } |
||
| 885 | |||
| 886 | switch ($doctrineType) { |
||
| 887 | 24 | case 'integer': |
|
| 888 | 24 | case 'smallint': |
|
| 889 | 24 | case 'bigint': |
|
| 890 | 24 | return 'Int'; |
|
| 891 | 24 | case 'string': |
|
| 892 | 1 | case 'text': |
|
| 893 | 24 | return 'String'; |
|
| 894 | 1 | case 'bool': |
|
| 895 | 1 | case 'boolean': |
|
| 896 | return 'Boolean'; |
||
| 897 | 1 | case 'float': |
|
| 898 | 1 | case 'decimal': |
|
| 899 | return 'Float'; |
||
| 900 | default: |
||
| 901 | 1 | return null; |
|
| 902 | } |
||
| 903 | } |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Transform a method arguments from reflection to a list of GraphQL argument. |
||
| 907 | */ |
||
| 908 | 24 | private static function guessArgs(ReflectionMethod $method): array |
|
| 909 | { |
||
| 910 | 24 | $arguments = []; |
|
| 911 | 24 | foreach ($method->getParameters() as $index => $parameter) { |
|
| 912 | 24 | if (!$parameter->hasType()) { |
|
| 913 | 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())); |
|
| 914 | } |
||
| 915 | |||
| 916 | try { |
||
| 917 | // @phpstan-ignore-next-line |
||
| 918 | 24 | $gqlType = self::resolveGraphQLTypeFromReflectionType($parameter->getType(), self::VALID_INPUT_TYPES, $parameter->isDefaultValueAvailable()); |
|
| 919 | } catch (Exception $e) { |
||
| 920 | throw new InvalidArgumentException(sprintf('Argument n°%s "$%s" on method "%s" cannot be auto-guessed : %s".', $index + 1, $parameter->getName(), $method->getName(), $e->getMessage())); |
||
| 921 | } |
||
| 922 | |||
| 923 | 24 | $argumentConfig = []; |
|
| 924 | 24 | if ($parameter->isDefaultValueAvailable()) { |
|
| 925 | 24 | $argumentConfig['defaultValue'] = $parameter->getDefaultValue(); |
|
| 926 | } |
||
| 927 | |||
| 928 | 24 | $argumentConfig['type'] = $gqlType; |
|
| 929 | |||
| 930 | 24 | $arguments[$parameter->getName()] = $argumentConfig; |
|
| 931 | } |
||
| 932 | |||
| 933 | 24 | return $arguments; |
|
| 934 | } |
||
| 935 | |||
| 936 | 24 | private static function resolveGraphQLTypeFromReflectionType(ReflectionNamedType $type, array $filterGraphQLTypes = [], bool $isOptional = false): string |
|
| 937 | { |
||
| 938 | 24 | $sType = $type->getName(); |
|
| 939 | 24 | if ($type->isBuiltin()) { |
|
| 940 | 24 | $gqlType = self::resolveTypeFromPhpType($sType); |
|
| 941 | 24 | if (null === $gqlType) { |
|
| 942 | 24 | throw new RuntimeException(sprintf('No corresponding GraphQL type found for builtin type "%s"', $sType)); |
|
| 943 | } |
||
| 944 | } else { |
||
| 945 | 24 | $gqlType = self::resolveTypeFromClass($sType, $filterGraphQLTypes); |
|
| 946 | 24 | if (null === $gqlType) { |
|
| 947 | throw new RuntimeException(sprintf('No corresponding GraphQL %s found for class "%s"', $filterGraphQLTypes ? implode(',', $filterGraphQLTypes) : 'object', $sType)); |
||
| 948 | } |
||
| 949 | } |
||
| 950 | |||
| 951 | 24 | return sprintf('%s%s', $gqlType, ($type->allowsNull() || $isOptional) ? '' : '!'); |
|
| 952 | } |
||
| 953 | |||
| 954 | /** |
||
| 955 | * Resolve a GraphQL Type from a class name. |
||
| 956 | */ |
||
| 957 | 24 | private static function resolveTypeFromClass(string $className, array $wantedTypes = []): ?string |
|
| 958 | { |
||
| 959 | 24 | foreach (self::$classesMap as $gqlType => $config) { |
|
| 960 | 24 | if ($config['class'] === $className) { |
|
| 961 | 24 | if (in_array($config['type'], $wantedTypes)) { |
|
| 962 | 24 | return $gqlType; |
|
| 963 | } |
||
| 964 | } |
||
| 965 | } |
||
| 966 | |||
| 967 | 1 | return null; |
|
| 968 | } |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Resolve a PHP class from a GraphQL type. |
||
| 972 | * |
||
| 973 | * @return string|array|null |
||
| 974 | */ |
||
| 975 | 24 | private static function resolveClassFromType(string $type) |
|
| 976 | { |
||
| 977 | 24 | return self::$classesMap[$type] ?? null; |
|
| 978 | } |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Search the classes map for class by predicate. |
||
| 982 | * |
||
| 983 | * @return array |
||
| 984 | */ |
||
| 985 | 25 | private static function searchClassesMapBy(callable $predicate, string $type = null) |
|
| 986 | { |
||
| 987 | 25 | $classNames = []; |
|
| 988 | 25 | foreach (self::$classesMap as $gqlType => $config) { |
|
| 989 | 25 | if ($type && $config['type'] !== $type) { |
|
| 990 | 25 | continue; |
|
| 991 | } |
||
| 992 | |||
| 993 | 24 | if ($predicate($gqlType, $config)) { |
|
| 994 | 24 | $classNames[$gqlType] = $config; |
|
| 995 | } |
||
| 996 | } |
||
| 997 | |||
| 998 | 25 | return $classNames; |
|
| 999 | } |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Convert a PHP Builtin type to a GraphQL type. |
||
| 1003 | */ |
||
| 1004 | 24 | private static function resolveTypeFromPhpType(string $phpType): ?string |
|
| 1020 | } |
||
| 1021 | } |
||
| 1022 | } |
||
| 1023 |
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.