Text   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A parse() 0 16 4
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 TextParser.
11
 *
12
 * @author Martijn
13
 */
14
class Text extends Parser
15
{
16
    use CaseSensitiveTrait;
17
18
    private $text;
19
    private $length;
20
21 51
    public function __construct($text)
22
    {
23 51
        $this->text = $text;
24
25 51
        $this->length = mb_strlen($text);
26 51
        if ($this->length <= 0) {
27 1
            throw new InvalidArgumentException('Empty argument');
28
        }
29 50
    }
30
31 128
    protected function parse(&$input, $offset, Context $context)
32
    {
33 128
        $this->pushCaseSensitivityToContext($context);
34
35 128
        $text = $context->handleCase($this->text);
36 128
        for ($c = 0; $c < $this->length; $c++) {
37 128
            if ($offset + $c >= mb_strlen($input) || $text[$c] != $context->handleCase($input[$offset + $c])) {
38 42
                $this->popCaseSensitivityFromContext($context);
39
40 42
                return $this->failure($input, $offset, $c);
41
            }
42
        }
43
44 103
        $this->popCaseSensitivityFromContext($context);
45
46 103
        return $this->success($input, $offset, $this->length);
47
    }
48
49 99
    public function __toString()
50
    {
51 99
        return '"' . $this->text . '"';
52
    }
53
}
54