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

MicrosoftTolerantPhpParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 34
ccs 10
cts 10
cp 1
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A import() 0 3 1
A parseNode() 0 14 2
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