Completed
Pull Request — master (#10)
by Todd
03:18
created

CompilerBuffer   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 92.78%

Importance

Changes 0
Metric Value
wmc 35
lcom 2
cbo 1
dl 0
loc 214
ccs 90
cts 97
cp 0.9278
rs 9
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A echoLiteral() 0 3 1
A echoCode() 0 3 1
A appendCode() 0 3 1
A indent() 0 3 1
A depth() 0 3 1
A depthName() 0 3 1
A pushScope() 0 3 1
A popScope() 0 3 1
A getScopeVariables() 0 3 1
A flushJoin() 0 6 1
A px() 0 3 1
A getStyle() 0 3 1
A setStyle() 0 4 1
A getBasename() 0 3 1
A setBasename() 0 4 1
A getNodeProp() 0 6 3
A getIndent() 0 3 1
A getDepth() 0 3 1
A getScope() 0 3 1
A getAllScopes() 0 3 1
A select() 0 12 2
A flush() 0 8 2
B flushArray() 0 22 5
A setNodeProp() 0 12 3
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Ebi;
9
10
11
class CompilerBuffer {
12
    const STYLE_JOIN = 'join';
13
    const STYLE_ARRAY = 'array';
14
15
    /**
16
     * @var ComponentBuffer[]
17
     */
18
    private $buffers;
19
20
    /**
21
     * @var ComponentBuffer
22
     */
23
    private $current;
24
25
    private $currentName;
26
27
    private $basename;
28
29
    private $style = self::STYLE_JOIN;
30
31
    /**
32
     * @var array
33
     */
34
    private $defaults;
35
36
    /**
37
     * @var \SplObjectStorage
38
     */
39
    private $nodeProps;
40
41 38
    public function __construct($style = self::STYLE_JOIN, array $defaults = []) {
42
        $defaults += [
43
            'baseIndent' => 0
44 38
        ];
45
46 38
        $this->buffers = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
47 38
        $this->nodeProps = new \SplObjectStorage();
48 38
        $this->style = $style;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
49 38
        $this->defaults = $defaults;
50 38
        $this->select('');
51 38
    }
52
53
    /**
54
     * Select a specific component buffer.
55
     * @param $component
56
     */
57 38
    public function select($component) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
58 38
        $previous = $this->currentName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
59 38
        $this->currentName = $component;
60
61 38
        if (!array_key_exists($component, $this->buffers)) {
62 38
            $this->buffers[$component] = $buffer = new ComponentBuffer($this->defaults);
0 ignored issues
show
Unused Code introduced by
$buffer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63 38
        }
64
65 38
        $this->current =& $this->buffers[$component];
66
67 38
        return $previous;
68
    }
69
70 38
    public function echoLiteral($value) {
71 38
        $this->current->echoLiteral($value);
72 38
    }
73
74 26
    public function echoCode($php) {
75 26
        $this->current->echoCode($php);
76 26
    }
77
78 38
    public function appendCode($php) {
79 38
        $this->current->appendCode($php);
80 38
    }
81
82 38
    public function indent($add) {
83 38
        $this->current->indent($add);
84 38
    }
85
86 13
    public function depth($add = 1) {
87 13
        $this->current->depth($add);
88 13
    }
89
90 13
    public function depthName($name, $add = 0) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
91 13
        return $this->current->depthName($name, $add);
92
    }
93
94 38
    public function pushScope(array $vars) {
95 38
        $this->current->pushScope($vars);
96 38
    }
97
98 38
    public function popScope() {
99 38
        $this->current->popScope();
100 38
    }
101
102 34
    public function getScopeVariables() {
103 34
        return $this->current->getScopeVariables();
104
    }
105
106 38
    public function flush() {
107 38
        switch ($this->getStyle()) {
108 38
            case self::STYLE_ARRAY:
109 9
                return $this->flushArray();
110 38
            default:
111 38
                return $this->flushJoin();
112 38
        }
113
    }
114
115
    private function flushJoin() {
116 38
        return implode("\n\n", array_map(function ($buffer) {
117
            /* @var ComponentBuffer $buffer */
118 38
            return $buffer->flush();
119 38
        }, $this->buffers));
120
    }
121
122 9
    private function flushArray() {
123 9
        $result = [];
124
125 9
        foreach ($this->buffers as $name => $buffer) {
126 9
            $flushed = $buffer->flush();
127 9
            if (empty($flushed)) {
128 7
                continue;
129
            }
130
131 2
            if ($name === '') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $name (integer) and '' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
132 2
                $result[] = $flushed;
133 2
            } else {
134 1
                $result[] = var_export($name, true).' => '.$flushed;
135
            }
136 9
        }
137
138 9
        if (empty($result)) {
139 7
            return '[]';
140
        } else {
141 2
            return "[\n".implode(",\n\n", $result)."\n".$this->px().']';
142
        }
143
    }
144
145 2
    protected function px($add = 0) {
146 2
        return str_repeat(' ', ($this->defaults['baseIndent'] + $add) * 4);
147
    }
148
149
    /**
150
     * Get the style.
151
     *
152
     * @return string Returns the style.
153
     */
154 38
    public function getStyle() {
155 38
        return $this->style;
156
    }
157
158
    /**
159
     * Set the style.
160
     *
161
     * @param string $style One of the **STYLE_*** constants.
162
     * @return $this
163
     */
164
    public function setStyle($style) {
165
        $this->style = $style;
166
        return $this;
167
    }
168
169
    /**
170
     * Get the basename.
171
     *
172
     * @return string Returns the basename.
173
     */
174
    public function getBasename() {
175
        return $this->basename;
176
    }
177
178
    /**
179
     * Set the basename.
180
     *
181
     * @param string $basename
182
     * @return $this
183
     */
184 38
    public function setBasename($basename) {
185 38
        $this->basename = $basename;
186 38
        return $this;
187
    }
188
189 38
    public function getNodeProp(\DOMNode $node, $name, $default = null) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
190 38
        if (!$this->nodeProps->contains($node) || !array_key_exists($name, $this->nodeProps[$node])) {
191 38
            return $default;
192
        }
193 4
        return $this->nodeProps[$node][$name];
194
    }
195
196 19
    public function setNodeProp(\DOMNode $node = null, $name, $value) {
197 19
        if ($node === null) {
198 15
            return $this;
199
        }
200
201 4
        if (!$this->nodeProps->contains($node)) {
202 4
            $this->nodeProps->attach($node, [$name => $value]);
203 4
        }
204
205 4
        $this->nodeProps[$node] = [$name => $value] + $this->nodeProps[$node];
206 4
        return $this;
207
    }
208
209 9
    public function getIndent() {
210 9
        return $this->current->getIndent();
211
    }
212
213 9
    public function getDepth() {
214 9
        return $this->current->getDepth();
215
    }
216
217
    public function getScope() {
218
        return $this->current->getScope();
219
    }
220
221 9
    public function getAllScopes() {
222 9
        return $this->current->getAllScopes();
223
    }
224
}
225