Total Complexity | 80 |
Total Lines | 696 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TypesRule 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 TypesRule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class TypesRule extends AbstractRule |
||
37 | { |
||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | public function evaluate(): void |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param NamedTypeInterface|ObjectType|InterfaceType $type |
||
99 | * @throws InvariantException |
||
100 | */ |
||
101 | protected function validateFields(NamedTypeInterface $type): void |
||
102 | { |
||
103 | $fields = $type->getFields(); |
||
|
|||
104 | |||
105 | // Objects and Interfaces both must define one or more fields. |
||
106 | if (empty($fields)) { |
||
107 | $this->context->reportError( |
||
108 | new SchemaValidationException( |
||
109 | \sprintf('Type %s must define one or more fields.', $type->getName()), |
||
110 | $this->getAllObjectOrInterfaceNodes($type) |
||
111 | ) |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | foreach ($fields as $fieldName => $field) { |
||
116 | // Ensure they are named correctly. |
||
117 | $this->validateName($field); |
||
118 | |||
119 | // Ensure they were defined at most once. |
||
120 | $fieldNodes = $this->getAllFieldNodes($type, $fieldName); |
||
121 | |||
122 | if (\count($fieldNodes) > 1) { |
||
123 | $this->context->reportError( |
||
124 | new SchemaValidationException( |
||
125 | \sprintf('Field %s.%s can only be defined once.', $type->getName(), $fieldName), |
||
126 | $fieldNodes |
||
127 | ) |
||
128 | ); |
||
129 | |||
130 | return; // continue loop |
||
131 | } |
||
132 | |||
133 | $fieldType = $field->getType(); |
||
134 | |||
135 | // Ensure the type is an output type |
||
136 | if (!isOutputType($fieldType)) { |
||
137 | $fieldTypeNode = $this->getFieldTypeNode($type, $fieldName); |
||
138 | $this->context->reportError( |
||
139 | new SchemaValidationException( |
||
140 | \sprintf( |
||
141 | 'The type of %s.%s must be Output Type but got: %s.', |
||
142 | $type->getName(), |
||
143 | $fieldName, |
||
144 | toString($fieldType) |
||
145 | ), |
||
146 | [$fieldTypeNode] |
||
147 | ) |
||
148 | ); |
||
149 | } |
||
150 | |||
151 | // Ensure the arguments are valid |
||
152 | $argumentNames = []; |
||
153 | |||
154 | foreach ($field->getArguments() as $argument) { |
||
155 | $argumentName = $argument->getName(); |
||
156 | |||
157 | // Ensure they are named correctly. |
||
158 | $this->validateName($argument); |
||
159 | |||
160 | // Ensure they are unique per field. |
||
161 | if (isset($argumentNames[$argumentName])) { |
||
162 | $this->context->reportError( |
||
163 | new SchemaValidationException( |
||
164 | \sprintf( |
||
165 | 'Field argument %s.%s(%s:) can only be defined once.', |
||
166 | $type->getName(), |
||
167 | $field->getName(), |
||
168 | $argumentName |
||
169 | ), |
||
170 | $this->getAllFieldArgumentNodes($type, $fieldName, $argumentName) |
||
171 | ) |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | $argumentNames[$argumentName] = true; |
||
176 | |||
177 | // Ensure the type is an input type |
||
178 | if (!isInputType($argument->getType())) { |
||
179 | $this->context->reportError( |
||
180 | new SchemaValidationException( |
||
181 | \sprintf( |
||
182 | 'The type of %s.%s(%s:) must be Input Type but got: %s.', |
||
183 | $type->getName(), |
||
184 | $fieldName, |
||
185 | $argumentName, |
||
186 | toString($argument->getType()) |
||
187 | ), |
||
188 | $this->getAllFieldArgumentNodes($type, $fieldName, $argumentName) |
||
189 | ) |
||
190 | ); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param ObjectType $objectType |
||
198 | * @throws InvariantException |
||
199 | */ |
||
200 | protected function validateObjectInterfaces(ObjectType $objectType): void |
||
201 | { |
||
202 | $implementedTypeNames = []; |
||
203 | |||
204 | foreach ($objectType->getInterfaces() as $interface) { |
||
205 | if (!($interface instanceof InterfaceType)) { |
||
206 | $this->context->reportError( |
||
207 | new SchemaValidationException( |
||
208 | \sprintf( |
||
209 | 'Type %s must only implement Interface types, it cannot implement %s.', |
||
210 | toString($objectType), |
||
211 | toString($interface) |
||
212 | ), |
||
213 | null !== $interface |
||
214 | ? [$this->getImplementsInterfaceNode($objectType, $interface->getName())] |
||
215 | : null |
||
216 | ) |
||
217 | ); |
||
218 | |||
219 | continue; |
||
220 | } |
||
221 | |||
222 | $interfaceName = $interface->getName(); |
||
223 | |||
224 | if (isset($implementedTypeNames[$interfaceName])) { |
||
225 | $this->context->reportError( |
||
226 | new SchemaValidationException( |
||
227 | \sprintf('Type %s can only implement %s once.', $objectType->getName(), $interfaceName), |
||
228 | $this->getAllImplementsInterfaceNodes($objectType, $interfaceName) |
||
229 | ) |
||
230 | ); |
||
231 | |||
232 | continue; |
||
233 | } |
||
234 | |||
235 | $implementedTypeNames[$interfaceName] = true; |
||
236 | |||
237 | $this->validateObjectImplementsInterface($objectType, $interface); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param ObjectType $objectType |
||
243 | * @param InterfaceType $interfaceType |
||
244 | * @throws InvariantException |
||
245 | */ |
||
246 | protected function validateObjectImplementsInterface(ObjectType $objectType, InterfaceType $interfaceType): void |
||
384 | } |
||
385 | } |
||
386 | } |
||
387 | } |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param ValidationContext $this ->context |
||
392 | * @param UnionType $unionType |
||
393 | * @throws InvariantException |
||
394 | */ |
||
395 | protected function validateUnionMembers(UnionType $unionType): void |
||
396 | { |
||
397 | $memberTypes = $unionType->getTypes(); |
||
398 | |||
399 | if (empty($memberTypes)) { |
||
400 | $this->context->reportError( |
||
401 | new SchemaValidationException( |
||
402 | sprintf('Union type %s must define one or more member types.', $unionType->getName()), |
||
403 | [$unionType->getAstNode()] |
||
404 | ) |
||
405 | ); |
||
406 | } |
||
407 | |||
408 | $includedTypeNames = []; |
||
409 | |||
410 | foreach ($memberTypes as $memberType) { |
||
411 | $memberTypeName = $memberType->getName(); |
||
412 | if (isset($includedTypeNames[$memberTypeName])) { |
||
413 | $this->context->reportError( |
||
414 | new SchemaValidationException( |
||
415 | \sprintf( |
||
416 | 'Union type %s can only include type %s once.', |
||
417 | $unionType->getName(), |
||
418 | $memberTypeName |
||
419 | ), |
||
420 | $this->getUnionMemberTypeNodes($unionType, $memberTypeName) |
||
421 | ) |
||
422 | ); |
||
423 | |||
424 | continue; |
||
425 | } |
||
426 | |||
427 | $includedTypeNames[$memberTypeName] = true; |
||
428 | |||
429 | if (!($memberType instanceof ObjectType)) { |
||
430 | $this->context->reportError( |
||
431 | new SchemaValidationException( |
||
432 | \sprintf( |
||
433 | 'Union type %s can only include Object types, it cannot include %s.', |
||
434 | $unionType->getName(), |
||
435 | toString($memberType) |
||
436 | ), |
||
437 | null !== $memberTypeName |
||
438 | ? $this->getUnionMemberTypeNodes($unionType, $memberTypeName) |
||
439 | : null |
||
440 | ) |
||
441 | ); |
||
442 | } |
||
443 | } |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @param ValidationContext $this ->context |
||
448 | * @param EnumType $enumType |
||
449 | * @throws InvariantException |
||
450 | */ |
||
451 | protected function validateEnumValues(EnumType $enumType): void |
||
452 | { |
||
453 | $enumValues = $enumType->getValues(); |
||
454 | |||
455 | if (empty($enumValues)) { |
||
456 | $this->context->reportError( |
||
457 | new SchemaValidationException( |
||
458 | \sprintf('Enum type %s must define one or more values.', $enumType->getName()), |
||
459 | [$enumType->getAstNode()] |
||
460 | ) |
||
461 | ); |
||
462 | } |
||
463 | |||
464 | foreach ($enumValues as $enumValue) { |
||
465 | $valueName = $enumValue->getName(); |
||
466 | |||
467 | // Ensure no duplicates. |
||
468 | $allNodes = $this->getEnumValueNodes($enumType, $valueName); |
||
469 | |||
470 | if (null !== $allNodes && \count($allNodes) > 1) { |
||
471 | $this->context->reportError( |
||
472 | new SchemaValidationException( |
||
473 | sprintf('Enum type %s can include value %s only once.', $enumType->getName(), $valueName), |
||
474 | $allNodes |
||
475 | ) |
||
476 | ); |
||
477 | |||
478 | continue; |
||
479 | } |
||
480 | |||
481 | // Ensure valid name. |
||
482 | $this->validateName($enumValue); |
||
483 | |||
484 | if ($valueName === 'true' || $valueName === 'false' || $valueName === 'null') { |
||
485 | $this->context->reportError( |
||
486 | new SchemaValidationException( |
||
487 | sprintf('Enum type %s cannot include value: %s.', $enumType->getName(), $valueName), |
||
488 | [$enumValue->getAstNode()] |
||
489 | ) |
||
490 | ); |
||
491 | |||
492 | continue; |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * @param ValidationContext $this ->context |
||
499 | * @param InputObjectType $inputObjectType |
||
500 | * @throws InvariantException |
||
501 | */ |
||
502 | protected function validateInputFields(InputObjectType $inputObjectType): void |
||
503 | { |
||
504 | $fields = $inputObjectType->getFields(); |
||
505 | |||
506 | if (empty($fields)) { |
||
507 | $this->context->reportError( |
||
508 | new SchemaValidationException( |
||
509 | \sprintf('Input Object type %s must define one or more fields.', $inputObjectType->getName()), |
||
510 | [$inputObjectType->getAstNode()] |
||
511 | ) |
||
512 | ); |
||
513 | } |
||
514 | |||
515 | // Ensure the arguments are valid |
||
516 | foreach ($fields as $fieldName => $field) { |
||
517 | // Ensure they are named correctly. |
||
518 | $this->validateName($field); |
||
519 | |||
520 | // TODO: Ensure they are unique per field. |
||
521 | |||
522 | // Ensure the type is an input type |
||
523 | if (!isInputType($field->getType())) { |
||
524 | $this->context->reportError( |
||
525 | new SchemaValidationException( |
||
526 | \sprintf( |
||
527 | 'The type of %s.%s must be Input Type but got: %s.', |
||
528 | $inputObjectType->getName(), |
||
529 | $fieldName, |
||
530 | toString($field->getType()) |
||
531 | ), |
||
532 | [$field->getAstNode()] |
||
533 | ) |
||
534 | ); |
||
535 | } |
||
536 | } |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * @param NamedTypeInterface|ObjectType|InterfaceType $type |
||
541 | * @return ObjectTypeDefinitionNode[]|ObjectTypeExtensionNode[]|InterfaceTypeDefinitionNode[] |
||
542 | * |InterfaceTypeExtensionNode[] |
||
543 | */ |
||
544 | protected function getAllObjectOrInterfaceNodes(NamedTypeInterface $type): array |
||
545 | { |
||
546 | $node = $type->getAstNode(); |
||
547 | |||
548 | if (null !== $node) { |
||
549 | return $type->hasExtensionAstNodes() |
||
550 | ? \array_merge([$node], $type->getExtensionAstNodes()) |
||
551 | : [$node]; |
||
552 | } |
||
553 | |||
554 | return $type->hasExtensionAstNodes() ? $type->getExtensionAstNodes() : []; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * @param NamedTypeInterface|ObjectType|InterfaceType $type |
||
559 | * @param string $fieldName |
||
560 | * @return TypeNodeInterface|null |
||
561 | */ |
||
562 | protected function getFieldTypeNode(NamedTypeInterface $type, string $fieldName): ?TypeNodeInterface |
||
563 | { |
||
564 | $fieldNode = $this->getFieldNode($type, $fieldName); |
||
565 | return null !== $fieldNode ? $fieldNode->getType() : null; |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * @param NamedTypeInterface $type |
||
570 | * @param string $fieldName |
||
571 | * @return FieldDefinitionNode|null |
||
572 | */ |
||
573 | protected function getFieldNode(NamedTypeInterface $type, string $fieldName): ?FieldDefinitionNode |
||
574 | { |
||
575 | return $this->getAllFieldNodes($type, $fieldName)[0] ?? null; |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @param NamedTypeInterface|ObjectType|InterfaceType $type |
||
580 | * @param string $fieldName |
||
581 | * @return FieldDefinitionNode[] |
||
582 | */ |
||
583 | protected function getAllFieldNodes(NamedTypeInterface $type, string $fieldName): array |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * @param NamedTypeInterface $type |
||
600 | * @param string $fieldName |
||
601 | * @param string $argumentName |
||
602 | * @return InputValueDefinitionNode|null |
||
603 | */ |
||
604 | protected function getFieldArgumentNode( |
||
605 | NamedTypeInterface $type, |
||
606 | string $fieldName, |
||
607 | string $argumentName |
||
608 | ): ?InputValueDefinitionNode { |
||
609 | return $this->getAllFieldArgumentNodes($type, $fieldName, $argumentName)[0] ?? null; |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * @param NamedTypeInterface|ObjectType|InterfaceType $type |
||
614 | * @param string $fieldName |
||
615 | * @param string $argumentName |
||
616 | * @return InputValueDefinitionNode[] |
||
617 | */ |
||
618 | protected function getAllFieldArgumentNodes( |
||
619 | NamedTypeInterface $type, |
||
620 | string $fieldName, |
||
621 | string $argumentName |
||
622 | ): array { |
||
623 | $nodes = []; |
||
624 | |||
625 | $fieldNode = $this->getFieldNode($type, $fieldName); |
||
626 | |||
627 | if (null !== $fieldNode) { |
||
628 | foreach ($fieldNode->getArguments() as $node) { |
||
629 | if ($node->getNameValue() === $argumentName) { |
||
630 | $nodes[] = $node; |
||
631 | } |
||
632 | } |
||
633 | } |
||
634 | |||
635 | return $nodes; |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * @param ObjectType $type |
||
640 | * @param string $interfaceName |
||
641 | * @return NamedTypeNode|null |
||
642 | */ |
||
643 | protected function getImplementsInterfaceNode(ObjectType $type, string $interfaceName): ?NamedTypeNode |
||
644 | { |
||
645 | return $this->getAllImplementsInterfaceNodes($type, $interfaceName)[0] ?? null; |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * @param ObjectType $type |
||
650 | * @param string $interfaceName |
||
651 | * @return NamedTypeNode[] |
||
652 | */ |
||
653 | protected function getAllImplementsInterfaceNodes(ObjectType $type, string $interfaceName): array |
||
654 | { |
||
655 | $nodes = []; |
||
656 | |||
657 | foreach ($this->getAllObjectOrInterfaceNodes($type) as $object) { |
||
658 | foreach ($object->getInterfaces() as $node) { |
||
659 | if ($node->getNameValue() === $interfaceName) { |
||
660 | $nodes[] = $node; |
||
661 | } |
||
662 | } |
||
663 | } |
||
664 | |||
665 | return $nodes; |
||
666 | } |
||
667 | |||
668 | /** |
||
669 | * @param NamedTypeInterface $type |
||
670 | * @param string $fieldName |
||
671 | * @param string $argumentName |
||
672 | * @return TypeNodeInterface|null |
||
673 | */ |
||
674 | protected function getFieldArgumentTypeNode( |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * @param UnionType $unionType |
||
685 | * @param string $memberTypeName |
||
686 | * @return array|null |
||
687 | */ |
||
688 | protected function getUnionMemberTypeNodes(UnionType $unionType, string $memberTypeName): ?array |
||
689 | { |
||
690 | /** @var UnionTypeDefinitionNode $node */ |
||
691 | $node = $unionType->getAstNode(); |
||
692 | |||
693 | if (null === $node) { |
||
694 | return null; |
||
695 | } |
||
696 | |||
697 | return \array_filter($node->getTypes(), function (NamedTypeNode $type) use ($memberTypeName) { |
||
698 | return $type->getNameValue() === $memberTypeName; |
||
699 | }); |
||
700 | } |
||
701 | |||
702 | /** |
||
703 | * @param EnumType $enumType |
||
704 | * @param string $valueName |
||
705 | * @return array|null |
||
706 | */ |
||
707 | protected function getEnumValueNodes(EnumType $enumType, string $valueName): ?array |
||
708 | { |
||
709 | /** @var EnumTypeDefinitionNode $node */ |
||
710 | $node = $enumType->getAstNode(); |
||
711 | |||
712 | if (null === $node) { |
||
713 | return null; |
||
714 | } |
||
715 | |||
716 | return \array_filter($node->getValues(), function (NameAwareInterface $type) use ($valueName) { |
||
717 | return $type->getNameValue() === $valueName; |
||
718 | }); |
||
719 | } |
||
720 | |||
721 | /** |
||
722 | * @param mixed $node |
||
723 | * @throws InvariantException |
||
724 | */ |
||
725 | protected function validateName($node): void |
||
732 | } |
||
733 | } |
||
734 | } |
||
735 |