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

NewlineFormatter::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace gossi\formatter\formatters;
3
4
use gossi\formatter\entities\Block;
5
use gossi\formatter\events\BlockEvent;
6
use gossi\formatter\parser\Context;
7
8
class NewlineFormatter extends SpecializedFormatter {
9
10
	protected function init() {
11
		$this->context->addListener(Context::EVENT_BLOCK_ENTER, [$this, 'preOpenCurlyBrace']);
12
		$this->context->addListener(Context::EVENT_BLOCK_LEAVE, [$this, 'postCloseCurlyBrace']);
13
	}
14
15
	public function preOpenCurlyBrace(BlockEvent $event) {
16
		$block = $event->getBlock();
17
18
		// curly braces in strucs
19
		if ($block->isStruct()) {
20
			$this->newlineOrSpaceBeforeCurly($this->config->getBraces('struct') == 'next');
21
		}
22
23
		// curly braces in functions
24
		else if ($block->isRoutine()) {
25
			$this->newlineOrSpaceBeforeCurly($this->config->getBraces('function') == 'next');
26
		}
27
28
		// curly braces in blocks
29
		else if ($block->isBlock()) {
30
			$this->newlineOrSpaceBeforeCurly($this->config->getBraces('blocks') == 'next');
31
		}
32
33
		// new line after open curly brace
34
		$this->defaultFormatter->addPostWriteln();
35
	}
36
37
	public function postCloseCurlyBrace(BlockEvent $event) {
38
		$block = $event->getBlock();
39
		$token = $event->getToken();
40
		$nextToken = $this->parser->getTracker()->nextToken($token);
41
42
		// check new line before T_ELSE and T_ELSEIF
43
		if (in_array($block->type, [Block::TYPE_IF, Block::TYPE_ELSEIF])
44
				&& in_array($nextToken->type, [T_ELSE, T_ELSEIF])) {
45
			$this->newlineOrSpaceAfterCurly($this->config->getNewline('elseif_else'));
46
		}
47
48
		// check new line before T_CATCH
49
		else if ($this->nextToken->type == T_CATCH) {
50
			$this->newlineOrSpaceAfterCurly($this->config->getNewline('catch'));
51
		}
52
53
		// check new line before finally
54
		else if ($token->contents == 'finally') {
55
			$this->newlineOrSpaceAfterCurly($this->config->getNewline('finally'));
56
		}
57
58
		// check new line before while in a do-while block
59
		else if ($block->type == Block::TYPE_DO && $nextToken->type == T_WHILE) {
60
			$this->newlineOrSpaceAfterCurly($this->config->getNewline('do_while'));
61
		}
62
63
		// anyway a new line
64
		else {
65
			$this->defaultFormatter->addPostWriteln();
66
		}
67
	}
68
69
	private function newlineOrSpaceBeforeCurly($condition) {
70
		if ($condition) {
71
			$this->writer->writeln();
72
		} else if ($this->config->getWhitespace('before_curly')) {
73
			$this->writer->write(' ');
74
		}
75
	}
76
77
	private function newlineOrSpaceAfterCurly($condition) {
78
		if ($condition) {
79
			$this->writer->writeln();
80
		} else {
81
			$this->defaultFormatter->addPostWrite(' ');
82
		}
83
	}
84
}
85