Except   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 32
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 10 3
A __toString() 0 3 1
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Structure;
4
5
use Vanderlee\Comprehend\Core\ArgumentsTrait;
6
use Vanderlee\Comprehend\Core\Context;
7
use Vanderlee\Comprehend\Match\Failure;
8
use Vanderlee\Comprehend\Match\Success;
9
use Vanderlee\Comprehend\Parser\Parser;
10
11
/**
12
 * Match the first parser but not the second.
13
 * Essentially the same as (A - B) = (A + !B).
14
 *
15
 * @author Martijn
16
 */
17
class Except extends Parser
18
{
19
    use ArgumentsTrait;
20
21
    private $parserMatch;
22
    private $parserNot;
23
24
    /**
25
     * @param Parser|string $match
26
     * @param Parser|string $not
27
     */
28 1
    public function __construct($match, $not)
29
    {
30 1
        $this->parserMatch = self::getArgument($match);
31 1
        $this->parserNot = self::getArgument($not);
32 1
    }
33
34 8
    protected function parse(&$input, $offset, Context $context)
35
    {
36 8
        $match = $this->parserMatch->parse($input, $offset, $context);
37 8
        $not = $this->parserNot->parse($input, $offset, $context);
38
39 8
        if (($match instanceof Success) && ($not instanceof Failure)) {
40 3
            return $this->success($input, $offset, $match->length, $match);
41
        }
42
43 5
        return $this->failure($input, $offset, min($match->length, $not->length));
44
    }
45
46 8
    public function __toString()
47
    {
48 8
        return '( ' . $this->parserMatch . ' - ' . $this->parserNot . ' )';
49
    }
50
}
51