Total Complexity | 95 |
Total Lines | 768 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like NodeBuilder 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 NodeBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | class NodeBuilder implements NodeBuilderInterface |
||
52 | { |
||
53 | /** |
||
54 | * @param array $ast |
||
55 | * @return NodeInterface |
||
56 | * @throws LanguageException |
||
57 | */ |
||
58 | public function build(array $ast): NodeInterface |
||
59 | { |
||
60 | if (!isset($ast['kind'])) { |
||
61 | throw new LanguageException(sprintf('Nodes must specify a kind, got %s', json_encode($ast))); |
||
62 | } |
||
63 | |||
64 | ['kind' => $kind] = $ast; |
||
65 | |||
66 | switch ($kind) { |
||
67 | case NodeKindEnum::ARGUMENT: |
||
68 | return $this->buildArgument($ast); |
||
69 | case NodeKindEnum::BOOLEAN: |
||
70 | return $this->buildBoolean($ast); |
||
71 | case NodeKindEnum::DIRECTIVE_DEFINITION: |
||
72 | return $this->buildDirectiveDefinition($ast); |
||
73 | case NodeKindEnum::DIRECTIVE: |
||
74 | return $this->buildDirective($ast); |
||
75 | case NodeKindEnum::DOCUMENT: |
||
76 | return $this->buildDocument($ast); |
||
77 | case NodeKindEnum::ENUM: |
||
78 | return $this->buildEnum($ast); |
||
79 | case NodeKindEnum::ENUM_TYPE_DEFINITION: |
||
80 | return $this->buildEnumTypeDefinition($ast); |
||
81 | case NodeKindEnum::ENUM_TYPE_EXTENSION: |
||
82 | return $this->buildEnumTypeExtension($ast); |
||
83 | case NodeKindEnum::ENUM_VALUE_DEFINITION: |
||
84 | return $this->buildEnumValueDefinition($ast); |
||
85 | case NodeKindEnum::FIELD: |
||
86 | return $this->buildField($ast); |
||
87 | case NodeKindEnum::FIELD_DEFINITION: |
||
88 | return $this->buildFieldDefinition($ast); |
||
89 | case NodeKindEnum::FLOAT: |
||
90 | return $this->buildFloat($ast); |
||
91 | case NodeKindEnum::FRAGMENT_DEFINITION: |
||
92 | return $this->buildFragmentDefinition($ast); |
||
93 | case NodeKindEnum::FRAGMENT_SPREAD: |
||
94 | return $this->buildFragmentSpread($ast); |
||
95 | case NodeKindEnum::INLINE_FRAGMENT: |
||
96 | return $this->buildInlineFragment($ast); |
||
97 | case NodeKindEnum::INPUT_OBJECT_TYPE_DEFINITION: |
||
98 | return $this->buildInputObjectTypeDefinition($ast); |
||
99 | case NodeKindEnum::INPUT_OBJECT_TYPE_EXTENSION: |
||
100 | return $this->buildInputObjectTypeExtension($ast); |
||
101 | case NodeKindEnum::INPUT_VALUE_DEFINITION: |
||
102 | return $this->buildInputValueDefinition($ast); |
||
103 | case NodeKindEnum::INTERFACE_TYPE_DEFINITION: |
||
104 | return $this->buildInterfaceTypeDefinition($ast); |
||
105 | case NodeKindEnum::INTERFACE_TYPE_EXTENSION: |
||
106 | return $this->buildInterfaceTypeExtension($ast); |
||
107 | case NodeKindEnum::INT: |
||
108 | return $this->buildInt($ast); |
||
109 | case NodeKindEnum::LIST_TYPE: |
||
110 | return $this->buildListType($ast); |
||
111 | case NodeKindEnum::LIST: |
||
112 | return $this->buildList($ast); |
||
113 | case NodeKindEnum::NAMED_TYPE: |
||
114 | return $this->buildNamedType($ast); |
||
115 | case NodeKindEnum::NAME: |
||
116 | return $this->buildName($ast); |
||
117 | case NodeKindEnum::NON_NULL_TYPE: |
||
118 | return $this->buildNonNullType($ast); |
||
119 | case NodeKindEnum::NULL: |
||
120 | return $this->buildNull($ast); |
||
121 | case NodeKindEnum::OBJECT_FIELD: |
||
122 | return $this->buildObjectField($ast); |
||
123 | case NodeKindEnum::OBJECT_TYPE_DEFINITION: |
||
124 | return $this->buildObjectTypeDefinition($ast); |
||
125 | case NodeKindEnum::OBJECT_TYPE_EXTENSION: |
||
126 | return $this->buildObjectTypeExtension($ast); |
||
127 | case NodeKindEnum::OBJECT: |
||
128 | return $this->buildObject($ast); |
||
129 | case NodeKindEnum::OPERATION_DEFINITION: |
||
130 | return $this->buildOperationDefinition($ast); |
||
131 | case NodeKindEnum::OPERATION_TYPE_DEFINITION: |
||
132 | return $this->buildOperationTypeDefinition($ast); |
||
133 | case NodeKindEnum::SCALAR_TYPE_DEFINITION: |
||
134 | return $this->buildScalarTypeDefinition($ast); |
||
135 | case NodeKindEnum::SCALAR_TYPE_EXTENSION: |
||
136 | return $this->buildScalarTypeExtension($ast); |
||
137 | case NodeKindEnum::SCHEMA_DEFINITION: |
||
138 | return $this->buildSchemaDefinition($ast); |
||
139 | case NodeKindEnum::SELECTION_SET: |
||
140 | return $this->buildSelectionSet($ast); |
||
141 | case NodeKindEnum::STRING: |
||
142 | return $this->buildString($ast); |
||
143 | case NodeKindEnum::UNION_TYPE_DEFINITION: |
||
144 | return $this->buildUnionTypeDefinition($ast); |
||
145 | case NodeKindEnum::UNION_TYPE_EXTENSION: |
||
146 | return $this->buildUnionTypeExtension($ast); |
||
147 | case NodeKindEnum::VARIABLE_DEFINITION: |
||
148 | return $this->buildVariableDefinition($ast); |
||
149 | case NodeKindEnum::VARIABLE: |
||
150 | return $this->buildVariable($ast); |
||
151 | } |
||
152 | |||
153 | throw new LanguageException(sprintf('Node of kind "%s" not supported.', $kind)); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param array $ast |
||
158 | * @return ArgumentNode |
||
159 | * @throws LanguageException |
||
160 | */ |
||
161 | protected function buildArgument(array $ast): ArgumentNode |
||
162 | { |
||
163 | return new ArgumentNode([ |
||
164 | 'name' => $this->buildNode($ast, 'name'), |
||
165 | 'value' => $this->buildNode($ast, 'value'), |
||
166 | 'location' => $this->createLocation($ast), |
||
167 | ]); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param array $ast |
||
172 | * @return BooleanValueNode |
||
173 | */ |
||
174 | protected function buildBoolean(array $ast): BooleanValueNode |
||
175 | { |
||
176 | return new BooleanValueNode([ |
||
177 | 'value' => $this->getValue($ast, 'value'), |
||
178 | 'location' => $this->createLocation($ast), |
||
179 | ]); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param array $ast |
||
184 | * @return DirectiveDefinitionNode |
||
185 | * @throws LanguageException |
||
186 | */ |
||
187 | protected function buildDirectiveDefinition(array $ast): DirectiveDefinitionNode |
||
188 | { |
||
189 | return new DirectiveDefinitionNode([ |
||
190 | 'description' => $this->buildNode($ast, 'description'), |
||
191 | 'name' => $this->buildNode($ast, 'name'), |
||
192 | 'arguments' => $this->buildNodes($ast, 'arguments'), |
||
193 | 'locations' => $this->buildNodes($ast, 'locations'), |
||
194 | 'location' => $this->createLocation($ast), |
||
195 | ]); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param array $ast |
||
200 | * @return DirectiveNode |
||
201 | * @throws LanguageException |
||
202 | */ |
||
203 | protected function buildDirective(array $ast): DirectiveNode |
||
204 | { |
||
205 | return new DirectiveNode([ |
||
206 | 'name' => $this->buildNode($ast, 'name'), |
||
207 | 'arguments' => $this->buildNodes($ast, 'arguments'), |
||
208 | 'location' => $this->createLocation($ast), |
||
209 | ]); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param array $ast |
||
214 | * @return DocumentNode |
||
215 | * @throws LanguageException |
||
216 | */ |
||
217 | protected function buildDocument(array $ast): DocumentNode |
||
222 | ]); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param array $ast |
||
227 | * @return EnumValueNode |
||
228 | */ |
||
229 | protected function buildEnum(array $ast): EnumValueNode |
||
230 | { |
||
231 | return new EnumValueNode([ |
||
232 | 'value' => $this->getValue($ast, 'value'), |
||
233 | 'location' => $this->createLocation($ast), |
||
234 | ]); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param array $ast |
||
239 | * @return EnumTypeDefinitionNode |
||
240 | * @throws LanguageException |
||
241 | */ |
||
242 | protected function buildEnumTypeDefinition(array $ast): EnumTypeDefinitionNode |
||
243 | { |
||
244 | return new EnumTypeDefinitionNode([ |
||
245 | 'description' => $this->buildNode($ast, 'description'), |
||
246 | 'name' => $this->buildNode($ast, 'name'), |
||
247 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
248 | 'values' => $this->buildNodes($ast, 'values'), |
||
249 | 'location' => $this->createLocation($ast), |
||
250 | ]); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param array $ast |
||
255 | * @return EnumTypeExtensionNode |
||
256 | * @throws LanguageException |
||
257 | */ |
||
258 | protected function buildEnumTypeExtension(array $ast): EnumTypeExtensionNode |
||
259 | { |
||
260 | return new EnumTypeExtensionNode([ |
||
261 | 'name' => $this->buildNode($ast, 'name'), |
||
262 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
263 | 'values' => $this->buildNodes($ast, 'values'), |
||
264 | 'location' => $this->createLocation($ast), |
||
265 | ]); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param array $ast |
||
270 | * @return EnumValueDefinitionNode |
||
271 | * @throws LanguageException |
||
272 | */ |
||
273 | protected function buildEnumValueDefinition(array $ast): EnumValueDefinitionNode |
||
274 | { |
||
275 | return new EnumValueDefinitionNode([ |
||
276 | 'description' => $this->buildNode($ast, 'description'), |
||
277 | 'name' => $this->buildNode($ast, 'name'), |
||
278 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
279 | 'location' => $this->createLocation($ast), |
||
280 | ]); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param array $ast |
||
285 | * @return FieldDefinitionNode |
||
286 | * @throws LanguageException |
||
287 | */ |
||
288 | protected function buildFieldDefinition(array $ast): FieldDefinitionNode |
||
289 | { |
||
290 | return new FieldDefinitionNode([ |
||
291 | 'description' => $this->buildNode($ast, 'description'), |
||
292 | 'name' => $this->buildNode($ast, 'name'), |
||
293 | 'arguments' => $this->buildNodes($ast, 'arguments'), |
||
294 | 'type' => $this->buildNode($ast, 'type'), |
||
295 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
296 | 'location' => $this->createLocation($ast), |
||
297 | ]); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param array $ast |
||
302 | * @return FieldNode |
||
303 | * @throws LanguageException |
||
304 | */ |
||
305 | protected function buildField(array $ast): FieldNode |
||
306 | { |
||
307 | return new FieldNode([ |
||
308 | 'alias' => $this->buildNode($ast, 'alias'), |
||
309 | 'name' => $this->buildNode($ast, 'name'), |
||
310 | 'arguments' => $this->buildNodes($ast, 'arguments'), |
||
311 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
312 | 'selectionSet' => $this->buildNode($ast, 'selectionSet'), |
||
313 | 'location' => $this->createLocation($ast), |
||
314 | ]); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @param array $ast |
||
319 | * @return FloatValueNode |
||
320 | */ |
||
321 | protected function buildFloat(array $ast): FloatValueNode |
||
322 | { |
||
323 | return new FloatValueNode([ |
||
324 | 'value' => $this->getValue($ast, 'value'), |
||
325 | 'location' => $this->createLocation($ast), |
||
326 | ]); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param array $ast |
||
331 | * @return FragmentDefinitionNode |
||
332 | * @throws LanguageException |
||
333 | */ |
||
334 | protected function buildFragmentDefinition(array $ast): FragmentDefinitionNode |
||
335 | { |
||
336 | return new FragmentDefinitionNode([ |
||
337 | 'name' => $this->buildNode($ast, 'name'), |
||
338 | 'variableDefinitions' => $this->buildNodes($ast, 'variableDefinitions'), |
||
339 | 'typeCondition' => $this->buildNode($ast, 'typeCondition'), |
||
340 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
341 | 'selectionSet' => $this->buildNode($ast, 'selectionSet'), |
||
342 | 'location' => $this->createLocation($ast), |
||
343 | ]); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param array $ast |
||
348 | * @return FragmentSpreadNode |
||
349 | * @throws LanguageException |
||
350 | */ |
||
351 | protected function buildFragmentSpread(array $ast): FragmentSpreadNode |
||
352 | { |
||
353 | return new FragmentSpreadNode([ |
||
354 | 'name' => $this->buildNode($ast, 'name'), |
||
355 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
356 | 'selectionSet' => $this->buildNode($ast, 'selectionSet'), |
||
357 | 'location' => $this->createLocation($ast), |
||
358 | ]); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param array $ast |
||
363 | * @return InlineFragmentNode |
||
364 | * @throws LanguageException |
||
365 | */ |
||
366 | protected function buildInlineFragment(array $ast): InlineFragmentNode |
||
367 | { |
||
368 | return new InlineFragmentNode([ |
||
369 | 'typeCondition' => $this->buildNode($ast, 'typeCondition'), |
||
370 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
371 | 'selectionSet' => $this->buildNode($ast, 'selectionSet'), |
||
372 | 'location' => $this->createLocation($ast), |
||
373 | ]); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @param array $ast |
||
378 | * @return InputObjectTypeDefinitionNode |
||
379 | * @throws LanguageException |
||
380 | */ |
||
381 | protected function buildInputObjectTypeDefinition(array $ast): InputObjectTypeDefinitionNode |
||
382 | { |
||
383 | return new InputObjectTypeDefinitionNode([ |
||
384 | 'description' => $this->buildNode($ast, 'description'), |
||
385 | 'name' => $this->buildNode($ast, 'name'), |
||
386 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
387 | 'fields' => $this->buildNodes($ast, 'fields'), |
||
388 | 'location' => $this->createLocation($ast), |
||
389 | ]); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @param array $ast |
||
394 | * @return InputObjectTypeExtensionNode |
||
395 | * @throws LanguageException |
||
396 | */ |
||
397 | protected function buildInputObjectTypeExtension(array $ast): InputObjectTypeExtensionNode |
||
398 | { |
||
399 | return new InputObjectTypeExtensionNode([ |
||
400 | 'name' => $this->buildNode($ast, 'name'), |
||
401 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
402 | 'fields' => $this->buildNodes($ast, 'fields'), |
||
403 | 'location' => $this->createLocation($ast), |
||
404 | ]); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * @param array $ast |
||
409 | * @return InputValueDefinitionNode |
||
410 | * @throws LanguageException |
||
411 | */ |
||
412 | protected function buildInputValueDefinition(array $ast): InputValueDefinitionNode |
||
413 | { |
||
414 | return new InputValueDefinitionNode([ |
||
415 | 'description' => $this->buildNode($ast, 'description'), |
||
416 | 'name' => $this->buildNode($ast, 'name'), |
||
417 | 'type' => $this->buildNode($ast, 'type'), |
||
418 | 'defaultValue' => $this->buildNode($ast, 'defaultValue'), |
||
419 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
420 | 'location' => $this->createLocation($ast), |
||
421 | ]); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @param array $ast |
||
426 | * @return InterfaceTypeDefinitionNode |
||
427 | * @throws LanguageException |
||
428 | */ |
||
429 | protected function buildInterfaceTypeDefinition(array $ast): InterfaceTypeDefinitionNode |
||
430 | { |
||
431 | return new InterfaceTypeDefinitionNode([ |
||
432 | 'description' => $this->buildNode($ast, 'description'), |
||
433 | 'name' => $this->buildNode($ast, 'name'), |
||
434 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
435 | 'fields' => $this->buildNodes($ast, 'fields'), |
||
436 | 'location' => $this->createLocation($ast), |
||
437 | ]); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * @param array $ast |
||
442 | * @return InterfaceTypeExtensionNode |
||
443 | * @throws LanguageException |
||
444 | */ |
||
445 | protected function buildInterfaceTypeExtension(array $ast): InterfaceTypeExtensionNode |
||
446 | { |
||
447 | return new InterfaceTypeExtensionNode([ |
||
448 | 'name' => $this->buildNode($ast, 'name'), |
||
449 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
450 | 'fields' => $this->buildNodes($ast, 'fields'), |
||
451 | 'location' => $this->createLocation($ast), |
||
452 | ]); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @param array $ast |
||
457 | * @return IntValueNode |
||
458 | */ |
||
459 | protected function buildInt(array $ast): IntValueNode |
||
460 | { |
||
461 | return new IntValueNode([ |
||
462 | 'value' => $this->getValue($ast, 'value'), |
||
463 | 'location' => $this->createLocation($ast), |
||
464 | ]); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @param array $ast |
||
469 | * @return ListTypeNode |
||
470 | * @throws LanguageException |
||
471 | */ |
||
472 | protected function buildListType(array $ast): ListTypeNode |
||
473 | { |
||
474 | return new ListTypeNode([ |
||
475 | 'type' => $this->buildNode($ast, 'type'), |
||
476 | 'location' => $this->createLocation($ast), |
||
477 | ]); |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * @param array $ast |
||
482 | * @return ListValueNode |
||
483 | * @throws LanguageException |
||
484 | */ |
||
485 | protected function buildList(array $ast): ListValueNode |
||
490 | ]); |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * @param array $ast |
||
495 | * @return NamedTypeNode |
||
496 | * @throws LanguageException |
||
497 | */ |
||
498 | protected function buildNamedType(array $ast): NamedTypeNode |
||
499 | { |
||
500 | return new NamedTypeNode([ |
||
501 | 'name' => $this->buildNode($ast, 'name'), |
||
502 | 'location' => $this->createLocation($ast), |
||
503 | ]); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * @param array $ast |
||
508 | * @return NameNode |
||
509 | */ |
||
510 | protected function buildName(array $ast): NameNode |
||
511 | { |
||
512 | return new NameNode([ |
||
513 | 'value' => $this->getValue($ast, 'value'), |
||
514 | 'location' => $this->createLocation($ast), |
||
515 | ]); |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * @param array $ast |
||
520 | * @return NonNullTypeNode |
||
521 | * @throws LanguageException |
||
522 | */ |
||
523 | protected function buildNonNullType(array $ast): NonNullTypeNode |
||
524 | { |
||
525 | return new NonNullTypeNode([ |
||
526 | 'type' => $this->buildNode($ast, 'type'), |
||
527 | 'location' => $this->createLocation($ast), |
||
528 | ]); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * @param array $ast |
||
533 | * @return NullValueNode |
||
534 | */ |
||
535 | protected function buildNull(array $ast): NullValueNode |
||
536 | { |
||
537 | return new NullValueNode([ |
||
538 | 'location' => $this->createLocation($ast), |
||
539 | ]); |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * @param array $ast |
||
544 | * @return ObjectFieldNode |
||
545 | * @throws LanguageException |
||
546 | */ |
||
547 | protected function buildObjectField(array $ast): ObjectFieldNode |
||
548 | { |
||
549 | return new ObjectFieldNode([ |
||
550 | 'name' => $this->buildNode($ast, 'name'), |
||
551 | 'value' => $this->buildNode($ast, 'value'), |
||
552 | 'location' => $this->createLocation($ast), |
||
553 | ]); |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * @param array $ast |
||
558 | * @return ObjectTypeDefinitionNode |
||
559 | * @throws LanguageException |
||
560 | */ |
||
561 | protected function buildObjectTypeDefinition(array $ast): ObjectTypeDefinitionNode |
||
562 | { |
||
563 | return new ObjectTypeDefinitionNode([ |
||
564 | 'description' => $this->buildNode($ast, 'description'), |
||
565 | 'name' => $this->buildNode($ast, 'name'), |
||
566 | 'interfaces' => $this->buildNodes($ast, 'interfaces'), |
||
567 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
568 | 'fields' => $this->buildNodes($ast, 'fields'), |
||
569 | 'location' => $this->createLocation($ast), |
||
570 | ]); |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * @param array $ast |
||
575 | * @return ObjectTypeExtensionNode |
||
576 | * @throws LanguageException |
||
577 | */ |
||
578 | protected function buildObjectTypeExtension(array $ast): ObjectTypeExtensionNode |
||
579 | { |
||
580 | return new ObjectTypeExtensionNode([ |
||
581 | 'name' => $this->buildNode($ast, 'name'), |
||
582 | 'interfaces' => $this->buildNodes($ast, 'interfaces'), |
||
583 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
584 | 'fields' => $this->buildNodes($ast, 'fields'), |
||
585 | 'location' => $this->createLocation($ast), |
||
586 | ]); |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * @param array $ast |
||
591 | * @return ObjectValueNode |
||
592 | * @throws LanguageException |
||
593 | */ |
||
594 | protected function buildObject(array $ast): ObjectValueNode |
||
595 | { |
||
596 | return new ObjectValueNode([ |
||
597 | 'fields' => $this->buildNodes($ast, 'fields'), |
||
598 | 'location' => $this->createLocation($ast), |
||
599 | ]); |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * @param array $ast |
||
604 | * @return OperationDefinitionNode |
||
605 | * @throws LanguageException |
||
606 | */ |
||
607 | protected function buildOperationDefinition(array $ast): OperationDefinitionNode |
||
608 | { |
||
609 | return new OperationDefinitionNode([ |
||
610 | 'operation' => $this->getValue($ast, 'operation'), |
||
611 | 'name' => $this->buildNode($ast, 'name'), |
||
612 | 'variableDefinitions' => $this->buildNodes($ast, 'variableDefinitions'), |
||
613 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
614 | 'selectionSet' => $this->buildNode($ast, 'selectionSet'), |
||
615 | 'location' => $this->createLocation($ast), |
||
616 | ]); |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * @param array $ast |
||
621 | * @return OperationTypeDefinitionNode |
||
622 | * @throws LanguageException |
||
623 | */ |
||
624 | protected function buildOperationTypeDefinition(array $ast): OperationTypeDefinitionNode |
||
625 | { |
||
626 | return new OperationTypeDefinitionNode([ |
||
627 | 'operation' => $this->getValue($ast, 'operation'), |
||
628 | 'type' => $this->buildNode($ast, 'type'), |
||
629 | 'location' => $this->createLocation($ast), |
||
630 | ]); |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * @param array $ast |
||
635 | * @return ScalarTypeDefinitionNode |
||
636 | * @throws LanguageException |
||
637 | */ |
||
638 | protected function buildScalarTypeDefinition(array $ast): ScalarTypeDefinitionNode |
||
639 | { |
||
640 | return new ScalarTypeDefinitionNode([ |
||
641 | 'description' => $this->buildNode($ast, 'description'), |
||
642 | 'name' => $this->buildNode($ast, 'name'), |
||
643 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
644 | 'location' => $this->createLocation($ast), |
||
645 | ]); |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * @param array $ast |
||
650 | * @return ScalarTypeExtensionNode |
||
651 | * @throws LanguageException |
||
652 | */ |
||
653 | protected function buildScalarTypeExtension(array $ast): ScalarTypeExtensionNode |
||
654 | { |
||
655 | return new ScalarTypeExtensionNode([ |
||
656 | 'name' => $this->buildNode($ast, 'name'), |
||
657 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
658 | 'location' => $this->createLocation($ast), |
||
659 | ]); |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * @param array $ast |
||
664 | * @return SchemaDefinitionNode |
||
665 | * @throws LanguageException |
||
666 | */ |
||
667 | protected function buildSchemaDefinition(array $ast): SchemaDefinitionNode |
||
668 | { |
||
669 | return new SchemaDefinitionNode([ |
||
670 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
671 | 'operationTypes' => $this->buildNodes($ast, 'operationTypes'), |
||
672 | 'location' => $this->createLocation($ast), |
||
673 | ]); |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * @param array $ast |
||
678 | * @return SelectionSetNode |
||
679 | * @throws LanguageException |
||
680 | */ |
||
681 | protected function buildSelectionSet(array $ast): SelectionSetNode |
||
682 | { |
||
683 | return new SelectionSetNode([ |
||
684 | 'selections' => $this->buildNodes($ast, 'selections'), |
||
685 | 'location' => $this->createLocation($ast), |
||
686 | ]); |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * @param array $ast |
||
691 | * @return StringValueNode |
||
692 | */ |
||
693 | protected function buildString(array $ast): StringValueNode |
||
694 | { |
||
695 | return new StringValueNode([ |
||
696 | 'value' => $this->getValue($ast, 'value'), |
||
697 | 'block' => $this->getValue($ast, 'block', false), |
||
698 | 'location' => $this->createLocation($ast), |
||
699 | ]); |
||
700 | } |
||
701 | |||
702 | /** |
||
703 | * @param array $ast |
||
704 | * @return UnionTypeDefinitionNode |
||
705 | * @throws LanguageException |
||
706 | */ |
||
707 | protected function buildUnionTypeDefinition(array $ast): UnionTypeDefinitionNode |
||
708 | { |
||
709 | return new UnionTypeDefinitionNode([ |
||
710 | 'description' => $this->buildNode($ast, 'description'), |
||
711 | 'name' => $this->buildNode($ast, 'name'), |
||
712 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
713 | 'types' => $this->buildNodes($ast, 'types'), |
||
714 | 'location' => $this->createLocation($ast), |
||
715 | ]); |
||
716 | } |
||
717 | |||
718 | /** |
||
719 | * @param array $ast |
||
720 | * @return UnionTypeExtensionNode |
||
721 | * @throws LanguageException |
||
722 | */ |
||
723 | protected function buildUnionTypeExtension(array $ast): UnionTypeExtensionNode |
||
724 | { |
||
725 | return new UnionTypeExtensionNode([ |
||
726 | 'name' => $this->buildNode($ast, 'name'), |
||
727 | 'directives' => $this->buildNodes($ast, 'directives'), |
||
728 | 'types' => $this->buildNodes($ast, 'types'), |
||
729 | 'location' => $this->createLocation($ast), |
||
730 | ]); |
||
731 | } |
||
732 | |||
733 | /** |
||
734 | * @param array $ast |
||
735 | * @return VariableDefinitionNode |
||
736 | * @throws LanguageException |
||
737 | */ |
||
738 | protected function buildVariableDefinition(array $ast): VariableDefinitionNode |
||
739 | { |
||
740 | return new VariableDefinitionNode([ |
||
741 | 'variable' => $this->buildNode($ast, 'variable'), |
||
742 | 'type' => $this->buildNode($ast, 'type'), |
||
743 | 'defaultValue' => $this->buildNode($ast, 'defaultValue'), |
||
744 | 'location' => $this->createLocation($ast), |
||
745 | ]); |
||
746 | } |
||
747 | |||
748 | /** |
||
749 | * @param array $ast |
||
750 | * @return VariableNode |
||
751 | * @throws LanguageException |
||
752 | */ |
||
753 | protected function buildVariable(array $ast): VariableNode |
||
758 | ]); |
||
759 | } |
||
760 | |||
761 | /** |
||
762 | * Creates a location object. |
||
763 | * |
||
764 | * @param array $ast |
||
765 | * @return Location|null |
||
766 | */ |
||
767 | protected function createLocation(array $ast): ?Location |
||
768 | { |
||
769 | return isset($ast['loc']['start'], $ast['loc']['end']) |
||
770 | ? new Location($ast['loc']['start'], $ast['loc']['end'], $ast['loc']['source'] ?? null) |
||
771 | : null; |
||
772 | } |
||
773 | |||
774 | /** |
||
775 | * Returns the value of a single property in the given AST. |
||
776 | * |
||
777 | * @param array $ast |
||
778 | * @param string $propertyName |
||
779 | * @param null $defaultValue |
||
|
|||
780 | * @return mixed|null |
||
781 | */ |
||
782 | protected function getValue(array $ast, string $propertyName, $defaultValue = null) |
||
785 | } |
||
786 | |||
787 | /** |
||
788 | * Builds a single item from the given AST. |
||
789 | * |
||
790 | * @param array $ast |
||
791 | * @param string $propertyName |
||
792 | * @return mixed|null |
||
793 | * @throws LanguageException |
||
794 | */ |
||
795 | protected function buildNode(array $ast, string $propertyName) |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * Builds many items from the given AST. |
||
802 | * |
||
803 | * @param array $ast |
||
804 | * @param string $propertyName |
||
805 | * @return array |
||
806 | * @throws LanguageException |
||
807 | */ |
||
808 | protected function buildNodes(array $ast, string $propertyName): array |
||
809 | { |
||
810 | $array = []; |
||
811 | |||
812 | if (isset($ast[$propertyName]) && \is_array($ast[$propertyName])) { |
||
813 | foreach ($ast[$propertyName] as $subAst) { |
||
814 | $array[] = $this->build($subAst); |
||
815 | } |
||
816 | } |
||
819 | } |
||
820 | } |
||
821 |