Passed
Pull Request — master (#23)
by Christoffer
01:58
created

Visitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
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 Visitor extends AbstractVisitor
8
{
9
10
    /**
11
     * @var callable|null
12
     */
13
    protected $enterFunction;
14
15
    /**
16
     * @var callable|null
17
     */
18
    protected $leaveFunction;
19
20
    /**
21
     * TestableVisitor constructor.
22
     * @param callable|null $enterFunction
23
     * @param callable|null $leaveFunction
24
     */
25
    public function __construct(?callable $enterFunction = null, ?callable $leaveFunction = null)
26
    {
27
        $this->enterFunction = $enterFunction;
28
        $this->leaveFunction = $leaveFunction;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function enterNode(
35
        NodeInterface $node,
36
        $key = null,
37
        ?NodeInterface $parent = null,
38
        array $path = []
39
    ): ?NodeInterface {
40
        return null !== $this->enterFunction
41
            ? call_user_func($this->enterFunction, $node, $key, $parent, $path)
42
            : $node;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function leaveNode(
49
        NodeInterface $node,
50
        $key = null,
51
        ?NodeInterface $parent = null,
52
        array $path = []
53
    ): ?NodeInterface {
54
        return null !== $this->leaveFunction
55
            ? call_user_func($this->leaveFunction, $node, $key, $parent, $path)
56
            : $node;
57
    }
58
}
59