Limited   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A recursion() 0 3 1
A factory() 0 3 1
A offsetUnset() 0 3 1
A offsetExists() 0 3 1
A __construct() 0 3 1
A offsetGet() 0 6 2
A offsetSet() 0 3 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