Visitor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 18
c 2
b 0
f 0
dl 0
loc 52
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 8 2
A visitAfter() 0 3 1
A after() 0 3 1
A visit() 0 8 4
A visitBefore() 0 3 1
A before() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of byrokrat\autogiro.
5
 *
6
 * byrokrat\autogiro is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\autogiro is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\autogiro\Visitor;
25
26
use byrokrat\autogiro\Tree\Node;
27
28
/**
29
 * Visitor that dynamically calls method based on node name and type
30
 *
31
 * Will read the node name and type (eg. SomeNode) and dispatch a node specific
32
 * method if defined in visitor (eg. beforeSomeNode or afterSomeNode).
33
 */
34
class Visitor implements VisitorInterface
35
{
36
    private const AFTER = 'after';
37
    private const BEFORE = 'before';
38
39
    /**
40
     * @var array<string, array>
41
     */
42
    private $hooks = [
43
        self::AFTER => [],
44
        self::BEFORE => [],
45
    ];
46
47
    public function after(string $name, callable $hook): void
48
    {
49
        $this->hooks[self::AFTER][strtolower($name)] = $hook;
50
    }
51
52
    public function before(string $name, callable $hook): void
53
    {
54
        $this->hooks[self::BEFORE][strtolower($name)] = $hook;
55
    }
56
57
    public function visitAfter(Node $node): void
58
    {
59
        $this->visit(self::AFTER, $node);
60
    }
61
62
    public function visitBefore(Node $node): void
63
    {
64
        $this->visit(self::BEFORE, $node);
65
    }
66
67
    private function visit(string $prefix, Node $node): void
68
    {
69
        if ($node->getName()) {
70
            $this->dispatch($prefix, $node->getName(), $node);
71
        }
72
73
        if ($node->getType() && $node->getType() != $node->getName()) {
74
            $this->dispatch($prefix, $node->getType(), $node);
75
        }
76
    }
77
78
    private function dispatch(string $prefix, string $name, Node $node): void
79
    {
80
        ($this->hooks[$prefix][strtolower($name)] ?? function () {})($node); // phpcs:ignore
81
82
        $method = $prefix . $name;
83
84
        if (method_exists($this, $method)) {
85
            $this->$method($node);
86
        }
87
    }
88
}
89