TokenizedString   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 99
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A has() 0 3 1
A move() 0 4 2
A next() 0 4 1
A reset() 0 4 1
A read() 0 3 1
A identifyChar() 0 5 2
A pos() 0 4 2
A newLine() 0 3 1
A lineNo() 0 3 1
A extractString() 0 8 3
A extractBrackets() 0 8 3
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm\Parser\Tokenizer;
8
use Transphporm\Parser\Tokenizer;
9
10
class TokenizedString {
11
	private $str;
12
	private $pos = -1;
13
	private $lineNo = 1;
14
15
	private $chars = [
16
		'"' => Tokenizer::STRING,
17
		'\'' => Tokenizer::STRING,
18
		'(' => Tokenizer::OPEN_BRACKET,
19
		')' => Tokenizer::CLOSE_BRACKET,
20
		'[' => Tokenizer::OPEN_SQUARE_BRACKET,
21
		']' => Tokenizer::CLOSE_SQUARE_BRACKET,
22
		'+' => Tokenizer::CONCAT,
23
		',' => Tokenizer::ARG,
24
		'.' => Tokenizer::DOT,
25
		'!' => Tokenizer::NOT,
26
		'=' => Tokenizer::EQUALS,
27
		'{' => Tokenizer::OPEN_BRACE,
28
		'}' => Tokenizer::CLOSE_BRACE,
29
		':' => Tokenizer::COLON,
30
		';' => Tokenizer::SEMI_COLON,
31
		'#' => Tokenizer::NUM_SIGN,
32
		'>' => Tokenizer::GREATER_THAN,
33
		'<' => Tokenizer::LOWER_THAN,
34
		'@' => Tokenizer::AT_SIGN,
35
		'-' => Tokenizer::SUBTRACT,
36
		'*' => Tokenizer::MULTIPLY,
37
		'/' => Tokenizer::DIVIDE,
38
		' ' => Tokenizer::WHITESPACE,
39
		"\n" => Tokenizer::NEW_LINE,
40
		"\r" => Tokenizer::WHITESPACE,
41
		"\t" => Tokenizer::WHITESPACE
42
	];
43
44
	public function __construct($str) {
45
		$this->str = $str;
46
	}
47
48
	public function move($n) {
49
		if ($n === false) $this->pos = strlen($this->str)-1;
50
		else $this->pos += $n;
51
	}
52
53
	public function next() {
54
		$this->pos++;
55
		return $this->pos < strlen($this->str);
56
	}
57
58
	public function reset() {
59
		$this->lineNo = 1;
60
		$this->pos = -1;
61
	}
62
63
	public function read($offset = 0) {
64
		return $this->str[$this->pos + $offset];
65
	}
66
67
	public function identifyChar($offset = 0) {
68
		$chr = $this->str[$this->pos + $offset];
69
		if (!empty($this->chars[$chr])) return $this->chars[$chr];
70
		else return Tokenizer::NAME;
71
	}
72
73
	public function has($offset = 0) {
74
		return isset($this->str[$this->pos + $offset]);
75
	}
76
77
	public function pos($str) {
78
		$pos = strpos($this->str,  $str, $this->pos);
79
		return $pos ? $pos-$this->pos : false;
80
	}
81
82
	public function newLine() {
83
		return $this->lineNo++;
84
	}
85
86
	public function lineNo() {
87
		return $this->lineNo;
88
	}
89
90
	public function extractString($offset = 0) {
91
		$pos = $this->pos + $offset;
92
		$char = $this->str[$pos];
93
		$end = strpos($this->str, $char, $pos+1);
94
		while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1);
95
96
		return substr($this->str, $pos+1, $end-$pos-1);
97
	}
98
99
	public function extractBrackets($startBracket = '(', $closeBracket = ')', $offset = 0) {
100
		$open = $this->pos+$offset;
101
		$close = strpos($this->str, $closeBracket, $open);
102
103
		$cPos = $open+1;
104
		while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1);
105
		return substr($this->str, $open+1, $close-$open-1);
106
	}
107
108
}