Passed
Push — master ( b48009...a47442 )
by Pol
02:39
created

NikicPhpAst::parseNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 1
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\phptree\Importer;
6
7
use ast\Node;
0 ignored issues
show
Bug introduced by
The type ast\Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Exception;
9
use loophp\phptree\Node\AttributeNode;
10
use loophp\phptree\Node\AttributeNodeInterface;
11
use loophp\phptree\Node\NodeInterface;
12
13
use function ast\get_metadata;
0 ignored issues
show
introduced by
The function ast\get_metadata was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
15
/**
16
 * Class NikicPhpAst.
17
 */
18
final class NikicPhpAst implements ImporterInterface
19
{
20
    /**
21
     * @var array<int, \ast\Metadata>
22
     */
23
    private $metadata = [];
24
25
    /**
26
     * @param Node $data
27
     *
28
     * @throws Exception
29
     *
30
     * @return NodeInterface
31
     */
32 1
    public function import($data): NodeInterface
33
    {
34 1
        $this->metadata = get_metadata();
0 ignored issues
show
Bug introduced by
The function get_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $this->metadata = /** @scrutinizer ignore-call */ get_metadata();
Loading history...
35
36 1
        return $this->parseNode($this->createNode(['label' => 'root']), $data);
37
    }
38
39
    /**
40
     * @param array $attributes
41
     *
42
     * @return AttributeNodeInterface
43
     */
44 1
    private function createNode(array $attributes): AttributeNodeInterface
45
    {
46 1
        return new AttributeNode($attributes);
47
    }
48
49
    /**
50
     * @param AttributeNodeInterface $parent
51
     * @param Node ...$astNodes
52
     *
53
     * @return NodeInterface
54
     */
55 1
    private function parseNode(AttributeNodeInterface $parent, Node ...$astNodes): NodeInterface
56
    {
57 1
        return array_reduce(
58 1
            $astNodes,
59
            function (AttributeNodeInterface $carry, Node $astNode): NodeInterface {
60
                return $carry
61 1
                    ->add(
62 1
                        $this->parseNode(
63 1
                            $this->createNode([
64 1
                                'label' => $this->metadata[$astNode->kind]->name,
65 1
                                'astNode' => $astNode,
66
                            ]),
67 1
                            ...array_values(array_filter($astNode->children, 'is_object'))
68
                        )
69
                    );
70 1
            },
71
            $parent
72
        );
73
    }
74
}
75