Total Complexity | 66 |
Total Lines | 620 |
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( |
||
167 | ComparisonContext $context, |
||
168 | array &$comparedFragments, |
||
169 | array $fieldMap, |
||
170 | string $fragmentName, |
||
171 | bool $areMutuallyExclusive |
||
172 | ): void { |
||
173 | // Memoize so a fragment is not compared for conflicts more than once. |
||
174 | if (isset($comparedFragments[$fragmentName])) { |
||
175 | return; |
||
176 | } |
||
177 | |||
178 | $comparedFragments[$fragmentName] = true; |
||
179 | |||
180 | $fragment = $this->getValidationContext()->getFragment($fragmentName); |
||
181 | |||
182 | if (null === $fragment) { |
||
183 | return; |
||
184 | } |
||
185 | |||
186 | $contextB = $this->getReferencedFieldsAndFragmentNames($fragment); |
||
187 | |||
188 | $fieldMapB = $contextB->getFieldMap(); |
||
189 | |||
190 | // Do not compare a fragment's fieldMap to itself. |
||
191 | if ($fieldMap === $fieldMapB) { |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | // (D) First collect any conflicts between the provided collection of fields |
||
196 | // and the collection of fields represented by the given fragment. |
||
197 | $this->collectConflictsBetween( |
||
198 | $context, |
||
199 | $fieldMap, |
||
200 | $fieldMapB, |
||
201 | $areMutuallyExclusive |
||
202 | ); |
||
203 | |||
204 | $fragmentNamesB = $contextB->getFragmentNames(); |
||
205 | |||
206 | // (E) Then collect any conflicts between the provided collection of fields |
||
207 | // and any fragment names found in the given fragment. |
||
208 | if (!empty($fragmentNamesB)) { |
||
209 | $fragmentNamesBCount = \count($fragmentNamesB); |
||
210 | |||
211 | /** @noinspection ForeachInvariantsInspection */ |
||
212 | for ($i = 0; $i < $fragmentNamesBCount; $i++) { |
||
213 | $this->collectConflictsBetweenFieldsAndFragment( |
||
214 | $context, |
||
215 | $comparedFragments, |
||
216 | $fieldMap, |
||
217 | $fragmentNamesB[$i], |
||
218 | $areMutuallyExclusive |
||
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( |
||
235 | ComparisonContext $context, |
||
236 | string $fragmentNameA, |
||
237 | string $fragmentNameB, |
||
238 | bool $areMutuallyExclusive |
||
239 | ): void { |
||
240 | // No need to compare a fragment to itself. |
||
241 | if ($fragmentNameA === $fragmentNameB) { |
||
242 | return; |
||
243 | } |
||
244 | |||
245 | // Memoize so two fragments are not compared for conflicts more than once. |
||
246 | if ($this->comparedFragmentPairs->has($fragmentNameA, $fragmentNameB, $areMutuallyExclusive)) { |
||
247 | return; |
||
248 | } |
||
249 | |||
250 | $this->comparedFragmentPairs->add($fragmentNameA, $fragmentNameB, $areMutuallyExclusive); |
||
251 | |||
252 | $fragmentA = $this->getValidationContext()->getFragment($fragmentNameA); |
||
253 | $fragmentB = $this->getValidationContext()->getFragment($fragmentNameB); |
||
254 | |||
255 | if (null === $fragmentA || null === $fragmentB) { |
||
256 | return; |
||
257 | } |
||
258 | |||
259 | $contextA = $this->getReferencedFieldsAndFragmentNames($fragmentA); |
||
260 | $contextB = $this->getReferencedFieldsAndFragmentNames($fragmentB); |
||
261 | |||
262 | // (F) First, collect all conflicts between these two collections of fields |
||
263 | // (not including any nested fragments). |
||
264 | $this->collectConflictsBetween( |
||
265 | $context, |
||
266 | $contextA->getFieldMap(), |
||
267 | $contextB->getFieldMap(), |
||
268 | $areMutuallyExclusive |
||
269 | ); |
||
270 | |||
271 | $fragmentNamesB = $contextB->getFragmentNames(); |
||
272 | |||
273 | // (G) Then collect conflicts between the first fragment and any nested |
||
274 | // fragments spread in the second fragment. |
||
275 | if (!empty($fragmentNamesB)) { |
||
276 | $fragmentNamesBCount = \count($fragmentNamesB); |
||
277 | |||
278 | /** @noinspection ForeachInvariantsInspection */ |
||
279 | for ($j = 0; $j < $fragmentNamesBCount; $j++) { |
||
280 | $this->collectConflictsBetweenFragments( |
||
281 | $context, |
||
282 | $fragmentNameA, |
||
283 | $fragmentNamesB[$j], |
||
284 | $areMutuallyExclusive |
||
285 | ); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | $fragmentNamesA = $contextA->getFragmentNames(); |
||
290 | |||
291 | // (G) Then collect conflicts between the second fragment and any nested |
||
292 | // fragments spread in the first fragment. |
||
293 | if (!empty($fragmentNamesA)) { |
||
294 | $fragmentNamesACount = \count($fragmentNamesA); |
||
295 | |||
296 | /** @noinspection ForeachInvariantsInspection */ |
||
297 | for ($i = 0; $i < $fragmentNamesACount; $i++) { |
||
298 | $this->collectConflictsBetweenFragments( |
||
299 | $context, |
||
300 | $fragmentNamesA[$i], |
||
301 | $fragmentNameB, |
||
302 | $areMutuallyExclusive |
||
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( |
||
322 | ?NamedTypeInterface $parentTypeA, |
||
323 | SelectionSetNode $selectionSetA, |
||
324 | ?NamedTypeInterface $parentTypeB, |
||
325 | SelectionSetNode $selectionSetB, |
||
326 | bool $areMutuallyExclusive |
||
327 | ): array { |
||
328 | $context = new ComparisonContext(); |
||
329 | |||
330 | $contextA = $this->getFieldsAndFragmentNames($selectionSetA, $parentTypeA); |
||
331 | $contextB = $this->getFieldsAndFragmentNames($selectionSetB, $parentTypeB); |
||
332 | $fieldMapA = $contextA->getFieldMap(); |
||
333 | $fieldMapB = $contextB->getFieldMap(); |
||
334 | $fragmentNamesA = $contextA->getFragmentNames(); |
||
335 | $fragmentNamesB = $contextB->getFragmentNames(); |
||
336 | $fragmentNamesACount = \count($fragmentNamesA); |
||
337 | $fragmentNamesBCount = \count($fragmentNamesB); |
||
338 | |||
339 | // (H) First, collect all conflicts between these two collections of field. |
||
340 | $this->collectConflictsBetween( |
||
341 | $context, |
||
342 | $fieldMapA, |
||
343 | $fieldMapB, |
||
344 | $areMutuallyExclusive |
||
345 | ); |
||
346 | |||
347 | // (I) Then collect conflicts between the first collection of fields and |
||
348 | // those referenced by each fragment name associated with the second. |
||
349 | if (!empty($fragmentNamesB)) { |
||
350 | $comparedFragments = []; |
||
351 | |||
352 | /** @noinspection ForeachInvariantsInspection */ |
||
353 | for ($j = 0; $j < $fragmentNamesBCount; $j++) { |
||
354 | $this->collectConflictsBetweenFieldsAndFragment( |
||
355 | $context, |
||
356 | $comparedFragments, |
||
357 | $fieldMapA, |
||
358 | $fragmentNamesB[$j], |
||
359 | $areMutuallyExclusive |
||
360 | ); |
||
361 | } |
||
362 | } |
||
363 | |||
364 | // (I) Then collect conflicts between the second collection of fields and |
||
365 | // those referenced by each fragment name associated with the first. |
||
366 | if (!empty($fragmentNamesA)) { |
||
367 | $comparedFragments = []; |
||
368 | |||
369 | /** @noinspection ForeachInvariantsInspection */ |
||
370 | for ($i = 0; $i < $fragmentNamesACount; $i++) { |
||
371 | $this->collectConflictsBetweenFieldsAndFragment( |
||
372 | $context, |
||
373 | $comparedFragments, |
||
374 | $fieldMapB, |
||
375 | $fragmentNamesA[$i], |
||
376 | $areMutuallyExclusive |
||
377 | ); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** @noinspection ForeachInvariantsInspection */ |
||
382 | for ($i = 0; $i < $fragmentNamesACount; $i++) { |
||
383 | /** @noinspection ForeachInvariantsInspection */ |
||
384 | for ($j = 0; $j < $fragmentNamesBCount; $j++) { |
||
385 | $this->collectConflictsBetweenFragments( |
||
386 | $context, |
||
387 | $fragmentNamesA[$i], |
||
388 | $fragmentNamesB[$j], |
||
389 | $areMutuallyExclusive |
||
390 | ); |
||
391 | } |
||
392 | } |
||
393 | |||
394 | return $context->getConflicts(); |
||
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 |
||
404 | { |
||
405 | // A field map is a keyed collection, where each key represents a response |
||
406 | // name and the value at that key is a list of all fields which provide that |
||
407 | // response name. For every response name, if there are multiple fields, they |
||
408 | // must be compared to find a potential conflict. |
||
409 | foreach ($context->getFieldMap() as $responseName => $fields) { |
||
410 | $fieldsCount = \count($fields); |
||
411 | |||
412 | // This compares every field in the list to every other field in this list |
||
413 | // (except to itself). If the list only has one item, nothing needs to |
||
414 | // be compared. |
||
415 | if ($fieldsCount > 1) { |
||
416 | /** @noinspection ForeachInvariantsInspection */ |
||
417 | for ($i = 0; $i < $fieldsCount; $i++) { |
||
418 | for ($j = $i + 1; $j < $fieldsCount; $j++) { |
||
419 | $conflict = $this->findConflict( |
||
420 | $responseName, |
||
421 | $fields[$i], |
||
422 | $fields[$j], |
||
423 | // within one collection is never mutually exclusive |
||
424 | false/* $areMutuallyExclusive */ |
||
425 | ); |
||
426 | |||
427 | if (null !== $conflict) { |
||
428 | $context->reportConflict($conflict); |
||
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 | for ($i = 0; $i < $fieldsACount; $i++) { |
||
467 | for ($j = 0; $j < $fieldsBCount; $j++) { |
||
468 | $conflict = $this->findConflict( |
||
469 | $responseName, |
||
470 | $fieldA, |
||
471 | $fieldB, |
||
472 | $parentFieldsAreMutuallyExclusive |
||
473 | ); |
||
474 | |||
475 | if (null !== $conflict) { |
||
476 | $context->reportConflict($conflict); |
||
477 | } |
||
478 | } |
||
479 | } |
||
480 | } |
||
481 | } |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Determines if there is a conflict between two particular fields, including |
||
486 | * comparing their sub-fields. |
||
487 | * |
||
488 | * @param string $responseName |
||
489 | * @param FieldContext $fieldA |
||
490 | * @param FieldContext $fieldB |
||
491 | * @param bool $parentFieldsAreMutuallyExclusive |
||
492 | * @return Conflict|null |
||
493 | * @throws InvalidTypeException |
||
494 | */ |
||
495 | protected function findConflict( |
||
496 | string $responseName, |
||
497 | FieldContext $fieldA, |
||
498 | FieldContext $fieldB, |
||
499 | bool $parentFieldsAreMutuallyExclusive |
||
500 | ): ?Conflict { |
||
501 | $parentTypeA = $fieldA->getParentType(); |
||
502 | $parentTypeB = $fieldB->getParentType(); |
||
503 | |||
504 | // If it is known that two fields could not possibly apply at the same |
||
505 | // time, due to the parent types, then it is safe to permit them to diverge |
||
506 | // in aliased field or arguments used as they will not present any ambiguity |
||
507 | // by differing. |
||
508 | // It is known that two parent types could never overlap if they are |
||
509 | // different Object types. Interface or Union types might overlap - if not |
||
510 | // in the current state of the schema, then perhaps in some future version, |
||
511 | // thus may not safely diverge. |
||
512 | $areMutuallyExclusive = $parentFieldsAreMutuallyExclusive |
||
513 | || ($parentTypeA !== $parentTypeB |
||
514 | && $parentTypeA instanceof ObjectType |
||
515 | && $parentTypeB instanceof ObjectType); |
||
516 | |||
517 | $nodeA = $fieldA->getNode(); |
||
518 | $nodeB = $fieldB->getNode(); |
||
519 | |||
520 | if (!$areMutuallyExclusive) { |
||
521 | // Two aliases must refer to the same field. |
||
522 | $nameA = $nodeA->getNameValue(); |
||
523 | $nameB = $nodeB->getNameValue(); |
||
524 | |||
525 | if ($nameA !== $nameB) { |
||
526 | return new Conflict( |
||
527 | $responseName, |
||
528 | sprintf('%s and %s are different fields', $nameA, $nameB), |
||
529 | [$nodeA], |
||
530 | [$nodeB] |
||
531 | ); |
||
532 | } |
||
533 | |||
534 | // Two field calls must have the same arguments. |
||
535 | if (!compareArguments($nodeA->getArguments(), $nodeB->getArguments())) { |
||
536 | return new Conflict( |
||
537 | $responseName, |
||
538 | 'they have differing arguments', |
||
539 | [$nodeA], |
||
540 | [$nodeB] |
||
541 | ); |
||
542 | } |
||
543 | } |
||
544 | |||
545 | $definitionA = $fieldA->getDefinition(); |
||
546 | $definitionB = $fieldB->getDefinition(); |
||
547 | |||
548 | // The return type for each field. |
||
549 | $typeA = null !== $definitionA ? $definitionA->getType() : null; |
||
550 | $typeB = null !== $definitionB ? $definitionB->getType() : null; |
||
551 | |||
552 | if (null !== $typeA && null !== $typeB && compareTypes($typeA, $typeB)) { |
||
553 | return new Conflict( |
||
554 | $responseName, |
||
555 | sprintf('they return conflicting types %s and %s', (string)$typeA, (string)$typeB), |
||
556 | [$nodeA], |
||
557 | [$nodeB] |
||
558 | ); |
||
559 | } |
||
560 | |||
561 | // Collect and compare sub-fields. Use the same "visited fragment names" list |
||
562 | // for both collections so fields in a fragment reference are never |
||
563 | // compared to themselves. |
||
564 | $selectionSetA = $nodeA->getSelectionSet(); |
||
565 | $selectionSetB = $nodeB->getSelectionSet(); |
||
566 | |||
567 | if (null !== $selectionSetA && null !== $selectionSetB) { |
||
568 | $conflicts = $this->findConflictsBetweenSubSelectionSets( |
||
569 | getNamedType($typeA), |
||
570 | $selectionSetA, |
||
571 | getNamedType($typeB), |
||
572 | $selectionSetB, |
||
573 | $areMutuallyExclusive |
||
574 | ); |
||
575 | |||
576 | return $this->subfieldConflicts($conflicts, $responseName, $nodeA, $nodeB); |
||
577 | } |
||
578 | |||
579 | return null; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Given a selection set, return the collection of fields (a mapping of response |
||
584 | * name to field nodes and definitions) as well as a list of fragment names |
||
585 | * referenced via fragment spreads. |
||
586 | * |
||
587 | * @param SelectionSetNode $selectionSet |
||
588 | * @param NamedTypeInterface|null $parentType |
||
589 | * @return ComparisonContext |
||
590 | * @throws InvalidTypeException |
||
591 | */ |
||
592 | protected function getFieldsAndFragmentNames( |
||
593 | SelectionSetNode $selectionSet, |
||
594 | ?NamedTypeInterface $parentType |
||
595 | ): ComparisonContext { |
||
596 | $cached = $this->cachedFieldsAndFragmentNames->get($selectionSet) ?? null; |
||
597 | |||
598 | if (null === $cached) { |
||
599 | $cached = new ComparisonContext(); |
||
600 | |||
601 | $this->collectFieldsAndFragmentNames($cached, $selectionSet, $parentType); |
||
602 | |||
603 | $this->cachedFieldsAndFragmentNames->set($selectionSet, $cached); |
||
604 | } |
||
605 | |||
606 | return $cached; |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * Given a reference to a fragment, return the represented collection of fields |
||
611 | * as well as a list of nested fragment names referenced via fragment spreads. |
||
612 | * |
||
613 | * @param FragmentDefinitionNode $fragment |
||
614 | * @return ComparisonContext |
||
615 | * @throws InvalidTypeException |
||
616 | */ |
||
617 | protected function getReferencedFieldsAndFragmentNames(FragmentDefinitionNode $fragment): ComparisonContext |
||
618 | { |
||
619 | $cached = $this->cachedFieldsAndFragmentNames->get($fragment) ?? null; |
||
620 | |||
621 | if (null !== $cached) { |
||
622 | return $cached; |
||
623 | } |
||
624 | |||
625 | /** @var NamedTypeInterface $fragmentType */ |
||
626 | $fragmentType = typeFromAST($this->getValidationContext()->getSchema(), $fragment->getTypeCondition()); |
||
627 | |||
628 | return $this->getFieldsAndFragmentNames($fragment->getSelectionSet(), $fragmentType); |
||
629 | } |
||
630 | |||
631 | /** |
||
632 | * @param ComparisonContext $context |
||
633 | * @param SelectionSetNode $selectionSet |
||
634 | * @param NamedTypeInterface|null $parentType |
||
635 | * @throws InvalidTypeException |
||
636 | */ |
||
637 | protected function collectFieldsAndFragmentNames( |
||
638 | ComparisonContext $context, |
||
639 | SelectionSetNode $selectionSet, |
||
640 | ?NamedTypeInterface $parentType |
||
641 | ): void { |
||
642 | foreach ($selectionSet->getSelections() as $selection) { |
||
643 | if ($selection instanceof FieldNode) { |
||
644 | $definition = ($parentType instanceof ObjectType || $parentType instanceof InterfaceType) |
||
645 | ? $parentType->getFields()[$selection->getNameValue()] |
||
646 | : null; |
||
647 | |||
648 | $context->registerField(new FieldContext($parentType, $selection, $definition)); |
||
|
|||
649 | } elseif ($selection instanceof FragmentSpreadNode) { |
||
650 | $context->registerFragment($selection); |
||
651 | } elseif ($selection instanceof InlineFragmentNode) { |
||
652 | $typeCondition = $selection->getTypeCondition(); |
||
653 | |||
654 | $inlineFragmentType = null !== $typeCondition |
||
655 | ? typeFromAST($this->getValidationContext()->getSchema(), $typeCondition) |
||
656 | : $parentType; |
||
657 | |||
658 | $this->collectFieldsAndFragmentNames($context, $selection->getSelectionSet(), $inlineFragmentType); |
||
659 | } |
||
660 | } |
||
661 | } |
||
662 | |||
663 | /** |
||
664 | * Given a series of Conflicts which occurred between two sub-fields, generate |
||
665 | * a single Conflict. |
||
666 | * |
||
667 | * @param array|Conflict[] $conflicts |
||
668 | * @param string $responseName |
||
669 | * @param FieldNode $nodeA |
||
670 | * @param FieldNode $nodeB |
||
671 | * @return Conflict|null |
||
672 | */ |
||
673 | protected function subfieldConflicts( |
||
694 | ); |
||
695 | } |
||
696 | } |
||
697 |