Completed
Push — master ( a37f55...6c95d8 )
by Christoffer
02:24
created

SpecificKindVisitor::leaveNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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