Lazily   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 5
c 1
b 0
f 0
dl 0
loc 16
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 9 2
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Parsers;
4
5
use RuntimeException;
6
use Stratadox\Parser\LazyContainer;
7
use Stratadox\Parser\Parser;
8
use Stratadox\Parser\Result;
9
10
/**
11
 * Lazily
12
 *
13
 * Lazy-loading proxy for the parser.
14
 */
15
final class Lazily extends Parser
16
{
17
    public function __construct(
18
        private LazyContainer $lazy,
19
        private string        $name,
20
    ) {}
21
22
    public function parse(string $input): Result
23
    {
24
        $parser = $this->lazy->factory($this->name)();
25
26
        if (!$parser instanceof Parser) {
27
            throw new RuntimeException("Missing the lazy parser `{$this->name}` for input `$input`");
28
        }
29
30
        return $parser->parse($input);
31
    }
32
}
33