Total Complexity | 66 |
Total Lines | 617 |
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 | protected $cachedFieldsAndFragmentNames = []; |
||
82 | |||
83 | /** |
||
84 | * A memoization for when two fragments are compared "between" each other for |
||
85 | * conflicts. Two fragments may be compared many times, so memoizing this can |
||
86 | * dramatically improve the performance of this validator. |
||
87 | * |
||
88 | * @var PairSet |
||
89 | */ |
||
90 | protected $comparedFragmentPairs; |
||
91 | |||
92 | /** |
||
93 | * @return ValidationContextInterface |
||
94 | */ |
||
95 | abstract public function getValidationContext(): ValidationContextInterface; |
||
96 | |||
97 | /** |
||
98 | * @param SelectionSetNode $selectionSet |
||
99 | * @param NamedTypeInterface|null $parentType |
||
100 | * @return array|Conflict[] |
||
101 | * @throws InvalidTypeException |
||
102 | */ |
||
103 | protected function findConflictsWithinSelectionSet( |
||
104 | SelectionSetNode $selectionSet, |
||
105 | ?NamedTypeInterface $parentType = null |
||
106 | ): array { |
||
107 | $this->comparedFragmentPairs = new PairSet(); |
||
108 | |||
109 | $context = $this->getFieldsAndFragmentNames($selectionSet, $parentType); |
||
110 | |||
111 | // (A) Find find all conflicts "within" the fields of this selection set. |
||
112 | // Note: this is the *only place* `collectConflictsWithin` is called. |
||
113 | $this->collectConflictsWithin($context); |
||
114 | |||
115 | $fieldMap = $context->getFieldMap(); |
||
116 | $fragmentNames = $context->getFragmentNames(); |
||
117 | |||
118 | // (B) Then collect conflicts between these fields and those represented by |
||
119 | // each spread fragment name found. |
||
120 | if (!empty($fragmentNames)) { |
||
121 | $fragmentNamesCount = \count($fragmentNames); |
||
122 | $comparedFragments = []; |
||
123 | |||
124 | /** @noinspection ForeachInvariantsInspection */ |
||
125 | for ($i = 0; $i < $fragmentNamesCount; $i++) { |
||
126 | $this->collectConflictsBetweenFieldsAndFragment( |
||
127 | $context, |
||
128 | $comparedFragments, |
||
129 | $fieldMap, |
||
130 | $fragmentNames[$i], |
||
131 | false/* $areMutuallyExclusive */ |
||
132 | ); |
||
133 | |||
134 | // (C) Then compare this fragment with all other fragments found in this |
||
135 | // selection set to collect conflicts between fragments spread together. |
||
136 | // This compares each item in the list of fragment names to every other |
||
137 | // item in that same list (except for itself). |
||
138 | for ($j = $i + 1; $j < $fragmentNamesCount; $j++) { |
||
139 | $this->collectConflictsBetweenFragments( |
||
140 | $context, |
||
141 | $fragmentNames[$i], |
||
142 | $fragmentNames[$j], |
||
143 | false/* $areMutuallyExclusive */ |
||
144 | ); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | return $context->getConflicts(); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Collect all conflicts found between a set of fields and a fragment reference |
||
154 | * including via spreading in any nested fragments. |
||
155 | * |
||
156 | * @param ComparisonContext $context |
||
157 | * @param array $comparedFragments |
||
158 | * @param array $fieldMap |
||
159 | * @param string $fragmentName |
||
160 | * @param bool $areMutuallyExclusive |
||
161 | * @throws InvalidTypeException |
||
162 | */ |
||
163 | protected function collectConflictsBetweenFieldsAndFragment( |
||
216 | ); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Collect all conflicts found between two fragments, including via spreading in |
||
223 | * any nested fragments. |
||
224 | * |
||
225 | * @param ComparisonContext $context |
||
226 | * @param string $fragmentNameA |
||
227 | * @param string $fragmentNameB |
||
228 | * @param bool $areMutuallyExclusive |
||
229 | * @throws InvalidTypeException |
||
230 | */ |
||
231 | protected function collectConflictsBetweenFragments( |
||
300 | ); |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Find all conflicts found between two selection sets, including those found |
||
307 | * via spreading in fragments. Called when determining if conflicts exist |
||
308 | * between the sub-fields of two overlapping fields. |
||
309 | * |
||
310 | * @param NamedTypeInterface|null $parentTypeA |
||
311 | * @param SelectionSetNode $selectionSetA |
||
312 | * @param NamedTypeInterface|null $parentTypeB |
||
313 | * @param SelectionSetNode $selectionSetB |
||
314 | * @param bool $areMutuallyExclusive |
||
315 | * @return Conflict[] |
||
316 | * @throws InvalidTypeException |
||
317 | */ |
||
318 | protected function findConflictsBetweenSubSelectionSets( |
||
319 | ?NamedTypeInterface $parentTypeA, |
||
320 | SelectionSetNode $selectionSetA, |
||
321 | ?NamedTypeInterface $parentTypeB, |
||
322 | SelectionSetNode $selectionSetB, |
||
323 | bool $areMutuallyExclusive |
||
324 | ): array { |
||
325 | $context = new ComparisonContext(); |
||
326 | |||
327 | $contextA = $this->getFieldsAndFragmentNames($selectionSetA, $parentTypeA); |
||
328 | $contextB = $this->getFieldsAndFragmentNames($selectionSetB, $parentTypeB); |
||
329 | $fieldMapA = $contextA->getFieldMap(); |
||
330 | $fieldMapB = $contextB->getFieldMap(); |
||
331 | $fragmentNamesA = $contextA->getFragmentNames(); |
||
332 | $fragmentNamesB = $contextB->getFragmentNames(); |
||
333 | $fragmentNamesACount = \count($fragmentNamesA); |
||
334 | $fragmentNamesBCount = \count($fragmentNamesB); |
||
335 | |||
336 | // (H) First, collect all conflicts between these two collections of field. |
||
337 | $this->collectConflictsBetween( |
||
338 | $context, |
||
339 | $fieldMapA, |
||
340 | $fieldMapB, |
||
341 | $areMutuallyExclusive |
||
342 | ); |
||
343 | |||
344 | // (I) Then collect conflicts between the first collection of fields and |
||
345 | // those referenced by each fragment name associated with the second. |
||
346 | if (!empty($fragmentNamesB)) { |
||
347 | $comparedFragments = []; |
||
348 | |||
349 | /** @noinspection ForeachInvariantsInspection */ |
||
350 | for ($j = 0; $j < $fragmentNamesBCount; $j++) { |
||
351 | $this->collectConflictsBetweenFieldsAndFragment( |
||
352 | $context, |
||
353 | $comparedFragments, |
||
354 | $fieldMapA, |
||
355 | $fragmentNamesB[$j], |
||
356 | $areMutuallyExclusive |
||
357 | ); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | // (I) Then collect conflicts between the second collection of fields and |
||
362 | // those referenced by each fragment name associated with the first. |
||
363 | if (!empty($fragmentNamesA)) { |
||
364 | $comparedFragments = []; |
||
365 | |||
366 | /** @noinspection ForeachInvariantsInspection */ |
||
367 | for ($i = 0; $i < $fragmentNamesACount; $i++) { |
||
368 | $this->collectConflictsBetweenFieldsAndFragment( |
||
369 | $context, |
||
370 | $comparedFragments, |
||
371 | $fieldMapB, |
||
372 | $fragmentNamesA[$i], |
||
373 | $areMutuallyExclusive |
||
374 | ); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | /** @noinspection ForeachInvariantsInspection */ |
||
379 | for ($i = 0; $i < $fragmentNamesACount; $i++) { |
||
380 | /** @noinspection ForeachInvariantsInspection */ |
||
381 | for ($j = 0; $j < $fragmentNamesBCount; $j++) { |
||
382 | $this->collectConflictsBetweenFragments( |
||
383 | $context, |
||
384 | $fragmentNamesA[$i], |
||
385 | $fragmentNamesB[$j], |
||
386 | $areMutuallyExclusive |
||
387 | ); |
||
388 | } |
||
389 | } |
||
390 | |||
391 | return $context->getConflicts(); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Collect all Conflicts "within" one collection of fields. |
||
396 | * |
||
397 | * @param ComparisonContext $context |
||
398 | */ |
||
399 | protected function collectConflictsWithin(ComparisonContext $context): void |
||
400 | { |
||
401 | // A field map is a keyed collection, where each key represents a response |
||
402 | // name and the value at that key is a list of all fields which provide that |
||
403 | // response name. For every response name, if there are multiple fields, they |
||
404 | // must be compared to find a potential conflict. |
||
405 | foreach ($context->getFieldMap() as $responseName => $fields) { |
||
406 | $fieldsCount = \count($fields); |
||
407 | |||
408 | // This compares every field in the list to every other field in this list |
||
409 | // (except to itself). If the list only has one item, nothing needs to |
||
410 | // be compared. |
||
411 | if ($fieldsCount > 1) { |
||
412 | /** @noinspection ForeachInvariantsInspection */ |
||
413 | for ($i = 0; $i < $fieldsCount; $i++) { |
||
414 | for ($j = $i + 1; $j < $fieldsCount; $j++) { |
||
415 | $conflict = $this->findConflict( |
||
416 | $responseName, |
||
417 | $fields[$i], |
||
418 | $fields[$j], |
||
419 | // within one collection is never mutually exclusive |
||
420 | false/* $areMutuallyExclusive */ |
||
421 | ); |
||
422 | |||
423 | if (null !== $conflict) { |
||
424 | $context->reportConflict($conflict); |
||
425 | } |
||
426 | } |
||
427 | } |
||
428 | } |
||
429 | } |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Collect all Conflicts between two collections of fields. This is similar to, |
||
434 | * but different from the `collectConflictsWithin` function above. This check |
||
435 | * assumes that `collectConflictsWithin` has already been called on each |
||
436 | * provided collection of fields. This is true because this validator traverses |
||
437 | * each individual selection set. |
||
438 | * |
||
439 | * @param ComparisonContext $context |
||
440 | * @param array $fieldMapA |
||
441 | * @param array $fieldMapB |
||
442 | * @param bool $parentFieldsAreMutuallyExclusive |
||
443 | */ |
||
444 | protected function collectConflictsBetween( |
||
445 | ComparisonContext $context, |
||
446 | array $fieldMapA, |
||
447 | array $fieldMapB, |
||
448 | bool $parentFieldsAreMutuallyExclusive |
||
449 | ): void { |
||
450 | // A field map is a keyed collection, where each key represents a response |
||
451 | // name and the value at that key is a list of all fields which provide that |
||
452 | // response name. For any response name which appears in both provided field |
||
453 | // maps, each field from the first field map must be compared to every field |
||
454 | // in the second field map to find potential conflicts. |
||
455 | foreach ($fieldMapA as $responseName => $fieldA) { |
||
456 | $fieldB = $fieldMapB[$responseName]; |
||
457 | |||
458 | if (null !== $fieldB) { |
||
459 | $fieldsACount = \count($fieldA); |
||
460 | $fieldsBCount = \count($fieldB); |
||
461 | for ($i = 0; $i < $fieldsACount; $i++) { |
||
462 | for ($j = 0; $j < $fieldsBCount; $j++) { |
||
463 | $conflict = $this->findConflict( |
||
464 | $responseName, |
||
465 | $fieldA, |
||
466 | $fieldB, |
||
467 | $parentFieldsAreMutuallyExclusive |
||
468 | ); |
||
469 | |||
470 | if (null !== $conflict) { |
||
471 | $context->reportConflict($conflict); |
||
472 | } |
||
473 | } |
||
474 | } |
||
475 | } |
||
476 | } |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Determines if there is a conflict between two particular fields, including |
||
481 | * comparing their sub-fields. |
||
482 | * |
||
483 | * @param string $responseName |
||
484 | * @param FieldContext $fieldA |
||
485 | * @param FieldContext $fieldB |
||
486 | * @param bool $parentFieldsAreMutuallyExclusive |
||
487 | * @return Conflict|null |
||
488 | */ |
||
489 | protected function findConflict( |
||
490 | string $responseName, |
||
491 | FieldContext $fieldA, |
||
492 | FieldContext $fieldB, |
||
493 | bool $parentFieldsAreMutuallyExclusive |
||
494 | ): ?Conflict { |
||
495 | $parentTypeA = $fieldA->getParentType(); |
||
496 | $parentTypeB = $fieldB->getParentType(); |
||
497 | |||
498 | // If it is known that two fields could not possibly apply at the same |
||
499 | // time, due to the parent types, then it is safe to permit them to diverge |
||
500 | // in aliased field or arguments used as they will not present any ambiguity |
||
501 | // by differing. |
||
502 | // It is known that two parent types could never overlap if they are |
||
503 | // different Object types. Interface or Union types might overlap - if not |
||
504 | // in the current state of the schema, then perhaps in some future version, |
||
505 | // thus may not safely diverge. |
||
506 | $areMutuallyExclusive = $parentFieldsAreMutuallyExclusive |
||
507 | || ($parentTypeA !== $parentTypeB |
||
508 | && $parentTypeA instanceof ObjectType |
||
509 | && $parentTypeB instanceof ObjectType); |
||
510 | |||
511 | $nodeA = $fieldA->getNode(); |
||
512 | $nodeB = $fieldB->getNode(); |
||
513 | |||
514 | if (!$areMutuallyExclusive) { |
||
515 | // Two aliases must refer to the same field. |
||
516 | $nameA = $nodeA->getNameValue(); |
||
517 | $nameB = $nodeB->getNameValue(); |
||
518 | |||
519 | if ($nameA !== $nameB) { |
||
520 | return new Conflict( |
||
521 | $responseName, |
||
522 | sprintf('%s and %s are different fields', $nameA, $nameB), |
||
523 | [$nodeA], |
||
524 | [$nodeB] |
||
525 | ); |
||
526 | } |
||
527 | } |
||
528 | |||
529 | $definitionA = $fieldA->getDefinition(); |
||
530 | $definitionB = $fieldB->getDefinition(); |
||
531 | |||
532 | // Two field calls must have the same arguments. |
||
533 | if (!compareArguments($nodeA->getArguments(), $nodeB->getArguments())) { |
||
534 | return new Conflict( |
||
535 | $responseName, |
||
536 | 'they have differing arguments', |
||
537 | [$nodeA], |
||
538 | [$nodeB] |
||
539 | ); |
||
540 | } |
||
541 | |||
542 | // The return type for each field. |
||
543 | $typeA = null !== $definitionA ? $definitionA->getType() : null; |
||
544 | $typeB = null !== $definitionB ? $definitionB->getType() : null; |
||
545 | |||
546 | if (null !== $typeA && null !== $typeB && compareTypes($typeA, $typeB)) { |
||
547 | return new Conflict( |
||
548 | $responseName, |
||
549 | sprintf('they return conflicting types %s and %s', (string)$typeA, (string)$typeB), |
||
550 | [$nodeA], |
||
551 | [$nodeB] |
||
552 | ); |
||
553 | } |
||
554 | |||
555 | // Collect and compare sub-fields. Use the same "visited fragment names" list |
||
556 | // for both collections so fields in a fragment reference are never |
||
557 | // compared to themselves. |
||
558 | $selectionSetA = $nodeA->getSelectionSet(); |
||
559 | $selectionSetB = $nodeB->getSelectionSet(); |
||
560 | |||
561 | if (null !== $selectionSetA && null !== $selectionSetB) { |
||
562 | $conflicts = $this->findConflictsBetweenSubSelectionSets( |
||
563 | getNamedType($typeA), |
||
564 | $selectionSetA, |
||
565 | getNamedType($typeB), |
||
566 | $selectionSetB, |
||
567 | $areMutuallyExclusive |
||
568 | ); |
||
569 | |||
570 | return $this->subfieldConflicts($conflicts, $responseName, $nodeA, $nodeB); |
||
571 | } |
||
572 | |||
573 | return null; |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Given a selection set, return the collection of fields (a mapping of response |
||
578 | * name to field nodes and definitions) as well as a list of fragment names |
||
579 | * referenced via fragment spreads. |
||
580 | * |
||
581 | * @param SelectionSetNode $selectionSet |
||
582 | * @param NamedTypeInterface|null $parentType |
||
583 | * @return ComparisonContext |
||
584 | * @throws InvalidTypeException |
||
585 | */ |
||
586 | protected function getFieldsAndFragmentNames( |
||
587 | SelectionSetNode $selectionSet, |
||
588 | ?NamedTypeInterface $parentType |
||
589 | ): ComparisonContext { |
||
590 | $cached = $this->cachedFieldsAndFragmentNames[(string)$selectionSet] ?? null; |
||
591 | |||
592 | if (null === $cached) { |
||
593 | $cached = $this->collectFieldsAndFragmentNames(new ComparisonContext(), $selectionSet, $parentType); |
||
594 | |||
595 | $this->cachedFieldsAndFragmentNames[(string)$selectionSet] = $cached; |
||
596 | } |
||
597 | |||
598 | return $cached; |
||
599 | } |
||
600 | |||
601 | /** |
||
602 | * Given a reference to a fragment, return the represented collection of fields |
||
603 | * as well as a list of nested fragment names referenced via fragment spreads. |
||
604 | * |
||
605 | * @param FragmentDefinitionNode $fragment |
||
606 | * @return ComparisonContext |
||
607 | * @throws InvalidTypeException |
||
608 | */ |
||
609 | protected function getReferencedFieldsAndFragmentNames(FragmentDefinitionNode $fragment): ComparisonContext |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * @param ComparisonContext $context |
||
625 | * @param SelectionSetNode $selectionSet |
||
626 | * @param NamedTypeInterface|null $parentType |
||
627 | * @return ComparisonContext |
||
628 | * @throws InvalidTypeException |
||
629 | */ |
||
630 | protected function collectFieldsAndFragmentNames( |
||
631 | ComparisonContext $context, |
||
632 | SelectionSetNode $selectionSet, |
||
633 | ?NamedTypeInterface $parentType |
||
634 | ): ComparisonContext { |
||
635 | foreach ($selectionSet->getSelections() as $selection) { |
||
636 | if ($selection instanceof FieldNode) { |
||
637 | $fieldName = $selection->getNameValue(); |
||
638 | |||
639 | $definition = ($parentType instanceof ObjectType || $parentType instanceof InterfaceType) |
||
640 | ? $parentType->getFields()[$fieldName] |
||
641 | : null; |
||
642 | |||
643 | $context->registerField(new FieldContext($parentType, $selection, $definition)); |
||
|
|||
644 | } elseif ($selection instanceof FragmentSpreadNode) { |
||
645 | $context->registerFragment($selection); |
||
646 | } elseif ($selection instanceof InlineFragmentNode) { |
||
647 | $typeCondition = $selection->getTypeCondition(); |
||
648 | |||
649 | $inlineFragmentType = null !== $typeCondition |
||
650 | ? typeFromAST($this->getValidationContext()->getSchema(), $typeCondition) |
||
651 | : $parentType; |
||
652 | |||
653 | $this->collectFieldsAndFragmentNames($context, $selection->getSelectionSet(), $inlineFragmentType); |
||
654 | } |
||
655 | } |
||
656 | |||
657 | return $context; |
||
658 | } |
||
659 | |||
660 | /** |
||
661 | * Given a series of Conflicts which occurred between two sub-fields, generate |
||
662 | * a single Conflict. |
||
663 | * |
||
664 | * @param array|Conflict[] $conflicts |
||
665 | * @param string $responseName |
||
666 | * @param FieldNode $nodeA |
||
667 | * @param FieldNode $nodeB |
||
668 | * @return Conflict|null |
||
669 | */ |
||
670 | protected function subfieldConflicts( |
||
691 | ); |
||
692 | } |
||
693 | } |
||
694 |