Completed
Pull Request — master (#30)
by
unknown
03:27
created

Writer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.62%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 0
dl 0
loc 97
ccs 41
cts 42
cp 0.9762
rs 10

9 Methods

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