Completed
Push — master ( c72839...dd8e38 )
by Thomas
02:47
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 5
Bugs 1 Features 0
Metric Value
wmc 20
c 5
b 1
f 0
lcom 1
cbo 5
dl 0
loc 96
ccs 55
cts 64
cp 0.8594
rs 10

12 Methods

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