![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/webonyx/graphql-php/src/Validator/Rules/ |
<?php declare(strict_types=1); namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Language\AST\FragmentDefinitionNode; use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\OperationDefinitionNode; use GraphQL\Language\Visitor; use GraphQL\Language\VisitorOperation; use GraphQL\Validator\QueryValidationContext; class NoUnusedFragments extends ValidationRule { /** @var array<int, OperationDefinitionNode> */ protected array $operationDefs; /** @var array<int, FragmentDefinitionNode> */ protected array $fragmentDefs; 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] )); } } }, ], ]; } public static function unusedFragMessage(string $fragName): string { return "Fragment \"{$fragName}\" is never used."; } }