Completed
Pull Request — master (#23)
by Christoffer
01:51
created

AcceptVisitorTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
C accept() 0 37 12
1
<?php
2
3
namespace Digia\GraphQL\Language\AST\Visitor;
4
5
trait AcceptVisitorTrait
6
{
7
8
    protected static $kindToNodesToVisitMap = [
9
        'Name' => [],
10
11
        'Document' => ['definitions'],
12
        'OperationDefinition' => [
13
            'name',
14
            'variableDefinitions',
15
            'directives',
16
            'selectionSet',
17
        ],
18
        'VariableDefinition' => ['variable', 'type', 'defaultValue'],
19
        'Variable' => ['name'],
20
        'SelectionSet' => ['selections'],
21
        'Field' => ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
22
        'Argument' => ['name', 'value'],
23
24
        'FragmentSpread' => ['name', 'directives'],
25
        'InlineFragment' => ['typeCondition', 'directives', 'selectionSet'],
26
        'FragmentDefinition' => [
27
            'name',
28
            // Note: fragment variable definitions are experimental and may be changed
29
            // or removed in the future.
30
            'variableDefinitions',
31
            'typeCondition',
32
            'directives',
33
            'selectionSet',
34
        ],
35
36
        'IntValue' => [],
37
        'FloatValue' => [],
38
        'StringValue' => [],
39
        'BooleanValue' => [],
40
        'NullValue' => [],
41
        'EnumValue' => [],
42
        'ListValue' => ['values'],
43
        'ObjectValue' => ['fields'],
44
        'ObjectField' => ['name', 'value'],
45
46
        'Directive' => ['name', 'arguments'],
47
48
        'NamedType' => ['name'],
49
        'ListType' => ['type'],
50
        'NonNullType' => ['type'],
51
52
        'SchemaDefinition' => ['directives', 'operationTypes'],
53
        'OperationTypeDefinition' => ['type'],
54
55
        'ScalarTypeDefinition' => ['description', 'name', 'directives'],
56
        'ObjectTypeDefinition' => [
57
            'description',
58
            'name',
59
            'interfaces',
60
            'directives',
61
            'fields',
62
        ],
63
        'FieldDefinition' => ['description', 'name', 'arguments', 'type', 'directives'],
64
        'InputValueDefinition' => [
65
            'description',
66
            'name',
67
            'type',
68
            'defaultValue',
69
            'directives',
70
        ],
71
        'InterfaceTypeDefinition' => ['description', 'name', 'directives', 'fields'],
72
        'UnionTypeDefinition' => ['description', 'name', 'directives', 'types'],
73
        'EnumTypeDefinition' => ['description', 'name', 'directives', 'values'],
74
        'EnumValueDefinition' => ['description', 'name', 'directives'],
75
        'InputObjectTypeDefinition' => ['description', 'name', 'directives', 'fields'],
76
77
        'ScalarTypeExtension' => ['name', 'directives'],
78
        'ObjectTypeExtension' => ['name', 'interfaces', 'directives', 'fields'],
79
        'InterfaceTypeExtension' => ['name', 'directives', 'fields'],
80
        'UnionTypeExtension' => ['name', 'directives', 'types'],
81
        'EnumTypeExtension' => ['name', 'directives', 'values'],
82
        'InputObjectTypeExtension' => ['name', 'directives', 'fields'],
83
84
        'DirectiveDefinition' => ['description', 'name', 'arguments', 'locations'],
85
    ];
86
87
    /**
88
     * @return string
89
     */
90
    abstract public function getKind(): string;
91
92
    /**
93
     * @param VisitorInterface $visitor
94
     * @param string|null $key
95
     * @return array|null
96
     */
97
    public function accept(VisitorInterface $visitor, ?string $key = null, array $path = []): ?array
98
    {
99
        $edited = null;
100
101
        /** @noinspection PhpParamsInspection */
102
        if (null !== ($enterResult = $visitor->enterNode($this, $key, $path))) {
0 ignored issues
show
Bug introduced by
$this of type Digia\GraphQL\Language\A...itor\AcceptVisitorTrait is incompatible with the type Digia\GraphQL\Language\AST\Node\NodeInterface expected by parameter $node of Digia\GraphQL\Language\A...rInterface::enterNode(). ( Ignorable by Annotation )

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

102
        if (null !== ($enterResult = $visitor->enterNode(/** @scrutinizer ignore-type */ $this, $key, $path))) {
Loading history...
103
            $edited = $enterResult;
104
            foreach (self::$kindToNodesToVisitMap[$this->getKind()] as $name) {
105
                $value = $this->{$name};
106
                if (is_array($value) && !empty($value)) {
107
                    $path[] = $name;
108
                    foreach ($value as $k => $v) {
109
                        if ($v instanceof AcceptVisitorInterface) {
110
                            $path[] = $k;
111
                            if (null !== ($result = $v->accept($visitor, $k, $path))) {
112
                                $edited[$name][$k] = $result;
113
                            }
114
                            $path = array_slice($path, 0, count($path) - 1);
115
                        }
116
                    }
117
                    $path = array_slice($path, 0, count($path) - 1);
118
                } elseif ($value instanceof AcceptVisitorInterface) {
119
                    $path[] = $name;
120
                    if (null !== ($result = $value->accept($visitor, $name, $path))) {
121
                        $edited[$name] = $result;
122
                    }
123
                    $path = array_slice($path, 0, count($path) - 1);
124
                }
125
            }
126
        }
127
128
        /** @noinspection PhpParamsInspection */
129
        if (null !== ($leaveResult = $visitor->leaveNode($this, $key, $path))) {
130
            $edited = array_merge($leaveResult, \is_array($edited) ? $edited : []);
131
        }
132
133
        return $edited;
134
    }
135
}
136