Completed
Push — master ( ad6885...9e3ac7 )
by Hannes
9s
created

Enumerator::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro.
4
 *
5
 * byrokrat\autogiro is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\autogiro is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro;
24
25
use byrokrat\autogiro\Tree\Node;
26
use byrokrat\autogiro\Exception\LogicException;
27
28
/**
29
 * Tool to enumerate tree nodes of specified types
30
 */
31
class Enumerator
32
{
33
    /**
34
     * Magic method to register enumeration callbacks
35
     *
36
     * Usage: $enumerator->onNodeType($callback);
37
     *
38
     * @param string $name Name of node type to capure prefixed with on (eg onNodeType)
39
     * @param array  $args First argument must be a callable to be called with each capured node
40
     *
41
     * @throws LogicException If node type or callback is not specified
42
     */
43
    public function __call(string $name, array $args)
44
    {
45
        if (!preg_match('/^on[a-zA-Z0-9_]+$/', $name)) {
46
            throw new LogicException("Unknown method $name");
47
        }
48
49
        if (!is_callable($args[0])) {
50
            throw new LogicException('Enumeration callback must be callable');
51
        }
52
53
        $this->callbacks[substr($name, 2)] = $args[0];
0 ignored issues
show
Bug introduced by
The property callbacks does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
    }
55
56
    /**
57
     * Enumerate nodes in tree
58
     */
59
    public function enumerate(Node $tree)
60
    {
61
        $this->dispatch($tree);
62
63
        foreach ($tree->getChildren() as $child) {
64
            $this->enumerate($child);
65
        }
66
    }
67
68
    /**
69
     * Invoke callback registered with node type
70
     */
71
    private function dispatch(Node $node)
72
    {
73
        if (isset($this->callbacks[$node->getType()])) {
74
            $this->callbacks[$node->getType()]($node);
75
        }
76
    }
77
}
78