Char::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 3
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Terminal;
4
5
use Vanderlee\Comprehend\Core\Context;
6
use Vanderlee\Comprehend\Parser\Parser;
7
8
/**
9
 * Description of CharParser.
10
 *
11
 * @author Martijn
12
 */
13
class Char extends Parser
14
{
15
    use CaseSensitiveTrait;
16
17
    private $character;
18
19
    /**
20
     * Match the specified character (`true`) or everything else (`false`).
21
     *
22
     * @var bool
23
     */
24
    private $include;
25
26 73
    public function __construct($character, $include = true)
27
    {
28 73
        $this->character = self::parseCharacter($character);
29 69
        $this->include = $include;
30 69
    }
31
32 254
    protected function parse(&$input, $offset, Context $context)
33
    {
34 254
        if ($offset >= mb_strlen($input)) {
35 74
            return $this->failure($input, $offset);
36
        }
37
38 242
        $this->pushCaseSensitivityToContext($context);
39
40 242
        if ($context->handleCase($input[$offset]) === $context->handleCase($this->character)) {
41 214
            $this->popCaseSensitivityFromContext($context);
42
43 214
            return $this->makeMatch($this->include, $input, $offset, 1);
44
        }
45
46 102
        $this->popCaseSensitivityFromContext($context);
47
48 102
        return $this->makeMatch(!$this->include, $input, $offset, 1);
49
    }
50
51 201
    public function __toString()
52
    {
53 201
        return '\'' . $this->character . '\'';
54
    }
55
}
56