Writer::write()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 5
nop 1
crap 7
1
<?php
2
/*
3
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
declare(strict_types=1);
18
19
namespace gossi\codegen\generator\utils;
20
21
/**
22
 * A writer implementation.
23
 *
24
 * This may be used to simplify writing well-formatted code.
25
 *
26
 * @author Johannes M. Schmitt <[email protected]>
27
 */
28
class Writer {
29
30
	private $content = '';
31
	private $indentationLevel = 0;
32
	private $indentation;
33
34
	private $options = [
35
		'indentation_character' => "\t",
36
		'indentation_size' => 1
37
	];
38
39 44
	public function __construct($options = []) {
40 44
		$this->options = array_merge($this->options, $options);
41
42 44
		$this->indentation = str_repeat($this->options['indentation_character'],
43 44
			$this->options['indentation_size']);
44 44
	}
45
46 29
	public function indent() {
47 29
		$this->indentationLevel += 1;
48
49 29
		return $this;
50
	}
51
52 28
	public function outdent() {
53 28
		$this->indentationLevel = max($this->indentationLevel - 1, 0);
54
55 28
		return $this;
56
	}
57
58
	/**
59
	 *
60
	 * @param string $content
61
	 */
62 36
	public function writeln(string $content = '') {
63 36
		$this->write($content . "\n");
64
65 36
		return $this;
66
	}
67
68
	/**
69
	 *
70
	 * @param string $content
71
	 */
72 42
	public function write(string $content) {
73 42
		$lines = explode("\n", $content);
74 42
		for ($i = 0, $c = count($lines); $i < $c; $i++) {
75 42
			if ($this->indentationLevel > 0
76 42
					&& !empty($lines[$i])
77 42
					&& (empty($this->content) || "\n" === substr($this->content, -1))) {
78 5
				$this->content .= str_repeat($this->indentation, $this->indentationLevel);
79
			}
80
81 42
			$this->content .= $lines[$i];
82
83 42
			if ($i + 1 < $c) {
84 36
				$this->content .= "\n";
85
			}
86
		}
87
88 42
		return $this;
89
	}
90
91 35
	public function rtrim() {
92 35
		$addNl = "\n" === substr($this->content, -1);
93 35
		$this->content = rtrim($this->content);
94
95 35
		if ($addNl) {
96 28
			$this->content .= "\n";
97
		}
98
99 35
		return $this;
100
	}
101
102 35
	public function endsWith($search): bool {
103 35
		return substr($this->content, -strlen($search)) === $search;
104
	}
105
106 41
	public function reset() {
107 41
		$this->content = '';
108 41
		$this->indentationLevel = 0;
109
110 41
		return $this;
111
	}
112
113 42
	public function getContent(): string {
114 42
		return $this->content;
115
	}
116
}
117