Total Complexity | 66 |
Total Lines | 622 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FindsConflictsTrait 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 FindsConflictsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
74 | trait FindsConflictsTrait |
||
75 | { |
||
76 | /** |
||
77 | * A cache for the "field map" and list of fragment names found in any given |
||
78 | * selection set. Selection sets may be asked for this information multiple |
||
79 | * times, so this improves the performance of this validator. |
||
80 | * |
||
81 | * @var Map |
||
82 | */ |
||
83 | protected $cachedFieldsAndFragmentNames; |
||
84 | |||
85 | /** |
||
86 | * A memoization for when two fragments are compared "between" each other for |
||
87 | * conflicts. Two fragments may be compared many times, so memoizing this can |
||
88 | * dramatically improve the performance of this validator. |
||
89 | * |
||
90 | * @var PairSet |
||
91 | */ |
||
92 | protected $comparedFragmentPairs; |
||
93 | |||
94 | /** |
||
95 | * @return ValidationContextInterface |
||
96 | */ |
||
97 | abstract public function getValidationContext(): ValidationContextInterface; |
||
98 | |||
99 | /** |
||
100 | * @param SelectionSetNode $selectionSet |
||
101 | * @param NamedTypeInterface|null $parentType |
||
102 | * @return array|Conflict[] |
||
103 | * @throws InvalidTypeException |
||
104 | */ |
||
105 | protected function findConflictsWithinSelectionSet( |
||
106 | SelectionSetNode $selectionSet, |
||
107 | ?NamedTypeInterface $parentType = null |
||
108 | ): array { |
||
109 | $this->cachedFieldsAndFragmentNames = new Map(); |
||
110 | $this->comparedFragmentPairs = new PairSet(); |
||
111 | |||
112 | $context = $this->getFieldsAndFragmentNames($selectionSet, $parentType); |
||
113 | |||
114 | // (A) Find find all conflicts "within" the fields of this selection set. |
||
115 | // Note: this is the *only place* `collectConflictsWithin` is called. |
||
116 | $this->collectConflictsWithin($context); |
||
117 | |||
118 | $fieldMap = $context->getFieldMap(); |
||
119 | $fragmentNames = $context->getFragmentNames(); |
||
120 | |||
121 | // (B) Then collect conflicts between these fields and those represented by |
||
122 | // each spread fragment name found. |
||
123 | if (!empty($fragmentNames)) { |
||
124 | $fragmentNamesCount = \count($fragmentNames); |
||
125 | $comparedFragments = []; |
||
126 | |||
127 | /** @noinspection ForeachInvariantsInspection */ |
||
128 | for ($i = 0; $i < $fragmentNamesCount; $i++) { |
||
129 | $this->collectConflictsBetweenFieldsAndFragment( |
||
130 | $context, |
||
131 | $comparedFragments, |
||
132 | $fieldMap, |
||
133 | $fragmentNames[$i], |
||
134 | false/* $areMutuallyExclusive */ |
||
135 | ); |
||
136 | |||
137 | // (C) Then compare this fragment with all other fragments found in this |
||
138 | // selection set to collect conflicts between fragments spread together. |
||
139 | // This compares each item in the list of fragment names to every other |
||
140 | // item in that same list (except for itself). |
||
141 | for ($j = $i + 1; $j < $fragmentNamesCount; $j++) { |
||
142 | $this->collectConflictsBetweenFragments( |
||
143 | $context, |
||
144 | $fragmentNames[$i], |
||
145 | $fragmentNames[$j], |
||
146 | false/* $areMutuallyExclusive */ |
||
147 | ); |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | return $context->getConflicts(); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Collect all conflicts found between a set of fields and a fragment reference |
||
157 | * including via spreading in any nested fragments. |
||
158 | * |
||
159 | * @param ComparisonContext $context |
||
160 | * @param array $comparedFragments |
||
161 | * @param array $fieldMap |
||
162 | * @param string $fragmentName |
||
163 | * @param bool $areMutuallyExclusive |
||
164 | * @throws InvalidTypeException |
||
165 | */ |
||
166 | protected function collectConflictsBetweenFieldsAndFragment( |
||
219 | ); |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Collect all conflicts found between two fragments, including via spreading in |
||
226 | * any nested fragments. |
||
227 | * |
||
228 | * @param ComparisonContext $context |
||
229 | * @param string $fragmentNameA |
||
230 | * @param string $fragmentNameB |
||
231 | * @param bool $areMutuallyExclusive |
||
232 | * @throws InvalidTypeException |
||
233 | */ |
||
234 | protected function collectConflictsBetweenFragments( |
||
303 | ); |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Find all conflicts found between two selection sets, including those found |
||
310 | * via spreading in fragments. Called when determining if conflicts exist |
||
311 | * between the sub-fields of two overlapping fields. |
||
312 | * |
||
313 | * @param NamedTypeInterface|null $parentTypeA |
||
314 | * @param SelectionSetNode $selectionSetA |
||
315 | * @param NamedTypeInterface|null $parentTypeB |
||
316 | * @param SelectionSetNode $selectionSetB |
||
317 | * @param bool $areMutuallyExclusive |
||
318 | * @return Conflict[] |
||
319 | * @throws InvalidTypeException |
||
320 | */ |
||
321 | protected function findConflictsBetweenSubSelectionSets( |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Collect all Conflicts "within" one collection of fields. |
||
399 | * |
||
400 | * @param ComparisonContext $context |
||
401 | * @throws InvalidTypeException |
||
402 | */ |
||
403 | protected function collectConflictsWithin(ComparisonContext $context): void |
||
429 | } |
||
430 | } |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Collect all Conflicts between two collections of fields. This is similar to, |
||
438 | * but different from the `collectConflictsWithin` function above. This check |
||
439 | * assumes that `collectConflictsWithin` has already been called on each |
||
440 | * provided collection of fields. This is true because this validator traverses |
||
441 | * each individual selection set. |
||
442 | * |
||
443 | * @param ComparisonContext $context |
||
444 | * @param array $fieldMapA |
||
445 | * @param array $fieldMapB |
||
446 | * @param bool $parentFieldsAreMutuallyExclusive |
||
447 | * @throws InvalidTypeException |
||
448 | */ |
||
449 | protected function collectConflictsBetween( |
||
450 | ComparisonContext $context, |
||
451 | array $fieldMapA, |
||
452 | array $fieldMapB, |
||
453 | bool $parentFieldsAreMutuallyExclusive |
||
454 | ): void { |
||
455 | // A field map is a keyed collection, where each key represents a response |
||
456 | // name and the value at that key is a list of all fields which provide that |
||
457 | // response name. For any response name which appears in both provided field |
||
458 | // maps, each field from the first field map must be compared to every field |
||
459 | // in the second field map to find potential conflicts. |
||
460 | foreach ($fieldMapA as $responseName => $fieldA) { |
||
461 | $fieldB = $fieldMapB[$responseName]; |
||
462 | |||
463 | if (null !== $fieldB) { |
||
464 | $fieldsACount = \count($fieldA); |
||
465 | $fieldsBCount = \count($fieldB); |
||
466 | /** @noinspection ForeachInvariantsInspection */ |
||
467 | for ($i = 0; $i < $fieldsACount; $i++) { |
||
468 | /** @noinspection ForeachInvariantsInspection */ |
||
469 | for ($j = 0; $j < $fieldsBCount; $j++) { |
||
470 | $conflict = $this->findConflict( |
||
471 | $responseName, |
||
472 | $fieldA[$i], |
||
473 | $fieldB[$j], |
||
474 | $parentFieldsAreMutuallyExclusive |
||
475 | ); |
||
476 | |||
477 | if (null !== $conflict) { |
||
478 | $context->reportConflict($conflict); |
||
479 | } |
||
480 | } |
||
481 | } |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Determines if there is a conflict between two particular fields, including |
||
488 | * comparing their sub-fields. |
||
489 | * |
||
490 | * @param string $responseName |
||
491 | * @param FieldContext $fieldA |
||
492 | * @param FieldContext $fieldB |
||
493 | * @param bool $parentFieldsAreMutuallyExclusive |
||
494 | * @return Conflict|null |
||
495 | * @throws InvalidTypeException |
||
496 | */ |
||
497 | protected function findConflict( |
||
498 | string $responseName, |
||
499 | FieldContext $fieldA, |
||
500 | FieldContext $fieldB, |
||
501 | bool $parentFieldsAreMutuallyExclusive |
||
502 | ): ?Conflict { |
||
503 | $parentTypeA = $fieldA->getParentType(); |
||
504 | $parentTypeB = $fieldB->getParentType(); |
||
505 | |||
506 | // If it is known that two fields could not possibly apply at the same |
||
507 | // time, due to the parent types, then it is safe to permit them to diverge |
||
508 | // in aliased field or arguments used as they will not present any ambiguity |
||
509 | // by differing. |
||
510 | // It is known that two parent types could never overlap if they are |
||
511 | // different Object types. Interface or Union types might overlap - if not |
||
512 | // in the current state of the schema, then perhaps in some future version, |
||
513 | // thus may not safely diverge. |
||
514 | $areMutuallyExclusive = $parentFieldsAreMutuallyExclusive |
||
515 | || ($parentTypeA !== $parentTypeB |
||
516 | && $parentTypeA instanceof ObjectType |
||
517 | && $parentTypeB instanceof ObjectType); |
||
518 | |||
519 | $nodeA = $fieldA->getNode(); |
||
520 | $nodeB = $fieldB->getNode(); |
||
521 | |||
522 | $definitionA = $fieldA->getDefinition(); |
||
523 | $definitionB = $fieldB->getDefinition(); |
||
524 | |||
525 | if (!$areMutuallyExclusive) { |
||
526 | // Two aliases must refer to the same field. |
||
527 | $nameA = $nodeA->getNameValue(); |
||
528 | $nameB = $nodeB->getNameValue(); |
||
529 | |||
530 | if ($nameA !== $nameB) { |
||
531 | return new Conflict( |
||
532 | $responseName, |
||
533 | sprintf('%s and %s are different fields', $nameA, $nameB), |
||
534 | [$nodeA], |
||
535 | [$nodeB] |
||
536 | ); |
||
537 | } |
||
538 | |||
539 | // Two field calls must have the same arguments. |
||
540 | if (!compareArguments($nodeA->getArguments(), $nodeB->getArguments())) { |
||
541 | return new Conflict( |
||
542 | $responseName, |
||
543 | 'they have differing arguments', |
||
544 | [$nodeA], |
||
545 | [$nodeB] |
||
546 | ); |
||
547 | } |
||
548 | } |
||
549 | |||
550 | // The return type for each field. |
||
551 | $typeA = null !== $definitionA ? $definitionA->getType() : null; |
||
552 | $typeB = null !== $definitionB ? $definitionB->getType() : null; |
||
553 | |||
554 | if (null !== $typeA && null !== $typeB && compareTypes($typeA, $typeB)) { |
||
555 | return new Conflict( |
||
556 | $responseName, |
||
557 | sprintf('they return conflicting types %s and %s', (string)$typeA, (string)$typeB), |
||
558 | [$nodeA], |
||
559 | [$nodeB] |
||
560 | ); |
||
561 | } |
||
562 | |||
563 | // Collect and compare sub-fields. Use the same "visited fragment names" list |
||
564 | // for both collections so fields in a fragment reference are never |
||
565 | // compared to themselves. |
||
566 | $selectionSetA = $nodeA->getSelectionSet(); |
||
567 | $selectionSetB = $nodeB->getSelectionSet(); |
||
568 | |||
569 | if (null !== $selectionSetA && null !== $selectionSetB) { |
||
570 | $conflicts = $this->findConflictsBetweenSubSelectionSets( |
||
571 | getNamedType($typeA), |
||
572 | $selectionSetA, |
||
573 | getNamedType($typeB), |
||
574 | $selectionSetB, |
||
575 | $areMutuallyExclusive |
||
576 | ); |
||
577 | |||
578 | return $this->subfieldConflicts($conflicts, $responseName, $nodeA, $nodeB); |
||
579 | } |
||
580 | |||
581 | return null; |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Given a selection set, return the collection of fields (a mapping of response |
||
586 | * name to field nodes and definitions) as well as a list of fragment names |
||
587 | * referenced via fragment spreads. |
||
588 | * |
||
589 | * @param SelectionSetNode $selectionSet |
||
590 | * @param NamedTypeInterface|null $parentType |
||
591 | * @return ComparisonContext |
||
592 | * @throws InvalidTypeException |
||
593 | */ |
||
594 | protected function getFieldsAndFragmentNames( |
||
595 | SelectionSetNode $selectionSet, |
||
596 | ?NamedTypeInterface $parentType |
||
597 | ): ComparisonContext { |
||
598 | $cached = $this->cachedFieldsAndFragmentNames->get($selectionSet) ?? null; |
||
599 | |||
600 | if (null === $cached) { |
||
601 | $cached = new ComparisonContext(); |
||
602 | |||
603 | $this->collectFieldsAndFragmentNames($cached, $selectionSet, $parentType); |
||
604 | |||
605 | $this->cachedFieldsAndFragmentNames->set($selectionSet, $cached); |
||
606 | } |
||
607 | |||
608 | return $cached; |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * Given a reference to a fragment, return the represented collection of fields |
||
613 | * as well as a list of nested fragment names referenced via fragment spreads. |
||
614 | * |
||
615 | * @param FragmentDefinitionNode $fragment |
||
616 | * @return ComparisonContext |
||
617 | * @throws InvalidTypeException |
||
618 | */ |
||
619 | protected function getReferencedFieldsAndFragmentNames(FragmentDefinitionNode $fragment): ComparisonContext |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * @param ComparisonContext $context |
||
635 | * @param SelectionSetNode $selectionSet |
||
636 | * @param NamedTypeInterface|null $parentType |
||
637 | * @throws InvalidTypeException |
||
638 | */ |
||
639 | protected function collectFieldsAndFragmentNames( |
||
640 | ComparisonContext $context, |
||
641 | SelectionSetNode $selectionSet, |
||
642 | ?NamedTypeInterface $parentType |
||
643 | ): void { |
||
644 | foreach ($selectionSet->getSelections() as $selection) { |
||
645 | if ($selection instanceof FieldNode) { |
||
646 | $definition = ($parentType instanceof ObjectType || $parentType instanceof InterfaceType) |
||
647 | ? $parentType->getFields()[$selection->getNameValue()] |
||
648 | : null; |
||
649 | |||
650 | $context->registerField(new FieldContext($parentType, $selection, $definition)); |
||
|
|||
651 | } elseif ($selection instanceof FragmentSpreadNode) { |
||
652 | $context->registerFragment($selection); |
||
653 | } elseif ($selection instanceof InlineFragmentNode) { |
||
654 | $typeCondition = $selection->getTypeCondition(); |
||
655 | |||
656 | $inlineFragmentType = null !== $typeCondition |
||
657 | ? typeFromAST($this->getValidationContext()->getSchema(), $typeCondition) |
||
658 | : $parentType; |
||
659 | |||
660 | $this->collectFieldsAndFragmentNames($context, $selection->getSelectionSet(), $inlineFragmentType); |
||
661 | } |
||
662 | } |
||
663 | } |
||
664 | |||
665 | /** |
||
666 | * Given a series of Conflicts which occurred between two sub-fields, generate |
||
667 | * a single Conflict. |
||
668 | * |
||
669 | * @param array|Conflict[] $conflicts |
||
670 | * @param string $responseName |
||
671 | * @param FieldNode $nodeA |
||
672 | * @param FieldNode $nodeB |
||
673 | * @return Conflict|null |
||
674 | */ |
||
675 | protected function subfieldConflicts( |
||
696 | ); |
||
697 | } |
||
698 | } |
||
699 |