Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

Except::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Parser\Parser;
8
9
/**
10
 * Match the first parser but not the second.
11
 * Essentially the same as (A - B) = (A + !B)
12
 *
13
 * @author Martijn
14
 */
15
class Except extends Parser
16
{
17
18
    use ArgumentsTrait;
19
20
    private $parser_match = null;
21
    private $parser_not   = null;
22
23
    /**
24
     *
25
     * @param Parser|string $match
26
     * @param Parser|string $not
27
     */
28
    public function __construct($match, $not)
29
    {
30
        $this->parser_match = self::getArgument($match);
31
        $this->parser_not   = self::getArgument($not);
32
    }
33
34
    protected function parse(&$input, $offset, Context $context)
35
    {
36
        $match = $this->parser_match->parse($input, $offset, $context);
37
        $not   = $this->parser_not->parse($input, $offset, $context);
38
39
        if ($match->match && !$not->match) {
40
            return $this->success($input, $offset, $match->length, $match);
0 ignored issues
show
Bug introduced by
It seems like $match can also be of type Vanderlee\Comprehend\Match\Failure; however, parameter $successes of Vanderlee\Comprehend\Parser\Parser::success() does only seem to accept Vanderlee\Comprehend\Mat...omprehend\Match\Success, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            return $this->success($input, $offset, $match->length, /** @scrutinizer ignore-type */ $match);
Loading history...
41
        }
42
43
        return $this->failure($input, $offset, min($match->length, $not->length));
44
    }
45
46
    public function __toString()
47
    {
48
        return '( ' . $this->parser_match . ' - ' . $this->parser_not . ' )';
49
    }
50
51
}
52