Completed
Pull Request — master (#36)
by Todd
05:02
created

ComponentBuffer::appendCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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 ComponentBuffer {
12
    private $buffer = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
13
    private $literalBuffer = '';
14
    private $inEcho = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
15
    private $indent;
16
    private $depth;
17
    private $scopes;
18
19 78
    public function __construct(array $defaults = []) {
20
        $defaults += [
21 78
            'depth' => 0,
22
            'indent' => 0,
23
            'scopes' => []
24
        ];
25
26 78
        $this->depth = $defaults['depth'];
27 78
        $this->indent = $defaults['indent'];
28 78
        $this->scopes = $defaults['scopes'];
29 78
    }
30
31 71
    public function echoLiteral($value) {
32 71
        $this->literalBuffer .= $value;
33 71
    }
34
35 56
    public function echoCode($php) {
36 56
        if (empty($php)) {
37
            return;
38
        }
39
40 56
        $this->flushLiteralBuffer();
41 56
        $this->ensureEcho(true);
42 56
        $this->buffer .= $php;
43 56
    }
44
45 78
    private function flushLiteralBuffer() {
46 78
        if (!empty($this->literalBuffer)) {
47 61
            $this->ensureEcho(true);
48 61
            $this->buffer .= var_export($this->literalBuffer, true);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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 61
            $this->literalBuffer = '';
50
        }
51 78
    }
52
53 71
    private function ensureEcho($append) {
54 71
        if (!$this->inEcho) {
55 71
            $this->buffer .= $this->px().'echo ';
56 71
            $this->inEcho = true;
57 47
        } elseif ($append) {
58 47
            $this->buffer .= ",\n".$this->px(+1);
59
        }
60 71
    }
61
62 78
    protected function px($add = 0) {
63 78
        return str_repeat(' ', ($this->indent + $add) * 4);
64
    }
65
66 78
    public function appendCode($php) {
67 78
        $this->flushLiteralBuffer();
68 78
        $this->flushEcho();
69
70 78
        $this->buffer .= $this->px().$php;
71 78
    }
72
73 78
    private function flushEcho() {
74 78
        $this->flushLiteralBuffer();
75
76 78
        if ($this->inEcho) {
77 70
            $this->buffer .= ";\n";
78 70
            $this->inEcho = false;
79
        }
80 78
    }
81
82 78
    public function indent($add) {
83 78
        $this->flushEcho();
84 78
        $this->indent += $add;
85 78
    }
86
87 26
    public function depth($add = 1) {
88 26
        $this->depth += $add;
89 26
    }
90
91 27
    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...
92 27
        $depth = $this->depth + $add;
93
94 27
        if ($depth === 0) {
95
            return $name;
96
        } else {
97 27
            return $name.$depth;
98
        }
99
    }
100
101 78
    public function pushScope(array $vars) {
102 78
        $this->scopes[] = $vars;
103 78
    }
104
105 70
    public function popScope() {
106 70
        array_pop($this->scopes);
107 70
    }
108
109 70
    public function getScopeVariables() {
110 70
        $r = array_replace(...$this->scopes);
111 70
        return $r;
112
    }
113
114
    public function getScope() {
115
        return end($this->scopes);
116
    }
117
118 14
    public function getAllScopes() {
119 14
        return $this->scopes;
120
    }
121
122 70
    public function flush() {
123 70
        $this->flushEcho();
124
125 70
        return $this->buffer;
126
    }
127
128
    /**
129
     * Get the indent.
130
     *
131
     * @return int Returns the indent.
132
     */
133 14
    public function getIndent() {
134 14
        return $this->indent;
135
    }
136
137
    /**
138
     * Get the depth.
139
     *
140
     * @return mixed Returns the depth.
141
     */
142 14
    public function getDepth() {
143 14
        return $this->depth;
144
    }
145
}
146