Completed
Push — master ( ddcbf1...743278 )
by Thomas
04:16 queued 01:18
created

DefaultFormatter   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 85.94%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 96
ccs 55
cts 64
cp 0.8594
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A addPreWrite() 0 3 1
A addPreWriteln() 0 3 1
A addPreOutdent() 0 3 1
A addPostWrite() 0 3 1
A addPostWriteln() 0 3 1
A addPostIndent() 0 3 1
A addPreIndent() 0 3 1
A addPostOutdent() 0 3 1
A hideToken() 0 3 1
B doVisitToken() 0 25 4
B processCommands() 0 21 6
1
<?php
2
namespace gossi\formatter\formatters;
3
4
use gossi\formatter\entities\Group;
5
use phootwork\collection\Queue;
6
use phootwork\tokenizer\Token;
7
8
class DefaultFormatter extends BaseFormatter {
9
10
	private $preCommands;
11
	private $postCommands;
12
	private $showToken = true;
13
14 2
	protected function init() {
15 2
		$this->preCommands = new Queue();
16 2
		$this->postCommands = new Queue();
17 2
	}
18
19 2
	public function addPreWrite($content = '') {
20 2
		$this->preCommands->enqueue(['write', $content]);
21 2
	}
22
23 1
	public function addPreWriteln($content = '') {
24 1
		$this->preCommands->enqueue(['writeln', $content]);
25 1
	}
26
27
	public function addPreIndent() {
28
		$this->preCommands->enqueue(['indent']);
29
	}
30
31 2
	public function addPreOutdent() {
32 2
		$this->preCommands->enqueue(['outdent']);
33 2
	}
34
35 2
	public function addPostWrite($content = '') {
36 2
		$this->postCommands->enqueue(['write', $content]);
37 2
	}
38
39 2
	public function addPostWriteln($content = '') {
40 2
		$this->postCommands->enqueue(['writeln', $content]);
41 2
	}
42
43 2
	public function addPostIndent() {
44 2
		$this->postCommands->enqueue(['indent']);
45 2
	}
46
47
	public function addPostOutdent() {
48
		$this->postCommands->enqueue(['outdent']);
49
	}
50
51
	public function hideToken() {
52
		$this->showToken = false;
53
	}
54
55 2
	protected function doVisitToken(Token $token) {
56 2
		$group = $this->context->getGroupContext();
57
58
		// pre commands
59 2
		$this->processCommands($this->preCommands);
60
61
		// finish line on semicolon
62 2
		if ($token->contents == ';' && $group->type != Group::BLOCK) {
63 2
			$this->context->resetLineContext();
64 2
			$this->writer->writeln($token->contents);
65 2
		}
66
67
		// when no semicolon and token output allowed
68 2
		else if ($this->showToken) {
69 2
			$this->writer->write($token->contents);
70 2
		}
71
72
		// post commands
73 2
		$this->processCommands($this->postCommands);
74
75
		// reset
76 2
		$this->preCommands->clear();
77 2
		$this->postCommands->clear();
78 2
		$this->showToken = true;
79 2
	}
80
81 2
	private function processCommands(Queue $commands) {
82 2
		foreach ($commands as $cmd) {
83 2
			switch ($cmd[0]) {
84 2
				case 'write':
85 2
					$this->writer->write($cmd[1]);
86 2
					break;
87
88 2
				case 'writeln':
89 2
					$this->writer->writeln($cmd[1]);
90 2
					break;
91
92 2
				case 'indent':
93 2
					$this->writer->indent();
94 2
					break;
95
96 2
				case 'outdent':
97 2
					$this->writer->outdent();
98 2
					break;
99 2
			}
100 2
		}
101 2
	}
102
103
}
104