Completed
Push — master ( db8578...85f9c3 )
by Kirill
10:30
created

BaseRuleDelegate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 50
c 0
b 0
f 0
ccs 0
cts 27
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 4 1
A getId() 0 4 1
A getChildrenRuleIds() 0 12 3
A getChildrenRules() 0 11 3
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\Grammar\PP2\Delegate;
11
12
use Railt\Compiler\Grammar\PP2;
13
use Railt\Compiler\Grammar\PP2\Mapping;
14
use Railt\Parser\Ast\Delegate;
15
use Railt\Parser\Ast\Rule;
16
use Railt\Parser\Rule\Symbol;
17
18
/**
19
 * Class BaseRuleDelegate
20
 */
21
abstract class BaseRuleDelegate extends Rule implements ProvidesSymbol
22
{
23
    /**
24
     * @return Mapping
25
     */
26
    protected function map(): Mapping
27
    {
28
        return $this->env(PP2::ENV_MAP);
29
    }
30
31
    /**
32
     * @param string|null $rule
33
     * @return int
34
     */
35
    protected function getId(string $rule = null): int
36
    {
37
        return $this->map()->id($rule);
38
    }
39
40
    /**
41
     * @return array|int[]
42
     */
43
    protected function getChildrenRuleIds(): array
44
    {
45
        $result = [];
46
47
        if ($this instanceof ProvidesChildrenSymbol) {
48
            foreach ($this->getChildrenRules() as $rule) {
49
                $result[] = $rule->getId();
50
            }
51
        }
52
53
        return $result;
54
    }
55
56
    /**
57
     * @return iterable|Symbol[]
58
     */
59
    public function getChildrenRules(): iterable
60
    {
61
        /** @var ProvidesSymbol $child */
62
        foreach ($this->getChildren() as $child) {
63
            yield $child->getRule();
64
65
            if ($child instanceof ProvidesChildrenSymbol) {
66
                yield from $child->getChildrenRules();
67
            }
68
        }
69
    }
70
}
71