Passed
Push — master ( 04d7e0...bd8f2a )
by Kirill
02:28
created

Builder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 92
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getId() 0 8 2
A rename() 0 4 1
A addChild() 0 4 1
A addChildBuilder() 0 4 1
A addChildrenBuilders() 0 6 2
A hasName() 0 4 1
reduce() 0 1 ?
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\Builder;
11
12
use Railt\Compiler\Grammar\PP2\Mapping;
13
use Railt\Parser\Rule\Symbol;
14
15
/**
16
 * Class Builder
17
 */
18
abstract class Builder
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $children = [];
24
25
    /**
26
     * @var int
27
     */
28
    protected $id;
29
30
    /**
31
     * @var Mapping
32
     */
33
    protected $mapper;
34
35
    /**
36
     * @var null|string
37
     */
38
    protected $name;
39
40
    /**
41
     * Builder constructor.
42
     * @param Mapping $mapper
43
     * @param string|null $name
44
     */
45
    public function __construct(Mapping $mapper, string $name = null)
46
    {
47
        $this->mapper = $mapper;
48
        $this->name = $name;
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getId(): int
55
    {
56
        if ($this->id === null) {
57
            $this->id = $this->mapper->id($this->name);
58
        }
59
60
        return $this->id;
61
    }
62
63
    /**
64
     * @param string $name
65
     */
66
    public function rename(string $name): void
67
    {
68
        $this->name = $name;
69
    }
70
71
    /**
72
     * @param int $child
73
     */
74
    public function addChild(int $child): void
75
    {
76
        $this->children[] = $child;
77
    }
78
79
    /**
80
     * @param Builder $builder
81
     */
82
    public function addChildBuilder(Builder $builder): void
83
    {
84
        $this->addChild($builder->getId());
85
    }
86
87
    /**
88
     * @param array $builders
89
     */
90
    public function addChildrenBuilders(array $builders): void
91
    {
92
        foreach ($builders as $builder) {
93
            $this->addChildBuilder($builder);
94
        }
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function hasName(): bool
101
    {
102
        return $this->name !== null;
103
    }
104
105
    /**
106
     * @return Symbol
107
     */
108
    abstract public function reduce(): Symbol;
109
}
110