MicrosoftTolerantPhpParser::createNode()   A
last analyzed

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
/**
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