Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

NoUnusedFragments::getVisitorpublicWC 1.0

Method of the class: NoUnusedFragments{}

No Hooks.

Returns

null. Nothing (null).

Usage

$NoUnusedFragments = new NoUnusedFragments();
$NoUnusedFragments->getVisitor( $context ): array;
$context(QueryValidationContext) (required)
.

NoUnusedFragments::getVisitor() code WC 10.9.1

public function getVisitor(QueryValidationContext $context): array
{
    $this->operationDefs = [];
    $this->fragmentDefs = [];

    return [
        NodeKind::OPERATION_DEFINITION => function ($node): VisitorOperation {
            $this->operationDefs[] = $node;

            return Visitor::skipNode();
        },
        NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $def): VisitorOperation {
            $this->fragmentDefs[] = $def;

            return Visitor::skipNode();
        },
        NodeKind::DOCUMENT => [
            'leave' => function () use ($context): void {
                $fragmentNameUsed = [];

                foreach ($this->operationDefs as $operation) {
                    foreach ($context->getRecursivelyReferencedFragments($operation) as $fragment) {
                        $fragmentNameUsed[$fragment->name->value] = true;
                    }
                }

                foreach ($this->fragmentDefs as $fragmentDef) {
                    $fragName = $fragmentDef->name->value;

                    if (! isset($fragmentNameUsed[$fragName])) {
                        $context->reportError(new Error(
                            static::unusedFragMessage($fragName),
                            [$fragmentDef]
                        ));
                    }
                }
            },
        ],
    ];
}