Passed
Push — master ( 00d502...37b4c7 )
by Pol
08:25
created

MicrosoftTolerantPhpParser::createNode()   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
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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($this->createNode(['label' => 'root']), $data);
29
    }
30
31
    /**
32
     * @param array $attributes
33
     *
34
     * @return \loophp\phptree\Node\AttributeNodeInterface
35
     */
36 1
    private function createNode(array $attributes): AttributeNodeInterface
37
    {
38 1
        return new AttributeNode($attributes);
39
    }
40
41
    /**
42
     * @param \loophp\phptree\Node\AttributeNodeInterface $parent
43
     * @param \Microsoft\PhpParser\Node ...$astNodes
44
     *
45
     * @return \loophp\phptree\Node\NodeInterface
46
     */
47 1
    private function parseNode(AttributeNodeInterface $parent, Node ...$astNodes): NodeInterface
48
    {
49 1
        return array_reduce(
50 1
            $astNodes,
51
            function (AttributeNodeInterface $carry, Node $astNode): NodeInterface {
52
                return $carry
53 1
                    ->add(
54 1
                        $this->parseNode(
55 1
                            $this->createNode([
56 1
                                'label' => $astNode->getNodeKindName(),
57 1
                                'astNode' => $astNode,
58
                            ]),
59 1
                            ...$astNode->getChildNodes()
60
                        )
61
                    );
62 1
            },
63
            $parent
64
        );
65
    }
66
}
67