StringLexer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 2
b 0
f 0
dl 0
loc 47
ccs 19
cts 20
cp 0.95
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A substr() 0 2 1
A __construct() 0 2 1
A next() 0 6 1
A peek() 0 4 1
A pos() 0 2 1
A hasPrefix() 0 4 1
A rewind() 0 5 2
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