Lazy::offsetUnset()   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\Lazily;
9
10
/**
11
 * Lazy Container
12
 *
13
 * Manages lazy loading, essential for recursive parsers.
14
 */
15
final class Lazy implements LazyContainer
16
{
17
    private array $registry = [];
18
19
    public static function container(): LazyContainer
20
    {
21
        return new self();
22
    }
23
24
    public function offsetExists($name): bool
25
    {
26
        return isset($this->registry[$name]);
27
    }
28
29
    public function offsetUnset($name): void
30
    {
31
        unset($this->registry[$name]);
32
    }
33
34
    public function offsetSet($name, $parser): void
35
    {
36
        $this->registry[$name] = fn() => $parser;
37
    }
38
39
    public function offsetGet($name): Parser
40
    {
41
        return new Lazily($this, $name);
42
    }
43
44
    public function factory(string $name): Closure
45
    {
46
        return $this->registry[$name] ?? fn() => null;
47
    }
48
}
49