mageekguy\atoum\php\tokenizer: lines coverage

72% of 1092

OPs

72% of 172

Lines

66% of 241

Branches

1% of 702

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\php\tokenizer::__construct() 8 100% 2 100% 1 0% 1 100%
mageekguy\atoum\php\tokenizer::getIterator() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\php\tokenizer::resetIterator() 13 100% 2 100% 1 100% 1 100%
mageekguy\atoum\php\tokenizer::tokenize() 120 89% 19 100% 26 88% 22 5%
mageekguy\atoum\php\tokenizer::appendImportation() 133 99% 18 100% 29 100% 26 0%
mageekguy\atoum\php\tokenizer::appendNamespace() 255 84% 51 80% 59 75% 418 0%
mageekguy\atoum\php\tokenizer::appendFunction() 129 97% 19 100% 26 92% 66 0%
mageekguy\atoum\php\tokenizer::appendConstant() 133 99% 18 100% 29 100% 26 0%
mageekguy\atoum\php\tokenizer::appendCommentAndWhitespace() 88 0% 8 0% 19 0% 19 0%
mageekguy\atoum\php\tokenizer::appendArray() 157 0% 28 0% 39 0% 114 0%
mageekguy\atoum\php\tokenizer::nextTokenIs() 50 92% 6 67% 11 82% 8 25%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\php;
4

                    
5
use
6
	mageekguy\atoum\exceptions,
7
	mageekguy\atoum\php\tokenizer,
8
	mageekguy\atoum\php\tokenizer\iterators
