TokenTracker::getNextToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace gossi\formatter\parser;
3
4
use phootwork\tokenizer\Token;
5
use phootwork\tokenizer\TokenCollection;
6
use phootwork\tokenizer\TokenVisitorInterface;
7
8
class TokenTracker implements TokenVisitorInterface {
9
10
	private $tokens;
11
	private $context;
12
13
	private $next;
14
	private $prev;
15
16 10
	public function __construct(TokenCollection $tokens, Context $contextManager) {
17 10
		$this->tokens = $tokens;
18 10
		$this->context = $contextManager;
19 10
		$this->context->setTracker($this);
20 10
	}
21
22 10
	public function visitToken(Token $token) {
23 10
		$this->next = $this->nextToken($token);
24 10
		$this->prev = $this->prevToken($token);
25 10
		$this->context->visitToken($token);
26 10
	}
27
28 3
	public function getNextToken() {
29 3
		return $this->next;
30
	}
31
32 10
	public function getPrevToken() {
33 10
		return $this->prev;
34
	}
35
36 10
	public function nextToken(Token $token, $offset = 1) {
37 10
		$index = $this->tokens->indexOf($token);
38 10
		$index += $offset;
39 10
		$token = $this->tokens->get($index);
40
41 10
		if ($token === null) {
42 10
			$token = new Token();
43
		}
44
45 10
		return $token;
46
	}
47
48 10
	public function prevToken($token, $offset = 1) {
49 10
		$index = $this->tokens->indexOf($token);
50 10
		$index -= $offset;
51 10
		$token = $this->tokens->get($index);
52
53 10
		if ($token === null) {
54 10
			$token = new Token();
55
		}
56
57 10
		return $token;
58
	}
59
60 10
	public function isLastToken(Token $token) {
61 10
		return $this->tokens->indexOf($token) == $this->tokens->size() - 1;
62
	}
63
64
}
65