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

BlanksFormatter::blankAfter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace gossi\formatter\formatters;
3
4
use phootwork\tokenizer\Token;
5
6
class BlanksFormatter extends SpecializedFormatter {
7
8
	private $units;
9
	private $currentUnit;
10
11
	protected function init() {
12
		$this->units = $this->parser->getAnalyzer()->getUnits();
13
	}
14
15
	protected function doVisitToken(Token $token) {
16
		$this->unitStart($token);
17
		$this->unitEnd($token);
18
	}
19
20
	private function unitStart(Token $token) {
21
		$unit = $this->units->findUnitByStart($token);
22
23
		if ($unit !== null) {
24
			$this->currentUnit = $unit;
25
			$this->blankBefore($unit->type);
26
		}
27
	}
28
29
	private function unitEnd(Token $token) {
30
		if ($this->currentUnit !== null && $this->currentUnit->end === $token) {
31
			$this->blankAfter($this->currentUnit->type);
32
			$this->currentUnit = null;
33
		}
34
	}
35
36
	private function blankBefore($key) {
37
		for ($i = 0, $count = $this->config->getBlanks('before_' . $key); $i < $count; $i++) {
38
			$this->defaultFormatter->addPreWriteln();
39
		}
40
	}
41
42
	private function blankAfter($key) {
43
	for ($i = 0, $count = $this->config->getBlanks('after_' . $key); $i < $count; $i++) {
44
			$this->defaultFormatter->addPostWriteln();
45
		}
46
	}
47
48
}
49