Completed
Pull Request — master (#43)
by
unknown
08:04
created

Writer   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 20
c 1
b 1
f 0
lcom 1
cbo 0
dl 0
loc 104
ccs 52
cts 54
cp 0.963
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A indent() 0 5 1
A outdent() 0 5 1
A writeln() 0 5 1
C write() 0 33 11
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\generator\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 40
	public function __construct($options = []) {
39 40
		$this->options = array_merge($this->options, $options);
40
41 40
		$this->indentation = str_repeat($this->options['indentation_character'], 
42 40
			$this->options['indentation_size']);
43 40
	}
44
45 26
	public function indent() {
46 26
		$this->indentationLevel += 1;
47
48 26
		return $this;
49
	}
50
51 25
	public function outdent() {
52 25
		$this->indentationLevel = max($this->indentationLevel - 1, 0);
53
54 25
		return $this;
55
	}
56
57
	/**
58
	 *
59
	 * @param string $content        	
60
	 */
61 33
	public function writeln($content = '') {
62 33
		$this->write($content . "\n");
63
64 33
		return $this;
65
	}
66
67
	/**
68
	 *
69
	 * @param string $content        	
70
	 */
71 38
	 public function write($content) {
72 38
 		$tokens = token_get_all('<?php ' . $content);
73
 		
74 38
 		$heredocEndLines = [];
75
 		
76 38
 		foreach ($tokens as $line => $token) {
77 38
 		    if (is_array($token)) {
78 38
 				if (token_name($token[0]) === 'T_END_HEREDOC') {
79
 		        	$heredocEndLines[] = $token[2] - 1;
80
 				}
81 38
 		    }
82 38
 		}
83
 		
84 38
 		$lines = explode("\n", $content);
85
 		
86 38
 		for ($i = 0, $c = count($lines); $i < $c; $i++) {
87 38
 			if (!in_array($i, $heredocEndLines)) {
88 38
 				if ($this->indentationLevel > 0
89 38
 						&& !empty($lines[$i])
90 38
 						&& (empty($this->content) || "\n" === substr($this->content, -1))) {
91 5
 					$this->content .= str_repeat($this->indentation, $this->indentationLevel);
92 5
 				}
93 38
 			}
94
 			
95 38
 			$this->content .= $lines[$i];
96
97 38
 			if ($i + 1 < $c) {
98 33
 				$this->content .= "\n";
99 33
 			}
100 38
 		}
101
102 38
 		return $this;
103
 	}
104
105 32
	public function rtrim() {
106 32
		$addNl = "\n" === substr($this->content, -1);
107 32
		$this->content = rtrim($this->content);
108
109 32
		if ($addNl) {
110 25
			$this->content .= "\n";
111 25
		}
112
113 32
		return $this;
114
	}
115
116 32
	public function endsWith($search) {
117 32
		return substr($this->content, -strlen($search)) === $search;
118
	}
119
120 37
	public function reset() {
121 37
		$this->content = '';
122 37
		$this->indentationLevel = 0;
123
124 37
		return $this;
125
	}
126
127 38
	public function getContent() {
128 38
		return $this->content;
129
	}
130
}
131