Completed
Push — master ( 0da8e6...403be5 )
by Christoffer
06:22 queued 03:31
created

validateFragementNode()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
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...
7
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...
8
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...
9
use Digia\GraphQL\Type\Definition\CompositeTypeInterface;
10
use function Digia\GraphQL\Util\typeFromAST;
11
use function Digia\GraphQL\Validation\fragmentOnNonCompositeMessage;
12
use function Digia\GraphQL\Validation\inlineFragmentOnNonCompositeMessage;
13
14
15
/**
16
 * Fragments on composite type
17
 *
18
 * Fragments use a type condition to determine if they apply, since fragments
19
 * can only be spread into a composite type (object, interface, or union), the
20
 * type condition must also be a composite type.
21
 */
22
class FragmentsOnCompositeTypesRule extends AbstractRule
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    public function enterNode(NodeInterface $node): ?NodeInterface
28
    {
29
        if ($node instanceof InlineFragmentNode) {
30
            $this->validateFragementNode($node, function (NodeInterface $node) {
31
                /** @noinspection PhpUndefinedMethodInspection */
32
                return inlineFragmentOnNonCompositeMessage((string)$node->getTypeCondition());
33
            });
34
        }
35
36
        if ($node instanceof FragmentDefinitionNode) {
37
            $this->validateFragementNode($node, function (NodeInterface $node) {
38
                /** @noinspection PhpUndefinedMethodInspection */
39
                return fragmentOnNonCompositeMessage((string)$node, (string)$node->getTypeCondition());
40
            });
41
        }
42
43
        return $node;
44
    }
45
46
    /**
47
     * @param NodeInterface|InlineFragmentNode|FragmentDefinitionNode $node
48
     * @param callable                                                $errorMessageFunction
49
     * @throws \Exception
50
     * @throws \TypeError
51
     */
52
    protected function validateFragementNode(NodeInterface $node, callable $errorMessageFunction)
53
    {
54
        $typeCondition = $node->getTypeCondition();
55
56
        if (null !== $typeCondition) {
57
            $type = typeFromAST($this->validationContext->getSchema(), $typeCondition);
58
59
            if (null !== $type && !($type instanceof CompositeTypeInterface)) {
60
                $this->validationContext->reportError(new ValidationException($errorMessageFunction($node),
61
                    [$typeCondition]));
62
            }
63
        }
64
    }
65
}
66