Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

UniqueInputFieldNames::getASTVisitorpublicWC 1.0

Method of the class: UniqueInputFieldNames{}

No Hooks.

Returns

null. Nothing (null).

Usage

$UniqueInputFieldNames = new UniqueInputFieldNames();
$UniqueInputFieldNames->getASTVisitor( $context ): array;
$context(ValidationContext) (required)
.

UniqueInputFieldNames::getASTVisitor() code WC 10.9.1

public function getASTVisitor(ValidationContext $context): array
{
    $this->knownNames = [];
    $this->knownNameStack = [];

    return [
        NodeKind::OBJECT => [
            'enter' => function (): void {
                $this->knownNameStack[] = $this->knownNames;
                $this->knownNames = [];
            },
            'leave' => function (): void {
                $knownNames = array_pop($this->knownNameStack);
                assert(is_array($knownNames), 'should not happen if the visitor works correctly');

                $this->knownNames = $knownNames;
            },
        ],
        NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) use ($context): VisitorOperation {
            $fieldName = $node->name->value;

            if (isset($this->knownNames[$fieldName])) {
                $context->reportError(new Error(
                    static::duplicateInputFieldMessage($fieldName),
                    [$this->knownNames[$fieldName], $node->name]
                ));
            } else {
                $this->knownNames[$fieldName] = $node->name;
            }

            return Visitor::skipNode();
        },
    ];
}