Issues (167)

Rule/UniqueDirectivesPerLocationRule.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Language\Node\DirectivesAwareInterface;
6
use Digia\GraphQL\Language\Node\NodeInterface;
7
use Digia\GraphQL\Language\Visitor\VisitorResult;
8
use Digia\GraphQL\Validation\ValidationException;
9
use function Digia\GraphQL\Validation\duplicateDirectiveMessage;
10
11
/**
12
 * Unique directive names per location
13
 *
14
 * A GraphQL document is only valid if all directives at a given location
15
 * are uniquely named.
16
 */
17
class UniqueDirectivesPerLocationRule extends AbstractRule
18
{
19
    /**
20
     * @inheritdoc
21
     */
22
    public function enterNode(NodeInterface $node): VisitorResult
23
    {
24
        if ($node instanceof DirectivesAwareInterface) {
25
            $directives      = $node->getDirectives();
26
            $knownDirectives = [];
27
28
            foreach ($directives as $directive) {
29
                $directiveName = $directive->getNameValue();
30
31
                if (isset($knownDirectives[$directiveName])) {
32
                    $this->context->reportError(
33
                        new ValidationException(
34
                            duplicateDirectiveMessage($directiveName),
0 ignored issues
show
It seems like $directiveName can also be of type null; however, parameter $directiveName of Digia\GraphQL\Validation...icateDirectiveMessage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
                            duplicateDirectiveMessage(/** @scrutinizer ignore-type */ $directiveName),
Loading history...
35
                            [$knownDirectives[$directiveName], $directive]
36
                        )
37
                    );
38
                } else {
39
                    $knownDirectives[$directiveName] = $directive;
40
                }
41
            }
42
        }
43
44
        return new VisitorResult($node);
45
    }
46
}
47