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
NoUnusedVariables.php
62 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\NodeKind; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\OperationDefinitionNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\VariableDefinitionNode; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext; |
| 10 | |
| 11 | class NoUnusedVariables extends ValidationRule |
| 12 | { |
| 13 | /** @var array<int, VariableDefinitionNode> */ |
| 14 | protected array $variableDefs; |
| 15 | |
| 16 | public function getVisitor(QueryValidationContext $context): array |
| 17 | { |
| 18 | $this->variableDefs = []; |
| 19 | |
| 20 | return [ |
| 21 | NodeKind::OPERATION_DEFINITION => [ |
| 22 | 'enter' => function (): void { |
| 23 | $this->variableDefs = []; |
| 24 | }, |
| 25 | 'leave' => function (OperationDefinitionNode $operation) use ($context): void { |
| 26 | $variableNameUsed = []; |
| 27 | $usages = $context->getRecursiveVariableUsages($operation); |
| 28 | $opName = $operation->name !== null |
| 29 | ? $operation->name->value |
| 30 | : null; |
| 31 | |
| 32 | foreach ($usages as $usage) { |
| 33 | $node = $usage['node']; |
| 34 | $variableNameUsed[$node->name->value] = true; |
| 35 | } |
| 36 | |
| 37 | foreach ($this->variableDefs as $variableDef) { |
| 38 | $variableName = $variableDef->variable->name->value; |
| 39 | |
| 40 | if (! isset($variableNameUsed[$variableName])) { |
| 41 | $context->reportError(new Error( |
| 42 | static::unusedVariableMessage($variableName, $opName), |
| 43 | [$variableDef] |
| 44 | )); |
| 45 | } |
| 46 | } |
| 47 | }, |
| 48 | ], |
| 49 | NodeKind::VARIABLE_DEFINITION => function ($def): void { |
| 50 | $this->variableDefs[] = $def; |
| 51 | }, |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | public static function unusedVariableMessage(string $varName, ?string $opName = null): string |
| 56 | { |
| 57 | return $opName !== null |
| 58 | ? "Variable \"\${$varName}\" is never used in operation \"{$opName}\"." |
| 59 | : "Variable \"\${$varName}\" is never used."; |
| 60 | } |
| 61 | } |
| 62 |