NikicPhpAst::parseNode()   A
last analyzed

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
/**
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 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...
13
use Exception;
14
use loophp\phptree\Node\AttributeNode;
15
use loophp\phptree\Node\AttributeNodeInterface;
16
use loophp\phptree\Node\NodeInterface;
17
18
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...
19
20
/**
21
 * Class NikicPhpAst.
22
 */
23
final class NikicPhpAst implements ImporterInterface
24
{
25
    /**
26
     * @var array<int, \ast\Metadata>
27
     */
28
    private $metadata = [];
29
30
    /**
31
     * @param Node $data
32
     *
33
     * @throws Exception
34
     */
35 1
    public function import($data): NodeInterface
36
    {
37 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

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