Limit   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 11 2
A __construct() 0 1 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Parsers;
4
5
use Stratadox\Parser\Parser;
6
use Stratadox\Parser\Result;
7
use Stratadox\Parser\Results\Error;
8
9
/**
10
 * Lazy-Limit
11
 *
12
 * Safety harness to prevent left-recursion from eating infinite resources.
13
 */
14
final class Limit extends Parser
15
{
16
    private array $locked = [];
17
18
    public function __construct(private Parser $parser) {}
19
20
    public function parse(string $input): Result
21
    {
22
        if (isset($this->locked[$input])) {
23
            return Error::in($input);
24
        }
25
26
        $this->locked[$input] = true;
27
        $result = $this->parser->parse($input);
28
        unset($this->locked[$input]);
29
30
        return $result;
31
    }
32
}
33