Passed
Pull Request — master (#45)
by Christoffer
02:10
created

KnownArgumentNamesRule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B enterNode() 0 54 8
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\GraphQLError;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Error\GraphQLError 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...
6
use Digia\GraphQL\Language\AST\Node\ArgumentNode;
7
use Digia\GraphQL\Language\AST\Node\DirectiveNode;
8
use Digia\GraphQL\Language\AST\Node\FieldNode;
9
use Digia\GraphQL\Language\AST\Node\NodeInterface;
10
use Digia\GraphQL\Type\Definition\Argument;
11
use function Digia\GraphQL\Util\suggestionList;
12
13
class KnownArgumentNamesRule extends AbstractRule
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function enterNode(NodeInterface $node): ?NodeInterface
19
    {
20
        if ($node instanceof ArgumentNode) {
21
            $argumentDefinition = $this->context->getArgument();
22
23
            if (null === $argumentDefinition) {
24
                $argumentOf = $node->getClosestAncestor();
25
                if ($argumentOf instanceof FieldNode) {
26
                    $fieldDefinition = $this->context->getFieldDefinition();
27
                    $parentType      = $this->context->getParentType();
28
29
                    if (null !== $fieldDefinition && null !== $parentType) {
30
                        $this->context->reportError(
31
                            new GraphQLError(
32
                                unknownArgumentMessage(
33
                                    $node->getNameValue(),
34
                                    $fieldDefinition->getName(),
35
                                    $parentType->getName(),
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Digia\GraphQL\Type\Definition\TypeInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Digia\GraphQL\Type\Defin...n\AbstractTypeInterface or Digia\GraphQL\Type\Defin...n\WrappingTypeInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

35
                                    $parentType->/** @scrutinizer ignore-call */ 
36
                                                 getName(),
Loading history...
36
                                    suggestionList(
37
                                        $node->getNameValue(),
38
                                        array_map(function (Argument $argument) {
39
                                            return $argument->getName();
40
                                        }, $fieldDefinition->getArguments())
41
                                    )
42
                                ),
43
                                [$node]
44
                            )
45
                        );
46
                    }
47
                } elseif ($argumentOf instanceof DirectiveNode) {
48
                    $directive = $this->context->getDirective();
49
50
                    if (null !== $directive) {
51
                        $this->context->reportError(
52
                            new GraphQLError(
53
                                unknownDirectiveArgumentMessage(
54
                                    $node->getNameValue(),
55
                                    $directive->getName(),
56
                                    suggestionList(
57
                                        $node->getNameValue(),
58
                                        array_map(function (Argument $argument) {
59
                                            return $argument->getName();
60
                                        }, $directive->getArguments())
61
                                    )
62
                                ),
63
                                [$node]
64
                            )
65
                        );
66
                    }
67
                }
68
            }
69
        }
70
71
        return $node;
72
    }
73
}
74