Completed
Pull Request — master (#69)
by Christoffer
04:07 queued 01:01
created

OverlappingFieldsCanBeMergedRule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A enterNode() 0 22 3
A __construct() 0 4 1
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 Digia\GraphQL\Validation\Conflict\Map;
10
use Digia\GraphQL\Validation\Conflict\PairSet;
11
use function Digia\GraphQL\Validation\fieldsConflictMessage;
12
13
/**
14
 * Overlapping fields can be merged
15
 *
16
 * A selection set is only valid if all fields (including spreading any
17
 * fragments) either correspond to distinct response names or can be merged
18
 * without ambiguity.
19
 */
20
class OverlappingFieldsCanBeMergedRule extends AbstractRule
21
{
22
    use FindsConflictsTrait;
23
24
    /**
25
     * OverlappingFieldsCanBeMergedRule constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->cachedFieldsAndFragmentNames = new Map();
30
        $this->comparedFragmentPairs        = new PairSet();
31
    }
32
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function enterNode(NodeInterface $node): ?NodeInterface
38
    {
39
        if ($node instanceof SelectionSetNode) {
40
            $parentType = $this->validationContext->getParentType();
41
            $conflicts  = $this->findConflictsWithinSelectionSet(
42
                $this->cachedFieldsAndFragmentNames,
43
                $this->comparedFragmentPairs,
44
                $node,
45
                $parentType
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

45
                /** @scrutinizer ignore-type */ $parentType
Loading history...
46
            );
47
48
            foreach ($conflicts as $conflict) {
49
                $this->validationContext->reportError(
50
                    new ValidationException(
51
                        fieldsConflictMessage($conflict->getResponseName(), $conflict->getReason()),
52
                        $conflict->getAllFields()
53
                    )
54
                );
55
            }
56
        }
57
58
        return $node;
59
    }
60
}
61