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