Source   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
eloc 26
dl 0
loc 91
ccs 29
cts 29
cp 1
rs 10
c 3
b 1
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A triggerError() 0 4 1
A consume() 0 6 2
A read() 0 11 3
A __construct() 0 8 2
A match() 0 10 2
1
<?php
2
/**
3
 * Input for parser
4
 */
5
namespace xKerman\Restricted;
6
7
use InvalidArgumentException;
8
9
/**
10
 * Parser Input
11
 */
12
class Source
13
{
14
    /** @var string $str given string to deserialize */
15
    private $str;
16
17
    /** @var int $length given string length */
18
    private $length;
19
20
    /** @var int $current current position of parser */
21
    private $current;
22
23
    /**
24
     * constructor
25
     *
26
     * @param string $str parser input
27
     * @throws \InvalidArgumentException
28
     */
29 110
    public function __construct($str)
30
    {
31 110
        if (!is_string($str)) {
0 ignored issues
show
introduced by
The condition is_string($str) is always true.
Loading history...
32 1
            throw new InvalidArgumentException('expected string, but got: ' . gettype($str));
33
        }
34 109
        $this->str = $str;
35 109
        $this->length = strlen($str);
36 109
        $this->current = 0;
37 109
    }
38
39
    /**
40
     * throw error with currnt position
41
     *
42
     * @return void
43
     * @throws UnserializeFailedException
44
     */
45 67
    public function triggerError()
46
    {
47 67
        $bytes = strlen($this->str);
48 67
        throw new UnserializeFailedException("unserialize(): Error at offset {$this->current} of {$bytes} bytes");
49
    }
50
51
    /**
52
     * consume given string if it is as expected
53
     *
54
     * @param string  $expected expected string
55
     * @param integer $length   length of $expected
56
     * @return void
57
     * @throws UnserializeFailedException
58
     */
59 26
    public function consume($expected, $length)
60
    {
61 26
        if (strpos($this->str, $expected, $this->current) !== $this->current) {
62 5
            return $this->triggerError();
63
        }
64 21
        $this->current += $length;
65 21
    }
66
67
    /**
68
     * read givin length substring
69
     *
70
     * @param integer $length length to read
71
     * @return string
72
     * @throws UnserializeFailedException
73
     */
74 25
    public function read($length)
75
    {
76 25
        if ($length < 0) {
77 1
            return $this->triggerError();
78
        }
79 24
        if ($this->current + $length > $this->length) {
80 1
            return $this->triggerError();
81
        }
82
83 23
        $this->current += $length;
84 23
        return substr($this->str, $this->current - $length, $length);
85
    }
86
87
    /**
88
     * return matching string for given regexp
89
     *
90
     * @param string $regexp Regular Expression for expected substring
91
     * @return array
92
     */
93 109
    public function match($regexp)
94
    {
95 109
        $matches = array();
96 109
        if (!preg_match($regexp, $this->str, $matches, 0, $this->current)) {
97 62
            return $this->triggerError();
98
        }
99
100 52
        $this->current += strlen($matches[0]);
101 52
        array_shift($matches);
102 52
        return $matches;
103
    }
104
}
105