Completed
Push — 2.x ( 2a04a1...0da1ca )
by Alexander
02:03
created

NodeFinderVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2017, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument\Transformer;
12
13
use PhpParser\Node;
14
use PhpParser\NodeVisitorAbstract;
15
16
/**
17
 * Node finder visitor for compatibility with PHP-Parser < 4.0
18
 */
19
class NodeFinderVisitor extends NodeVisitorAbstract
20
{
21
    /**
22
     * List of node class names to search
23
     *
24
     * @var string[]
25
     */
26
    protected $searchNodes;
27
28
    /**
29
     * List of found nodes
30
     *
31
     * @var Node[] Found nodes
32
     */
33
    protected $foundNodes;
34
35
    /**
36
     * @param array $searchNodes List of node names to search in AST
37
     */
38 25
    public function __construct(array $searchNodes)
39
    {
40 25
        $this->searchNodes = $searchNodes;
41 25
    }
42
43
    /**
44
     * Get found nodes.
45
     *
46
     * Nodes are returned in pre-order.
47
     *
48
     * @return Node[] Found nodes
49
     */
50 25
    public function getFoundNodes()
51
    {
52 25
        return $this->foundNodes;
53
    }
54
55
    /**
56
     * Called once before traversal.
57
     *
58
     * Return value semantics:
59
     *  * null:      $nodes stays as-is
60
     *  * otherwise: $nodes is set to the return value
61
     *
62
     * @param Node[] $nodes Array of nodes
63
     *
64
     * @return null|Node[] Array of nodes
65
     */
66 25
    public function beforeTraverse(array $nodes)
67
    {
68 25
        $this->foundNodes = [];
69
70 25
        return null;
71
    }
72
73
    /**
74
     * Called when entering a node.
75
     *
76
     * Return value semantics:
77
     *  * null
78
     *        => $node stays as-is
79
     *  * NodeTraverser::DONT_TRAVERSE_CHILDREN
80
     *        => Children of $node are not traversed. $node stays as-is
81
     *  * NodeTraverser::STOP_TRAVERSAL
82
     *        => Traversal is aborted. $node stays as-is
83
     *  * otherwise
84
     *        => $node is set to the return value
85
     *
86
     * @param Node $node Node
87
     *
88
     * @return null|int|Node Node
89
     */
90 25
    public function enterNode(Node $node)
91
    {
92 25
        foreach ($this->searchNodes as $nodeClassName) {
93 25
            if ($node instanceof $nodeClassName) {
94 25
                $this->foundNodes[] = $node;
95
            }
96
        }
97
98 25
        return null;
99
    }
100
}
101