Range   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 56
ccs 30
cts 30
cp 1
rs 10
c 1
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 5
A parse() 0 25 6
A __toString() 0 3 1
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Terminal;
4
5
use InvalidArgumentException;
6
use Vanderlee\Comprehend\Core\Context;
7
use Vanderlee\Comprehend\Parser\Parser;
8
9
/**
10
 * Description of RangeParser.
11
 *
12
 * @author Martijn
13
 */
14
class Range extends Parser
15
{
16
    use CaseSensitiveTrait;
17
18
    private $first = null;
19
    private $last = null;
20
    private $in = true;
21
22 21
    public function __construct($first, $last, $in = true)
23
    {
24 21
        if ($first === null
25 21
            && $last === null) {
26 1
            throw new InvalidArgumentException('Empty arguments');
27
        }
28
29 20
        $this->first = $first === null
30 1
            ? null
31 19
            : self::parseCharacter($first);
32
33 18
        $this->last = $last === null
34 1
            ? null
35 17
            : self::parseCharacter($last);
36
37 16
        $this->in = (bool)$in;
38 16
    }
39
40 76
    protected function parse(&$input, $offset, Context $context)
41
    {
42 76
        if ($offset >= mb_strlen($input)) {
43 25
            return $this->failure($input, $offset);
44
        }
45
46 74
        $isSuccess = !$this->in; // By default not successful if required to match only within range
47
48 74
        $this->pushCaseSensitivityToContext($context);
49
50 74
        $first = ord($context->handleCase($this->first));
51 74
        $last = ord($context->handleCase($this->last));
52 74
        $ord = ord($context->handleCase($input[$offset]));
53
54 74
        $this->popCaseSensitivityFromContext($context);
55
56 74
        if ($first <= $ord
57 67
            && ($this->last === null
58 74
                || $ord <= $last)) {
59 61
            $isSuccess = !$isSuccess;
60
        }
61
62 74
        return $isSuccess
63 62
            ? $this->success($input, $offset, 1)
64 74
            : $this->failure($input, $offset);
65
    }
66
67 59
    public function __toString()
68
    {
69 59
        return sprintf('x%02x-x%02x', ord($this->first), ord($this->last));
70
    }
71
}
72