This commit is contained in:
Kilian Hofmann
2021-06-01 19:56:34 +02:00
parent e74df463f2
commit 499abe195e
284 changed files with 55166 additions and 0 deletions
@@ -0,0 +1,69 @@
<?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\Validator\ValidationContext;
use function sprintf;
class NoUnusedFragments extends ValidationRule
{
/** @var OperationDefinitionNode[] */
public $operationDefs;
/** @var FragmentDefinitionNode[] */
public $fragmentDefs;
public function getVisitor(ValidationContext $context)
{
$this->operationDefs = [];
$this->fragmentDefs = [];
return [
NodeKind::OPERATION_DEFINITION => function ($node) {
$this->operationDefs[] = $node;
return Visitor::skipNode();
},
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $def) {
$this->fragmentDefs[] = $def;
return Visitor::skipNode();
},
NodeKind::DOCUMENT => [
'leave' => function () use ($context) {
$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 (! empty($fragmentNameUsed[$fragName])) {
continue;
}
$context->reportError(new Error(
self::unusedFragMessage($fragName),
[$fragmentDef]
));
}
},
],
];
}
public static function unusedFragMessage($fragName)
{
return sprintf('Fragment "%s" is never used.', $fragName);
}
}