Completed
Pull Request — master (#59)
by Christoffer
03:01 queued 52s
created

getDirectiveLocationFromASTPath()   C

Complexity

Conditions 26
Paths 20

Size

Total Lines 64
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 43
nc 20
nop 1
dl 0
loc 64
rs 5.84
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\AST\DirectiveLocationEnum;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\AST\DirectiveLocationEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Digia\GraphQL\Language\AST\Node\DirectiveNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\AST\Node\DirectiveNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Digia\GraphQL\Language\AST\Node\EnumTypeDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...\EnumTypeDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Digia\GraphQL\Language\AST\Node\EnumTypeExtensionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...e\EnumTypeExtensionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Digia\GraphQL\Language\AST\Node\EnumValueDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...EnumValueDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Digia\GraphQL\Language\AST\Node\FieldDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...ode\FieldDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Digia\GraphQL\Language\AST\Node\FieldNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\AST\Node\FieldNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...\FragmentDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Digia\GraphQL\Language\AST\Node\FragmentSpreadNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\AST\Node\FragmentSpreadNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Digia\GraphQL\Language\AST\Node\InlineFragmentNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\AST\Node\InlineFragmentNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Digia\GraphQL\Language\AST\Node\InputObjectTypeDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...bjectTypeDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Digia\GraphQL\Language\AST\Node\InputObjectTypeExtensionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...ObjectTypeExtensionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Digia\GraphQL\Language\AST\Node\InputValueDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...nputValueDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Digia\GraphQL\Language\AST\Node\InterfaceTypeDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...rfaceTypeDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Digia\GraphQL\Language\AST\Node\InterfaceTypeExtensionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...erfaceTypeExtensionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Digia\GraphQL\Language\AST\Node\NodeInterface;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\AST\Node\NodeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Digia\GraphQL\Language\AST\Node\ObjectTypeDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...bjectTypeDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Digia\GraphQL\Language\AST\Node\ObjectTypeExtensionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...ObjectTypeExtensionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Digia\GraphQL\Language\AST\Node\OperationDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...OperationDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Digia\GraphQL\Language\AST\Node\ScalarTypeDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...calarTypeDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Digia\GraphQL\Language\AST\Node\ScalarTypeExtensionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...ScalarTypeExtensionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Digia\GraphQL\Language\AST\Node\SchemaDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...de\SchemaDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Digia\GraphQL\Language\AST\Node\UnionTypeDefinitionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...UnionTypeDefinitionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use Digia\GraphQL\Language\AST\Node\UnionTypeExtensionNode;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...\UnionTypeExtensionNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Digia\GraphQL\Language\AST\Visitor\AcceptsVisitorsInterface;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Language\A...cceptsVisitorsInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Digia\GraphQL\Type\Definition\Directive;
32
use function Digia\GraphQL\Util\find;
33
use function Digia\GraphQL\Validation\misplacedDirectiveMessage;
34
use function Digia\GraphQL\Validation\unknownDirectiveMessage;
35
36
/**
37
 * Known directives
38
 *
39
 * A GraphQL document is only valid if all `@directives` are known by the
40
 * schema and legally positioned.
41
 */
42
class KnownDirectivesRule extends AbstractRule
43
{
44
    /**
45
     * @inheritdoc
46
     */
47
    public function enterNode(NodeInterface $node): ?NodeInterface
48
    {
49
        if ($node instanceof DirectiveNode) {
50
            /** @var Directive $directiveDefinition */
51
            $directiveDefinition = find(
52
                $this->validationContext->getSchema()->getDirectives(),
53
                function (Directive $definition) use ($node) {
54
                    return $definition->getName() === $node->getNameValue();
55
                }
56
            );
57
58
            if (null == $directiveDefinition) {
59
                $this->validationContext->reportError(
60
                    new ValidationException(unknownDirectiveMessage((string)$node), [$node])
61
                );
62
63
                return $node;
64
            }
65
66
            $location = $this->getDirectiveLocationFromASTPath($node);
67
68
            if (null !== $location && !in_array($location, $directiveDefinition->getLocations())) {
69
                $this->validationContext->reportError(
70
                    new ValidationException(misplacedDirectiveMessage((string)$node, $location), [$node])
71
                );
72
            }
73
        }
74
75
        return $node;
76
    }
77
78
    /**
79
     * @param NodeInterface|AcceptsVisitorsInterface $node
80
     * @return string|null
81
     */
82
    protected function getDirectiveLocationFromASTPath(NodeInterface $node): ?string
83
    {
84
        /** @var NodeInterface $appliedTo */
85
        $appliedTo = $node->getAncestor();
86
87
        if ($appliedTo instanceof OperationDefinitionNode) {
88
            switch ($appliedTo->getOperation()) {
89
                case 'query':
90
                    return DirectiveLocationEnum::QUERY;
91
                case 'mutation':
92
                    return DirectiveLocationEnum::MUTATION;
93
                case 'subscription':
94
                    return DirectiveLocationEnum::SUBSCRIPTION;
95
                default:
96
                    return null;
97
            }
98
        }
99
        if ($appliedTo instanceof FieldNode) {
100
            return DirectiveLocationEnum::FIELD;
101
        }
102
        if ($appliedTo instanceof FragmentSpreadNode) {
103
            return DirectiveLocationEnum::FRAGMENT_SPREAD;
104
        }
105
        if ($appliedTo instanceof InlineFragmentNode) {
106
            return DirectiveLocationEnum::INLINE_FRAGMENT;
107
        }
108
        if ($appliedTo instanceof FragmentDefinitionNode) {
109
            return DirectiveLocationEnum::FRAGMENT_DEFINITION;
110
        }
111
        if ($appliedTo instanceof SchemaDefinitionNode) {
112
            return DirectiveLocationEnum::SCHEMA;
113
        }
114
        if ($appliedTo instanceof ScalarTypeDefinitionNode || $appliedTo instanceof ScalarTypeExtensionNode) {
115
            return DirectiveLocationEnum::SCALAR;
116
        }
117
        if ($appliedTo instanceof ObjectTypeDefinitionNode || $appliedTo instanceof ObjectTypeExtensionNode) {
118
            return DirectiveLocationEnum::OBJECT;
119
        }
120
        if ($appliedTo instanceof FieldDefinitionNode) {
121
            return DirectiveLocationEnum::FIELD_DEFINITION;
122
        }
123
        if ($appliedTo instanceof InterfaceTypeDefinitionNode || $appliedTo instanceof InterfaceTypeExtensionNode) {
124
            return DirectiveLocationEnum::INTERFACE;
125
        }
126
        if ($appliedTo instanceof UnionTypeDefinitionNode || $appliedTo instanceof UnionTypeExtensionNode) {
127
            return DirectiveLocationEnum::UNION;
128
        }
129
        if ($appliedTo instanceof EnumTypeDefinitionNode || $appliedTo instanceof EnumTypeExtensionNode) {
130
            return DirectiveLocationEnum::ENUM;
131
        }
132
        if ($appliedTo instanceof EnumValueDefinitionNode) {
133
            return DirectiveLocationEnum::ENUM_VALUE;
134
        }
135
        if ($appliedTo instanceof InputObjectTypeDefinitionNode || $appliedTo instanceof InputObjectTypeExtensionNode) {
136
            return DirectiveLocationEnum::INPUT_OBJECT;
137
        }
138
        if ($appliedTo instanceof InputValueDefinitionNode) {
139
            $parentNode = $node->getAncestor(2);
140
            return $parentNode instanceof InputObjectTypeDefinitionNode
141
                ? DirectiveLocationEnum::INPUT_FIELD_DEFINITION
142
                : DirectiveLocationEnum::ARGUMENT_DEFINITION;
143
        }
144
145
        return null;
146
    }
147
}
148