CustomValidationRule.php
2 months ago
DisableIntrospection.php
2 months ago
ExecutableDefinitions.php
2 months ago
FieldsOnCorrectType.php
2 months ago
FragmentsOnCompositeTypes.php
2 months ago
KnownArgumentNames.php
2 months ago
KnownArgumentNamesOnDirectives.php
2 months ago
KnownDirectives.php
2 months ago
KnownFragmentNames.php
2 months ago
KnownTypeNames.php
2 months ago
LoneAnonymousOperation.php
2 months ago
LoneSchemaDefinition.php
2 months ago
NoFragmentCycles.php
2 months ago
NoUndefinedVariables.php
2 months ago
NoUnusedFragments.php
2 months ago
NoUnusedVariables.php
2 months ago
OneOfInputObjectsRule.php
2 months ago
OverlappingFieldsCanBeMerged.php
2 months ago
PossibleFragmentSpreads.php
2 months ago
PossibleTypeExtensions.php
2 months ago
ProvidedRequiredArguments.php
2 months ago
ProvidedRequiredArgumentsOnDirectives.php
2 months ago
QueryComplexity.php
2 months ago
QueryDepth.php
2 months ago
QuerySecurityRule.php
2 months ago
ScalarLeafs.php
2 months ago
SingleFieldSubscription.php
2 months ago
UniqueArgumentDefinitionNames.php
2 months ago
UniqueArgumentNames.php
2 months ago
UniqueDirectiveNames.php
2 months ago
UniqueDirectivesPerLocation.php
2 months ago
UniqueEnumValueNames.php
2 months ago
UniqueFieldDefinitionNames.php
2 months ago
UniqueFragmentNames.php
2 months ago
UniqueInputFieldNames.php
2 months ago
UniqueOperationNames.php
2 months ago
UniqueOperationTypes.php
2 months ago
UniqueTypeNames.php
2 months ago
UniqueVariableNames.php
2 months ago
ValidationRule.php
2 months ago
ValuesOfCorrectType.php
2 months ago
VariablesAreInputTypes.php
2 months ago
VariablesInAllowedPosition.php
2 months ago
NoFragmentCycles.php
102 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\Error; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FragmentDefinitionNode; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FragmentSpreadNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Language\VisitorOperation; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext; |
| 12 | |
| 13 | class NoFragmentCycles extends ValidationRule |
| 14 | { |
| 15 | /** @var array<string, bool> */ |
| 16 | protected array $visitedFrags; |
| 17 | |
| 18 | /** @var array<int, FragmentSpreadNode> */ |
| 19 | protected array $spreadPath; |
| 20 | |
| 21 | /** @var array<string, int|null> */ |
| 22 | protected array $spreadPathIndexByName; |
| 23 | |
| 24 | public function getVisitor(QueryValidationContext $context): array |
| 25 | { |
| 26 | // Tracks already visited fragments to maintain O(N) and to ensure that cycles |
| 27 | // are not redundantly reported. |
| 28 | $this->visitedFrags = []; |
| 29 | |
| 30 | // Array of AST nodes used to produce meaningful errors |
| 31 | $this->spreadPath = []; |
| 32 | |
| 33 | // Position in the spread path |
| 34 | $this->spreadPathIndexByName = []; |
| 35 | |
| 36 | return [ |
| 37 | NodeKind::OPERATION_DEFINITION => static fn (): VisitorOperation => Visitor::skipNode(), |
| 38 | NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context): VisitorOperation { |
| 39 | $this->detectCycleRecursive($node, $context); |
| 40 | |
| 41 | return Visitor::skipNode(); |
| 42 | }, |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | protected function detectCycleRecursive(FragmentDefinitionNode $fragment, QueryValidationContext $context): void |
| 47 | { |
| 48 | if (isset($this->visitedFrags[$fragment->name->value])) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $fragmentName = $fragment->name->value; |
| 53 | $this->visitedFrags[$fragmentName] = true; |
| 54 | |
| 55 | $spreadNodes = $context->getFragmentSpreads($fragment); |
| 56 | |
| 57 | if ($spreadNodes === []) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $this->spreadPathIndexByName[$fragmentName] = count($this->spreadPath); |
| 62 | |
| 63 | foreach ($spreadNodes as $spreadNode) { |
| 64 | $spreadName = $spreadNode->name->value; |
| 65 | $cycleIndex = $this->spreadPathIndexByName[$spreadName] ?? null; |
| 66 | |
| 67 | $this->spreadPath[] = $spreadNode; |
| 68 | if ($cycleIndex === null) { |
| 69 | $spreadFragment = $context->getFragment($spreadName); |
| 70 | if ($spreadFragment !== null) { |
| 71 | $this->detectCycleRecursive($spreadFragment, $context); |
| 72 | } |
| 73 | } else { |
| 74 | $cyclePath = array_slice($this->spreadPath, $cycleIndex); |
| 75 | $fragmentNames = []; |
| 76 | foreach (array_slice($cyclePath, 0, -1) as $frag) { |
| 77 | $fragmentNames[] = $frag->name->value; |
| 78 | } |
| 79 | |
| 80 | $context->reportError(new Error( |
| 81 | static::cycleErrorMessage($spreadName, $fragmentNames), |
| 82 | $cyclePath |
| 83 | )); |
| 84 | } |
| 85 | |
| 86 | array_pop($this->spreadPath); |
| 87 | } |
| 88 | |
| 89 | $this->spreadPathIndexByName[$fragmentName] = null; |
| 90 | } |
| 91 | |
| 92 | /** @param array<string> $spreadNames */ |
| 93 | public static function cycleErrorMessage(string $fragName, array $spreadNames = []): string |
| 94 | { |
| 95 | $via = $spreadNames === [] |
| 96 | ? '' |
| 97 | : ' via ' . implode(', ', $spreadNames); |
| 98 | |
| 99 | return "Cannot spread fragment \"{$fragName}\" within itself{$via}."; |
| 100 | } |
| 101 | } |
| 102 |