Lexer::repair()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 13
cp 0.8462
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0582
1
<?php
2
namespace gossi\formatter\parser;
3
4
use phootwork\tokenizer\Token;
5
use phootwork\tokenizer\TokenCollection;
6
7
/**
8
 * Dunno if this is a lexer...
9
 * 
10
 */
11
class Lexer {
12
13
	private $tokens;
14
15 10
	public function repair(TokenCollection $tokens) {
16 10
		$fixedTokens = new TokenCollection();
17
18 10
		for ($i = 0, $n = $tokens->size(); $i < $n; $i++) {
19 10
			$token = $tokens->get($i);
20
21
			// fix ELSEIF
22 10
			if ($token->type == T_ELSE) {
23 5
				$nextToken = $tokens->get($i + 1);
24
25 5
				if ($nextToken->type == T_IF) {
26
					$i++;
27
					$fixedTokens->add(new Token([T_ELSEIF, 'else if']));
28
				} else {
29 5
					$fixedTokens->add($token);
30
				}
31
32 5
				continue;
33
			}
34
35 10
			$fixedTokens->add($token);
36
		}
37 10
		return $fixedTokens;
38
	}
39
40
	/**
41
	 *
42
	 * @param TokenCollection $tokens
43
	 * @return TokenCollection
44
	 */
45
	public function filterTokens(TokenCollection $tokens) {
46 10
		return $tokens->filter(function (Token $token) {
47 10
			return $token->type != T_WHITESPACE;
48 10
		});
49
	}
50
}
51