Completed
Push — master ( a7556a...5a15ba )
by Richard
04:16
created

AdapterVisitor::enterNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 *
5
 * Copyright (c) 2016, 2015 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Indexer;
12
13
/**
14
 * Visitor that wraps an ASTVisitor to make it compatible with PhpParser.
15
 */
16
class AdapterVisitor implements \PhpParser\NodeVisitor {
17
    /**
18
     * @var LocationImpl
19
     */
20
    protected $location;
21
22
    /**
23
     * @var Insert
24
     */
25
    protected $insert;
26
27
    /**
28
     * @var ASTVisitor
29
     */
30
    protected $visitor;
31
32
    /**
33
     * This is used to exit early in enterNode and to break enterNode apart
34
     * into many methods.
35
     *
36
     * @var array<string,string>
37
     */
38
    protected $jump_labels;
39
40 25
    public function __construct(LocationImpl $location, Insert $insert, ASTVisitor $visitor) {
41 25
        $this->location = $location;
42 25
        $this->insert = $insert;
43 25
        $this->visitor = $visitor;
44 25
        $this->jump_labels = $visitor->visitorJumpLabels();
45 25
        assert('$this->jump_labels_are_correct()');
46 25
    }
47
48 25
    protected function jump_labels_are_correct() {
49 25
        if (!is_array($this->jump_labels)) {
50
            return false;
51
        }
52 25
        foreach ($this->jump_labels as $label => $method) {
53 25
            if (!is_callable(array($this->visitor, $method))) {
54
                return false;
55
            }
56 25
        }
57 25
        return true;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 25
    public function beforeTraverse(array $nodes) {
64 25
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 25
    public function afterTraverse(array $nodes) {
70 25
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 25
    public function enterNode(\PhpParser\Node $node) {
76
77 25
        $cls = get_class($node);
78 25
        if (isset($this->jump_labels[$cls])) {
79 23
            $this->location->set_current_node($node);
80 23
            $name = $this->jump_labels[$cls];
81 23
            $this->visitor->$name($this->insert, $this->location, $node);
82 23
            $this->location->flush_current_node();
83 23
        }
84 25
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 25
    public function leaveNode(\PhpParser\Node $node) {
90 25
    }
91
}
92