Limited::recursion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Containers;
4
5
use Closure;
6
use Stratadox\Parser\LazyContainer;
7
use Stratadox\Parser\Parser;
8
use Stratadox\Parser\Parsers\Limit;
9
10
/**
11
 * Limited lazy container
12
 *
13
 * Prevents infinite looping on left-recursion.
14
 */
15
final class Limited implements LazyContainer
16
{
17
    private array $cache = [];
18
19
    public function __construct(
20
        private LazyContainer $container
21
    ) {}
22
23
    public static function recursion(LazyContainer $container): LazyContainer
24
    {
25
        return new self($container);
26
    }
27
28
    public function offsetGet($name): Parser
29
    {
30
        if (!isset($this->cache[$name])) {
31
            $this->cache[$name] = new Limit($this->container[$name]);
32
        }
33
        return $this->cache[$name];
34
    }
35
36
    public function offsetSet($name, $parser): void
37
    {
38
        $this->container[$name] = $parser;
39
    }
40
41
    public function offsetExists($name): bool
42
    {
43
        return isset($this->container[$name]);
44
    }
45
46
    public function offsetUnset($name): void
47
    {
48
        unset($this->container[$name]);
49
    }
50
51
    public function factory(string $name): Closure
52
    {
53
        return $this->container->factory($name);
54
    }
55
}
56