Completed
Push — master ( c72839...dd8e38 )
by Thomas
02:47
created

Lexer::repair()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.128

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
ccs 12
cts 15
cp 0.8
rs 8.6845
cc 4
eloc 14
nc 4
nop 1
crap 4.128
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;
0 ignored issues
show
Unused Code introduced by
The property $tokens is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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 10
		}
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