9
;
10

                    
11
class tokenizer implements \iteratorAggregate
12
{
13
	protected $iterator = null;
14

                    
15
	private $tokens = null;
16
	private $currentIterator = null;
17

                    
18
	public function __construct()100%
19
	{
20
		$this->resetIterator();
21
	}
22

                    
23
	public function getIterator()100%
24
	{
25
		return $this->iterator;
26
	}
27

                    
28
	public function resetIterator()100%
29
	{
30
		$this->iterator = new iterators\phpScript();
31

                    
32
		return $this;
33
	}
34

                    
35
	public function tokenize($string)100%
36
	{
37
		$this->currentIterator = $this->iterator;
38

                    
39
		foreach ($this->tokens = new \arrayIterator(token_get_all($string)) as $key => $token)
40
		{
41
			switch ($token[0])
42
			{
43
				case T_CONST:
44
					$token = $this->appendConstant();
45
					break;
46

                    
47
				case T_USE:
48
					$token = $this->appendImportation();
49
					break;
50

                    
51
				case T_NAMESPACE:
52
					$token = $this->appendNamespace();
53
					break;
54

                    
55
				case T_FUNCTION:
56
					$token = $this->appendFunction();
57
					break;
58
			}
59

                    
60
			$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
61
		}
62

                    
63
		return $this;
64
	}
65

                    
66
	private function appendImportation()100%
67
	{
68
		$this->currentIterator->appendImportation($this->currentImportation = new iterators\phpImportation());
69
		$this->currentIterator = $this->currentImportation;
70

                    
71
		$inImportation = true;
72

                    
73
		while ($inImportation === true)
74
		{
75
			$token = $this->tokens->current();
76

                    
77
			switch ($token[0])
78
			{
79
				case ';':
80
				case T_CLOSE_TAG:
81
					$this->currentIterator = $this->currentIterator->getParent();
82
					$inImportation = false;
83
					break;
84

                    
85
				default:
86
					$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
87
					$this->tokens->next();
88
			}
89

                    
90
			$inImportation = $inImportation && $this->tokens->valid();
91
		}
92

                    
93
		return $this->tokens->valid() === false ? null : $this->tokens->current();
94
	}
95

                    
96
	private function appendNamespace()80%
97
	{
98
		$inNamespace = true;
99

                    
100
		while ($inNamespace === true)
101
		{
102
			$token = $this->tokens->current();
103

                    
104
			switch ($token[0])
105
			{
106
				case T_NAMESPACE:
107
					$parent = $this->currentIterator->getParent();
108

                    
109
					if ($parent !== null)
110
					{
111
						$this->currentIterator = $parent;
112
					}
113

                    
114
					$this->currentIterator->appendNamespace($this->currentNamespace = new iterators\phpNamespace());
115
					$this->currentIterator = $this->currentNamespace;
116
					break;
117

                    
118

                    
119
				case T_CONST:
120
					$this->appendConstant();
121
					break;
122

                    
123
				case T_FUNCTION:
124
					$this->appendFunction();
125
					break;
126

                    
127
				case T_FINAL:
128
				case T_ABSTRACT:
129
				case T_CLASS:
130
					$this->appendClass();
131
					break;
132

                    
133
				case T_INTERFACE:
134
					$this->appendInterface();
135
					break;
136

                    
137
				case ';':
138
					$this->currentIterator = $this->currentIterator->getParent();
139
					$inNamespace = false;
140
					break;
141

                    
142
				case T_CLOSE_TAG:
143
					if ($this->nextTokenIs(T_OPEN_TAG) === false)
144
					{
145
						$this->currentIterator = $this->currentIterator->getParent();
146
						$inNamespace = false;
147
					}
148
					break;
149

                    
150
				case '}':
151
					$inNamespace = false;
152
					break;
153
			}
154

                    
155
			$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
156

                    
157
			if ($token[0] === '}')
158
			{
159
				$this->currentIterator = $this->currentIterator->getParent();
160
			}
161

                    
162
			$this->tokens->next();
163

                    
164
			$inNamespace = $inNamespace && $this->tokens->valid();
165
		}
166

                    
167
		return $this->tokens->valid() === false ? null : $this->tokens->current();
168
	}
169

                    
170
	private function appendFunction()100%
171
	{
172
		$inFunction = true;
173

                    
174
		$this->currentIterator->appendFunction($this->currentFunction = new iterators\phpFunction());
175
		$this->currentIterator = $this->currentFunction;
176

                    
177
		while ($inFunction === true)
178
		{
179
			$token = $this->tokens->current();
180

                    
181
			switch ($token[0])
182
			{
183
				case '}':
184
					$inFunction = false;
185
					break;
186
			}
187

                    
188
			$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
189

                    
190
			if ($token[0] === '}')
191
			{
192
				$this->currentIterator = $this->currentIterator->getParent();
193
			}
194

                    
195
			$this->tokens->next();
196

                    
197
			$inFunction = $inFunction && $this->tokens->valid();
198
		}
199

                    
200
		return $this->tokens->valid() === false ? null : $this->tokens->current();
201
	}
202

                    
203
	private function appendConstant()100%
204
	{
205
		$this->currentIterator->appendConstant($this->currentNamespace = new iterators\phpConstant());
206
		$this->currentIterator = $this->currentNamespace;
207

                    
208
		$inConstant = true;
209

                    
210
		while ($inConstant === true)
211
		{
212
			$token = $this->tokens->current();
213

                    
214
			switch ($token[0])
215
			{
216
				case ';':
217
				case T_CLOSE_TAG:
218
					$this->currentIterator = $this->currentIterator->getParent();
219
					$inConstant = false;
220
					break;
221

                    
222
				default:
223
					$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
224
					$this->tokens->next();
225
			}
226

                    
227
			$inConstant = $inConstant && $this->tokens->valid();
228
		}
229

                    
230
		return $this->tokens->valid() === false ? null : $this->tokens->current();
231
	}
232

                    
233
	private function appendCommentAndWhitespace()0%
234
	{
235
		$key = $this->tokens->key();
236

                    
237
		while (isset($this->tokens[$key + 1]) === true && ($this->tokens[$key + 1][0] === T_WHITESPACE || $this->tokens[$key + 1][0] === T_COMMENT))
238
		{
239
			$this->tokens->next();
240

                    
241
			$token = $this->tokens->current();
242

                    
243
			$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
244

                    
245
			$key = $this->tokens->key();
246
		}
247
	}
248

                    
249
	private function appendArray()0%
250
	{
251
		$this->appendCommentAndWhitespace();
252

                    
253
		$this->tokens->next();
254

                    
255
		if ($this->tokens->valid() === true)
256
		{
257
			$token = $this->tokens->current();
258

                    
259
			if ($token[0] === '(')
260
			{
261
				$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
262

                    
263
				$stack = 1;
264

                    
265
				while ($stack > 0 && $this->tokens->valid() === true)
266
				{
267
					$this->tokens->next();
268

                    
269
					if ($this->tokens->valid() === true)
270
					{
271
						$token = $this->tokens->current();
272

                    
273
						if ($token[0] === '(')
274
						{
275
							$stack++;
276
						}
277
						else if ($token[0] === ')')
278
						{
279
							$stack--;
280
						}
281

                    
282
						$this->currentIterator->append(new tokenizer\token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
283
					}
284
				}
285
			}
286
		}
287
	}
288

                    
289
	private function nextTokenIs($tokenName, array $skipedTags = array(T_WHITESPACE, T_COMMENT, T_INLINE_HTML))67%
290
	{
291
		$key = $this->tokens->key() + 1;
292

                    
293
		while (isset($this->tokens[$key]) === true && in_array($this->tokens[$key], $skipedTags) === true)
294
		{
295
			$key++;
296
		}
297

                    
298
		$key++;
299

                    
300
		return (isset($this->tokens[$key]) === true && $this->tokens[$key][0] === $tokenName);
301
	}
302
}