Completed
Pull Request — master (#34)
by Christoffer
02:28
created

SpecificKindVisitor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enterNode() 0 11 2
A __construct() 0 10 1
A leaveNode() 0 11 2
1
<?php
2
3
namespace Digia\GraphQL\Language\AST\Visitor;
4
5
use Digia\GraphQL\Language\AST\Node\NodeInterface;
6
7
class SpecificKindVisitor extends Visitor
8
{
9
10
    /**
11
     * @var array
12
     */
13
    protected $enterKinds = [];
14
15
    /**
16
     * @var array
17
     */
18
    protected $leaveKinds = [];
19
20
    /**
21
     * KindVisitor constructor.
22
     * @param array         $enterKinds
23
     * @param array         $leaveKinds
24
     * @param callable|null $enterFunction
25
     * @param callable|null $leaveFunction
26
     */
27
    public function __construct(
28
        array $enterKinds,
29
        array $leaveKinds,
30
        ?callable $enterFunction = null,
31
        ?callable $leaveFunction = null
32
    ) {
33
        parent::__construct($enterFunction, $leaveFunction);
34
35
        $this->enterKinds = $enterKinds;
36
        $this->leaveKinds = $leaveKinds;
37
    }
38
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function enterNode(
44
        NodeInterface $node,
45
        $key = null,
46
        ?NodeInterface $parent = null,
47
        array $path = []
48
    ): ?NodeInterface {
49
        if (\in_array($node->getKind(), $this->enterKinds, true)) {
50
            return parent::enterNode($node, $key, $parent, $path);
51
        }
52
53
        return $node;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function leaveNode(
60
        NodeInterface $node,
61
        $key = null,
62
        ?NodeInterface $parent = null,
63
        array $path = []
64
    ): ?NodeInterface {
65
        if (\in_array($node->getKind(), $this->leaveKinds, true)) {
66
            return parent::leaveNode($node, $key, $parent, $path);
67
        }
68
69
        return $node;
70
    }
71
}
72