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
UniqueInputFieldNames.php
77 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\NameNode; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\ObjectFieldNode; |
| 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 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\SDLValidationContext; |
| 13 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\ValidationContext; |
| 14 | |
| 15 | /** |
| 16 | * @phpstan-import-type VisitorArray from Visitor |
| 17 | */ |
| 18 | class UniqueInputFieldNames extends ValidationRule |
| 19 | { |
| 20 | /** @var array<string, NameNode> */ |
| 21 | protected array $knownNames; |
| 22 | |
| 23 | /** @var array<array<string, NameNode>> */ |
| 24 | protected array $knownNameStack; |
| 25 | |
| 26 | public function getVisitor(QueryValidationContext $context): array |
| 27 | { |
| 28 | return $this->getASTVisitor($context); |
| 29 | } |
| 30 | |
| 31 | public function getSDLVisitor(SDLValidationContext $context): array |
| 32 | { |
| 33 | return $this->getASTVisitor($context); |
| 34 | } |
| 35 | |
| 36 | /** @phpstan-return VisitorArray */ |
| 37 | public function getASTVisitor(ValidationContext $context): array |
| 38 | { |
| 39 | $this->knownNames = []; |
| 40 | $this->knownNameStack = []; |
| 41 | |
| 42 | return [ |
| 43 | NodeKind::OBJECT => [ |
| 44 | 'enter' => function (): void { |
| 45 | $this->knownNameStack[] = $this->knownNames; |
| 46 | $this->knownNames = []; |
| 47 | }, |
| 48 | 'leave' => function (): void { |
| 49 | $knownNames = array_pop($this->knownNameStack); |
| 50 | assert(is_array($knownNames), 'should not happen if the visitor works correctly'); |
| 51 | |
| 52 | $this->knownNames = $knownNames; |
| 53 | }, |
| 54 | ], |
| 55 | NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) use ($context): VisitorOperation { |
| 56 | $fieldName = $node->name->value; |
| 57 | |
| 58 | if (isset($this->knownNames[$fieldName])) { |
| 59 | $context->reportError(new Error( |
| 60 | static::duplicateInputFieldMessage($fieldName), |
| 61 | [$this->knownNames[$fieldName], $node->name] |
| 62 | )); |
| 63 | } else { |
| 64 | $this->knownNames[$fieldName] = $node->name; |
| 65 | } |
| 66 | |
| 67 | return Visitor::skipNode(); |
| 68 | }, |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | public static function duplicateInputFieldMessage(string $fieldName): string |
| 73 | { |
| 74 | return "There can be only one input field named \"{$fieldName}\"."; |
| 75 | } |
| 76 | } |
| 77 |