MicrosoftTolerantPhpParser   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createNode() 0 3 1
A import() 0 3 1
A parseNode() 0 17 1
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\phptree\Importer;
11
12
use Exception;
13
use loophp\phptree\Node\AttributeNode;
14
use loophp\phptree\Node\AttributeNodeInterface;
15
use loophp\phptree\Node\NodeInterface;
16
use Microsoft\PhpParser\Node;
17
use Microsoft\PhpParser\Node\SourceFileNode;
18
19
/**
20
 * Class MicrosoftTolerantPhpParser.
21
 */
22
final class MicrosoftTolerantPhpParser implements ImporterInterface
23
{
24
    /**
25
     * @param SourceFileNode $data
26
     *
27
     * @throws Exception
28
     */
29 1
    public function import($data): NodeInterface
30
    {
31 1
        return $this->parseNode($this->createNode(['label' => 'root']), $data);
32
    }
33
34 1
    private function createNode(array $attributes): AttributeNodeInterface
35
    {
36 1
        return new AttributeNode($attributes);
37
    }
38
39
    /**
40
     * @param Node ...$astNodes
41
     */
42 1
    private function parseNode(AttributeNodeInterface $parent, Node ...$astNodes): NodeInterface
43
    {
44 1
        return array_reduce(
45 1
            $astNodes,
46
            function (AttributeNodeInterface $carry, Node $astNode): NodeInterface {
47
                return $carry
48 1
                    ->add(
49 1
                        $this->parseNode(
50 1
                            $this->createNode([
51 1
                                'label' => $astNode->getNodeKindName(),
52 1
                                'astNode' => $astNode,
53
                            ]),
54 1
                            ...$astNode->getChildNodes()
55
                        )
56
                    );
57 1
            },
58
            $parent
59
        );
60
    }
61
}
62