StringLexer::hasPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace donatj\Printf;
4
5
class StringLexer {
6
7
	private int $pos = 0;
8
9
	private string $string;
10
11
	/**
12
	 * StringScanner constructor.
13
	 */
14 15
	public function __construct( string $string ) {
15 15
		$this->string = $string;
16
	}
17
18 15
	public function peek( int $size = 1 ) : CharData {
19 15
		$str = substr($this->string, $this->pos, $size);
20
21 15
		return new CharData($str, strlen($str) < $size);
22
	}
23
24 15
	public function pos() : int {
25 15
		return $this->pos;
26
	}
27
28 15
	public function next() : CharData {
29 15
		$data = $this->peek();
30
31 15
		$this->pos += $data->length();
32
33 15
		return $data;
34
	}
35
36 15
	public function rewind() : void {
37 15
		$this->pos--;
38 15
		if( $this->pos < 0 ) {
39
			//@todo custom exception
40
			throw new \RuntimeException('cannot rewind');
41
		}
42
	}
43
44 14
	public function hasPrefix( string $prefix ) : bool {
45 14
		$peek = $this->peek(strlen($prefix));
46
47 14
		return $peek->getString() === $prefix;
48
	}
49
50 14
	public function substr( int $start, int $length ) : string {
51 14
		return substr($this->string, $start, $length);
52
	}
53
54
}
55