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
NoUndefinedVariables.php
61 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 | /** |
| 12 | * A Automattic\WooCommerce\Vendor\GraphQL operation is only valid if all variables encountered, both directly |
| 13 | * and via fragment spreads, are defined by that operation. |
| 14 | */ |
| 15 | class NoUndefinedVariables extends ValidationRule |
| 16 | { |
| 17 | public function getVisitor(QueryValidationContext $context): array |
| 18 | { |
| 19 | /** @var array<string, true> $variableNameDefined */ |
| 20 | $variableNameDefined = []; |
| 21 | |
| 22 | return [ |
| 23 | NodeKind::OPERATION_DEFINITION => [ |
| 24 | 'enter' => static function () use (&$variableNameDefined): void { |
| 25 | $variableNameDefined = []; |
| 26 | }, |
| 27 | 'leave' => static function (OperationDefinitionNode $operation) use (&$variableNameDefined, $context): void { |
| 28 | $usages = $context->getRecursiveVariableUsages($operation); |
| 29 | |
| 30 | foreach ($usages as $usage) { |
| 31 | $node = $usage['node']; |
| 32 | $varName = $node->name->value; |
| 33 | |
| 34 | if (! isset($variableNameDefined[$varName])) { |
| 35 | $context->reportError(new Error( |
| 36 | static::undefinedVarMessage( |
| 37 | $varName, |
| 38 | $operation->name !== null |
| 39 | ? $operation->name->value |
| 40 | : null |
| 41 | ), |
| 42 | [$node, $operation] |
| 43 | )); |
| 44 | } |
| 45 | } |
| 46 | }, |
| 47 | ], |
| 48 | NodeKind::VARIABLE_DEFINITION => static function (VariableDefinitionNode $def) use (&$variableNameDefined): void { |
| 49 | $variableNameDefined[$def->variable->name->value] = true; |
| 50 | }, |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | public static function undefinedVarMessage(string $varName, ?string $opName): string |
| 55 | { |
| 56 | return $opName === null |
| 57 | ? "Variable \"\${$varName}\" is not defined by operation \"{$opName}\"." |
| 58 | : "Variable \"\${$varName}\" is not defined."; |
| 59 | } |
| 60 | } |
| 61 |