Completed
Push — master ( 010cd6...22a8dd )
by Todd
11s
created

ComponentBuffer::getScopeVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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