Passed
Push — master ( 835266...50b7f4 )
by Pol
02:47
created

MicrosoftTolerantPhpParser::import()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\phptree\Importer;
6
7
use Exception;
8
use loophp\phptree\Node\AttributeNode;
9
use loophp\phptree\Node\AttributeNodeInterface;
10
use loophp\phptree\Node\NodeInterface;
11
use Microsoft\PhpParser\Node;
12
use Microsoft\PhpParser\Node\SourceFileNode;
13
14
/**
15
 * Class MicrosoftTolerantPhpParser.
16
 */
17
final class MicrosoftTolerantPhpParser implements ImporterInterface
18
{
19
    /**
20
     * @param SourceFileNode $data
21
     *
22
     * @throws Exception
23
     *
24
     * @return \loophp\phptree\Node\NodeInterface
25
     */
26 1
    public function import($data): NodeInterface
27
    {
28 1
        return $this->parseNode($data, (new AttributeNode(['label' => 'root'])));
29
    }
30
31
    /**
32
     * @param \Microsoft\PhpParser\Node $astNode
33
     * @param \loophp\phptree\Node\AttributeNodeInterface $parent
34
     *
35
     * @return \loophp\phptree\Node\AttributeNodeInterface
36
     */
37 1
    private function parseNode(Node $astNode, AttributeNodeInterface $parent): AttributeNodeInterface
38
    {
39 1
        $node = new AttributeNode([
40 1
            'label' => $astNode->getNodeKindName(),
41 1
            'astNode' => $astNode,
42
        ]);
43
44 1
        $parent->add($node);
45
46 1
        foreach ($astNode->getChildNodes() as $child) {
47 1
            $this->parseNode($child, $node);
48
        }
49
50 1
        return $parent;
51
    }
52
}
53