Optional   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A ignored() 0 3 1
A parse() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Parsers;
4
5
use Stratadox\Parser\Helpers\Cast;
6
use Stratadox\Parser\Parser;
7
use Stratadox\Parser\Result;
8
use Stratadox\Parser\Results\Skip;
9
10
/**
11
 * Optional
12
 *
13
 * Ignores the parser, yielding a skip result whether the parser succeeds or not.
14
 */
15
final class Optional extends Parser
16
{
17
    public function __construct(private Parser $parser) {}
18
19
    public static function ignored(Parser|string $parser): Parser
20
    {
21
        return new self(Cast::asParser($parser));
22
    }
23
24
    public function parse(string $input): Result
25
    {
26
        return Skip::upTo($this->parser->parse($input)->unparsed());
27
    }
28
}
29