Completed
Pull Request — master (#69)
by Christoffer
02:11
created

OverlappingFieldsCanBeMergedRule::enterNode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 16
rs 9.4285
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\Node\NodeInterface;
7
use Digia\GraphQL\Language\Node\SelectionSetNode;
8
use Digia\GraphQL\Validation\Conflict\FindsConflictsTrait;
9
use function Digia\GraphQL\Validation\fieldsConflictMessage;
10
11
/**
12
 * Overlapping fields can be merged
13
 *
14
 * A selection set is only valid if all fields (including spreading any
15
 * fragments) either correspond to distinct response names or can be merged
16
 * without ambiguity.
17
 */
18
class OverlappingFieldsCanBeMergedRule extends AbstractRule
19
{
20
    use FindsConflictsTrait;
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function enterNode(NodeInterface $node): ?NodeInterface
26
    {
27
        if ($node instanceof SelectionSetNode) {
28
            $parentType = $this->validationContext->getParentType();
29
30
            foreach ($this->findConflictsWithinSelectionSet($node, $parentType) as $conflict) {
0 ignored issues
show
Bug introduced by
It seems like $parentType can also be of type Digia\GraphQL\Type\Definition\TypeInterface; however, parameter $parentType of Digia\GraphQL\Validation...ctsWithinSelectionSet() does only seem to accept null|Digia\GraphQL\Type\...tion\NamedTypeInterface, 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

30
            foreach ($this->findConflictsWithinSelectionSet($node, /** @scrutinizer ignore-type */ $parentType) as $conflict) {
Loading history...
31
                $this->validationContext->reportError(
32
                    new ValidationException(
33
                        fieldsConflictMessage($conflict->getResponseName(), $conflict->getReason()),
34
                        $conflict->getAllFields()
35
                    )
36
                );
37
            }
38
        }
39
40
        return $node;
41
    }
42
}
43