Maybe::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 1
rs 10
cc 1
nc 1
nop 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
 * Maybe
12
 *
13
 * Returns the result if successful, or an empty "skip this" result otherwise.
14
 */
15
final class Maybe extends Parser
16
{
17
    public function __construct(private Parser $maybe) {}
18
19
    public static function that(Parser|string $parser): Parser
20
    {
21
        return new self(Cast::asParser($parser));
22
    }
23
24
    public function parse(string $input): Result
25
    {
26
        $result = $this->maybe->parse($input);
27
28
        if ($result->ok()) {
29
            return $result;
30
        }
31
        return Skip::upTo($input);
32
    }
33
}
34