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
NoUnusedFragments.php
67 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\NodeKind; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\OperationDefinitionNode; |
| 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 NoUnusedFragments extends ValidationRule |
| 14 | { |
| 15 | /** @var array<int, OperationDefinitionNode> */ |
| 16 | protected array $operationDefs; |
| 17 | |
| 18 | /** @var array<int, FragmentDefinitionNode> */ |
| 19 | protected array $fragmentDefs; |
| 20 | |
| 21 | public function getVisitor(QueryValidationContext $context): array |
| 22 | { |
| 23 | $this->operationDefs = []; |
| 24 | $this->fragmentDefs = []; |
| 25 | |
| 26 | return [ |
| 27 | NodeKind::OPERATION_DEFINITION => function ($node): VisitorOperation { |
| 28 | $this->operationDefs[] = $node; |
| 29 | |
| 30 | return Visitor::skipNode(); |
| 31 | }, |
| 32 | NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $def): VisitorOperation { |
| 33 | $this->fragmentDefs[] = $def; |
| 34 | |
| 35 | return Visitor::skipNode(); |
| 36 | }, |
| 37 | NodeKind::DOCUMENT => [ |
| 38 | 'leave' => function () use ($context): void { |
| 39 | $fragmentNameUsed = []; |
| 40 | |
| 41 | foreach ($this->operationDefs as $operation) { |
| 42 | foreach ($context->getRecursivelyReferencedFragments($operation) as $fragment) { |
| 43 | $fragmentNameUsed[$fragment->name->value] = true; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | foreach ($this->fragmentDefs as $fragmentDef) { |
| 48 | $fragName = $fragmentDef->name->value; |
| 49 | |
| 50 | if (! isset($fragmentNameUsed[$fragName])) { |
| 51 | $context->reportError(new Error( |
| 52 | static::unusedFragMessage($fragName), |
| 53 | [$fragmentDef] |
| 54 | )); |
| 55 | } |
| 56 | } |
| 57 | }, |
| 58 | ], |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | public static function unusedFragMessage(string $fragName): string |
| 63 | { |
| 64 | return "Fragment \"{$fragName}\" is never used."; |
| 65 | } |
| 66 | } |
| 67 |