Version 4.6.0
This commit is contained in:
1 parent
f79dcf067a
commit
517a5e1f70
2036 files changed
+110041
-26162
No files matched your search
@@ -1,39 +1,44 @@
|
||||
<?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\ValidationContext;
|
||||
use function sprintf;
|
||||
|
||||
class NoUnusedFragments extends AbstractValidationRule
|
||||
class NoUnusedFragments extends ValidationRule
|
||||
{
|
||||
static function unusedFragMessage($fragName)
|
||||
{
|
||||
return "Fragment \"$fragName\" is never used.";
|
||||
}
|
||||
|
||||
/** @var OperationDefinitionNode[] */
|
||||
public $operationDefs;
|
||||
|
||||
/** @var FragmentDefinitionNode[] */
|
||||
public $fragmentDefs;
|
||||
|
||||
public function getVisitor(ValidationContext $context)
|
||||
{
|
||||
$this->operationDefs = [];
|
||||
$this->fragmentDefs = [];
|
||||
$this->fragmentDefs = [];
|
||||
|
||||
return [
|
||||
NodeKind::OPERATION_DEFINITION => function($node) {
|
||||
NodeKind::OPERATION_DEFINITION => function ($node) : VisitorOperation {
|
||||
$this->operationDefs[] = $node;
|
||||
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $def) {
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $def) : VisitorOperation {
|
||||
$this->fragmentDefs[] = $def;
|
||||
|
||||
return Visitor::skipNode();
|
||||
},
|
||||
NodeKind::DOCUMENT => [
|
||||
'leave' => function() use ($context) {
|
||||
NodeKind::DOCUMENT => [
|
||||
'leave' => function () use ($context) : void {
|
||||
$fragmentNameUsed = [];
|
||||
|
||||
foreach ($this->operationDefs as $operation) {
|
||||
@@ -44,15 +49,22 @@ class NoUnusedFragments extends AbstractValidationRule
|
||||
|
||||
foreach ($this->fragmentDefs as $fragmentDef) {
|
||||
$fragName = $fragmentDef->name->value;
|
||||
if (empty($fragmentNameUsed[$fragName])) {
|
||||
$context->reportError(new Error(
|
||||
self::unusedFragMessage($fragName),
|
||||
[ $fragmentDef ]
|
||||
));
|
||||
if ($fragmentNameUsed[$fragName] ?? false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context->reportError(new Error(
|
||||
self::unusedFragMessage($fragName),
|
||||
[$fragmentDef]
|
||||
));
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function unusedFragMessage($fragName)
|
||||
{
|
||||
return sprintf('Fragment "%s" is never used.', $fragName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user