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

NewlineFormatter::newlineOrSpaceAfterCurly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

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