Completed
Push — master ( 99f10a...f4bdff )
by Thomas
01:46
created

Lexer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 40
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterTokens() 0 5 1
A repair() 0 24 4
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
		}
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