Grammar   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 8
c 1
b 0
f 0
dl 0
loc 40
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A offsetSet() 0 3 1
A container() 0 3 1
A with() 0 3 1
A offsetExists() 0 3 2
A offsetUnset() 0 3 1
A lazy() 0 3 1
A offsetGet() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Containers;
4
5
use Stratadox\Parser\Container;
6
use Stratadox\Parser\LazyContainer;
7
use Stratadox\Parser\Parser;
8
9
/**
10
 * Grammar Container
11
 *
12
 * Mixes lazy and eager containers.
13
 */
14
final class Grammar implements Container
15
{
16
    public function __construct(
17
        private Container     $eager,
18
        private LazyContainer $lazy,
19
    ) {}
20
21
    public static function container(): self
22
    {
23
        return new self(Eager::container(), Lazy::container());
24
    }
25
26
    public static function with(LazyContainer $lazy): self
27
    {
28
        return new self(Eager::container(), $lazy);
29
    }
30
31
    public function offsetGet($name): Parser
32
    {
33
        return $this->eager[$name] ?? $this->lazy[$name];
34
    }
35
36
    public function offsetSet($name, $parser): void
37
    {
38
        $this->eager[$name] = $parser;
39
    }
40
41
    public function offsetExists($name): bool
42
    {
43
        return isset($this->eager[$name]) || isset($this->lazy[$name]);
44
    }
45
46
    public function offsetUnset($name): void
47
    {
48
        unset($this->eager[$name]);
49
    }
50
51
    public function lazy(string $name, Parser $parser): void
52
    {
53
        $this->lazy[$name] = $parser;
54
    }
55
}
56