Completed
Push — master ( fef961...cb7bad )
by Thomas
01:47
created

BlanksFormatter::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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