| Conditions | 14 |
| Paths | 82 |
| Total Lines | 80 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 39 | public function build(DocumentNode $documentNode, array $options = []): SchemaInterface |
||
| 40 | { |
||
| 41 | $schemaDefinition = null; |
||
| 42 | $typeDefinitions = []; |
||
| 43 | $nodeMap = []; |
||
| 44 | $directiveDefinitions = []; |
||
| 45 | |||
| 46 | foreach ($documentNode->getDefinitions() as $definition) { |
||
| 47 | if ($definition instanceof SchemaDefinitionNode) { |
||
| 48 | if ($schemaDefinition) { |
||
| 49 | throw new \Exception('Must provide only one schema definition.'); |
||
| 50 | } |
||
| 51 | $schemaDefinition = $definition; |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($definition instanceof TypeDefinitionNodeInterface) { |
||
| 56 | $typeName = $definition->getNameValue(); |
||
| 57 | if (isset($nodeMap[$typeName])) { |
||
| 58 | throw new \Exception(sprintf('Type "%s" was defined more than once.', $typeName)); |
||
| 59 | } |
||
| 60 | $typeDefinitions[] = $definition; |
||
| 61 | $nodeMap[$typeName] = $definition; |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($definition instanceof DirectiveDefinitionNode) { |
||
| 66 | $directiveDefinitions[] = $definition; |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | $operationTypes = null !== $schemaDefinition ? getOperationTypes($schemaDefinition, $nodeMap) : [ |
||
| 72 | 'query' => $nodeMap['Query'] ?? null, |
||
| 73 | 'mutation' => $nodeMap['Mutation'] ?? null, |
||
| 74 | 'subscription' => $nodeMap['Subscription'] ?? null, |
||
| 75 | ]; |
||
| 76 | |||
| 77 | $this->definitionBuilder->setTypeDefinitionMap($nodeMap); |
||
| 78 | |||
| 79 | $types = array_map(function (TypeDefinitionNodeInterface $definition) { |
||
| 80 | return $this->definitionBuilder->buildType($definition); |
||
| 81 | }, $typeDefinitions); |
||
| 82 | |||
| 83 | $directives = array_map(function (DirectiveDefinitionNode $definition) { |
||
| 84 | return $this->definitionBuilder->buildDirective($definition); |
||
| 85 | }, $directiveDefinitions); |
||
| 86 | |||
| 87 | if (!arraySome($directives, function (DirectiveInterface $directive) { |
||
| 88 | return $directive->getName() === 'skip'; |
||
| 89 | })) { |
||
| 90 | $directives[] = GraphQLSkipDirective(); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!arraySome($directives, function (DirectiveInterface $directive) { |
||
| 94 | return $directive->getName() === 'include'; |
||
| 95 | })) { |
||
| 96 | $directives[] = GraphQLIncludeDirective(); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!arraySome($directives, function (DirectiveInterface $directive) { |
||
| 100 | return $directive->getName() === 'deprecated'; |
||
| 101 | })) { |
||
| 102 | $directives[] = GraphQLDeprecatedDirective(); |
||
| 103 | } |
||
| 104 | |||
| 105 | return GraphQLSchema([ |
||
| 106 | 'query' => isset($operationTypes['query']) |
||
| 107 | ? $this->definitionBuilder->buildType($operationTypes['query']) |
||
| 108 | : null, |
||
| 109 | 'mutation' => isset($operationTypes['mutation']) |
||
| 110 | ? $this->definitionBuilder->buildType($operationTypes['mutation']) |
||
| 111 | : null, |
||
| 112 | 'subscription' => isset($operationTypes['subscription']) |
||
| 113 | ? $this->definitionBuilder->buildType($operationTypes['subscription']) |
||
| 114 | : null, |
||
| 115 | 'types' => $types, |
||
| 116 | 'directives' => $directives, |
||
| 117 | 'astNode' => $schemaDefinition, |
||
| 118 | 'assumeValid' => $options['assumeValid'] ?? false, |
||
| 119 | ]); |
||
| 151 |