Completed
Pull Request — master (#20)
by Todd
03:42
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 54
    public function __construct($style = self::STYLE_JOIN, array $defaults = []) {
42
        $defaults += [
43
            'baseIndent' => 0
44 54
        ];
45
46 54
        $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 54
        $this->nodeProps = new \SplObjectStorage();
48 54
        $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 54
        $this->defaults = $defaults;
50 54
        $this->select('');
51 54
    }
52
53
    /**
54
     * Select a specific component buffer.
55
     * @param $component
56
     */
57 54
    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 54
        $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 54
        $this->currentName = $component;
60
61 54
        if (!array_key_exists($component, $this->buffers)) {
62 54
            $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 54
        }
64
65 54
        $this->current =& $this->buffers[$component];
66
67 54
        return $previous;
68
    }
69
70 52
    public function echoLiteral($value) {
71 52
        $this->current->echoLiteral($value);
72 52
    }
73
74 42
    public function echoCode($php) {
75 42
        $this->current->echoCode($php);
76 42
    }
77
78 54
    public function appendCode($php) {
79 54
        $this->current->appendCode($php);
80 54
    }
81
82 54
    public function indent($add) {
83 54
        $this->current->indent($add);
84 54
    }
85
86 18
    public function depth($add = 1) {
87 18
        $this->current->depth($add);
88 18
    }
89
90 18
    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 18
        return $this->current->depthName($name, $add);
92
    }
93
94 54
    public function pushScope(array $vars) {
95 54
        $this->current->pushScope($vars);
96 54
    }
97
98 54
    public function popScope() {
99 54
        $this->current->popScope();
100 54
    }
101
102 49
    public function getScopeVariables() {
103 49
        return $this->current->getScopeVariables();
104
    }
105
106 54
    public function flush() {
107 54
        switch ($this->getStyle()) {
108 54
            case self::STYLE_ARRAY:
109 11
                return $this->flushArray();
110 54
            default:
111 54
                return $this->flushJoin();
112 54
        }
113
    }
114
115
    private function flushJoin() {
116 54
        return implode("\n\n", array_map(function ($buffer) {
117
            /* @var ComponentBuffer $buffer */
118 54
            return $buffer->flush();
119 54
        }, $this->buffers));
120
    }
121
122 11
    private function flushArray() {
123 11
        $result = [];
124
125 11
        foreach ($this->buffers as $name => $buffer) {
126 11
            $flushed = $buffer->flush();
127 11
            if (empty($flushed)) {
128 9
                continue;
129
            }
130
131 3
            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 3
                $result[] = $flushed;
133 3
            } else {
134 1
                $result[] = var_export($name, true).' => '.$flushed;
135
            }
136 11
        }
137
138 11
        if (empty($result)) {
139 9
            return '[]';
140
        } else {
141 3
            return "[\n".implode(",\n\n", $result)."\n".$this->px().']';
142
        }
143
    }
144
145 3
    protected function px($add = 0) {
146 3
        return str_repeat(' ', ($this->defaults['baseIndent'] + $add) * 4);
147
    }
148
149
    /**
150
     * Get the style.
151
     *
152
     * @return string Returns the style.
153
     */
154 54
    public function getStyle() {
155 54
        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 54
    public function setBasename($basename) {
185 54
        $this->basename = $basename;
186 54
        return $this;
187
    }
188
189 54
    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 54
        if (!$this->nodeProps->contains($node) || !array_key_exists($name, $this->nodeProps[$node])) {
191 54
            return $default;
192
        }
193 4
        return $this->nodeProps[$node][$name];
194
    }
195
196 21
    public function setNodeProp(\DOMNode $node = null, $name, $value) {
197 21
        if ($node === null) {
198 17
            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 11
    public function getIndent() {
210 11
        return $this->current->getIndent();
211
    }
212
213 11
    public function getDepth() {
214 11
        return $this->current->getDepth();
215
    }
216
217
    public function getScope() {
218
        return $this->current->getScope();
219
    }
220
221 11
    public function getAllScopes() {
222 11
        return $this->current->getAllScopes();
223
    }
224
}
225