Passed
Push — master ( 15140b...85fc11 )
by Kirill
03:26
created

Rule::getValue()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
cc 4
eloc 6
nc 5
nop 0
ccs 0
cts 7
cp 0
crap 20
rs 9.2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Compiler\Parser\Ast;
11
12
/**
13
 * Class Rule
14
 */
15
class Rule extends Node implements RuleInterface
16
{
17
    /**
18
     * @var array|iterable|\Traversable
19
     */
20
    protected $children;
21
22
    /**
23
     * Rule constructor.
24
     * @param string $name
25
     * @param iterable $children
26
     */
27 54
    public function __construct(string $name, iterable $children = [], int $offset = 0)
28
    {
29 54
        parent::__construct($name, $offset);
30 54
        $this->children = $children;
31 54
    }
32
33
    /**
34
     * @return array
35
     */
36 54
    public function getChildren(): array
37
    {
38 54
        if ($this->children instanceof \Traversable) {
39
            $this->children = \iterator_to_array($this->children);
40
        }
41
42 54
        return $this->children;
43
    }
44
45
    /**
46
     * @param int $index
47
     * @return null|NodeInterface
48
     */
49
    public function getChild(int $index): ?NodeInterface
50
    {
51
        return $this->getChildren()[$index] ?? null;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function hasChildren(): bool
58
    {
59
        return \count($this->getChildren()) > 0;
60
    }
61
62
    /**
63
     * @param NodeInterface $node
64
     */
65
    public function append(NodeInterface $node): void
66
    {
67
        $this->getChildren();
68
69
        $this->children[] = $node;
70
    }
71
72
    /**
73
     * @return null|NodeInterface
74
     */
75
    public function pop(): ?NodeInterface
76
    {
77
        return \count($this->children) ? \array_pop($this->children) : null;
0 ignored issues
show
Bug Compatibility introduced by
The expression \count($this->children) ...this->children) : null; of type array|null adds the type array to the return on line 77 which is incompatible with the return type declared by the interface Railt\Compiler\Parser\Ast\RuleInterface::pop of type null|Railt\Compiler\Parser\Ast\NodeInterface.
Loading history...
78
    }
79
80
    /**
81
     * @return int
82
     */
83
    public function count(): int
84
    {
85
        return \count($this->getChildren());
86
    }
87
88
    /**
89
     * @return \Traversable
90
     */
91
    public function getIterator(): \Traversable
92
    {
93
        yield from $this->getChildren();
94
    }
95
96
    /**
97
     * @param string $name
98
     * @param int|null $depth
99
     * @return null|NodeInterface
100
     */
101
    public function find(string $name, int $depth = null): ?NodeInterface
102
    {
103
        foreach ($this->getChildren() as $child) {
104
            if ($child->is($name)) {
105
                return $child;
106
            }
107
108
            if (\is_int($depth) && $depth > 0 && $child instanceof RuleInterface) {
109
                return $child->find($name, $depth - 1);
110
            }
111
        }
112
113
        return null;
114
    }
115
116
    /**
117
     * @return string[]|\Traversable
118
     */
119
    public function getValue()
120
    {
121
        foreach ($this->children as $child) {
122
            if ($child instanceof RuleInterface) {
123
                yield from $child->getValue();
124
            }
125
126
            if ($child instanceof LeafInterface) {
127
                yield $child => $child->getValue();
128
            }
129
        }
130
    }
131
}
132