Classic::block()   F
last analyzed

Complexity

Conditions 15
Paths 451

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 15

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 32
cts 32
cp 1
rs 2.5124
c 0
b 0
f 0
cc 15
nc 451
nop 1
crap 15

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LesserPhp\Formatter;
4
5
/**
6
 * lesserphp
7
 * https://www.maswaba.de/lesserphp
8
 *
9
 * LESS CSS compiler, adapted from http://lesscss.org
10
 *
11
 * Copyright 2013, Leaf Corcoran <[email protected]>
12
 * Copyright 2016, Marcus Schwarz <[email protected]>
13
 * Licensed under MIT or GPLv3, see LICENSE
14
 * @package LesserPhp
15
 */
16
class Classic implements FormatterInterface
17
{
18
19
    /**
20
     * @var string
21
     */
22
    private $indentChar = '  ';
23
24
    /**
25
     * @var string
26
     */
27
    private $break = "\n";
28
29
    /**
30
     * @var string
31
     */
32
    private $open = ' {';
33
34
    /**
35
     * @var string
36
     */
37
    private $close = '}';
38
39
    /**
40
     * @var string
41
     */
42
    private $selectorSeparator = ', ';
43
44
    /**
45
     * @var string
46
     */
47
    private $assignSeparator = ':';
48
49
    /**
50
     * @var string
51
     */
52
    private $openSingle = ' { ';
53
54
    /**
55
     * @var string
56
     */
57
    private $closeSingle = ' }';
58
59
    /**
60
     * @var bool
61
     */
62
    private $disableSingle = false;
63
64
    /**
65
     * @var bool
66
     */
67
    private $breakSelectors = false;
68
69
    /**
70
     * @var bool
71
     */
72
    private $compressColors = false;
73
74
    /**
75
     * @var int
76
     */
77
    private $indentLevel = 0;
78
79
    /**
80
     * @param int $n
81
     *
82
     * @return string
83
     */
84 38
    public function indentStr($n = 0)
85
    {
86 38
        return str_repeat($this->indentChar, max($this->indentLevel + $n, 0));
87
    }
88
89
    /**
90
     * @param string $name
91
     * @param string $value
92
     *
93
     * @return string string
94
     */
95 37
    public function property($name, $value)
96
    {
97 37
        return $name . $this->assignSeparator . $value . ';';
98
    }
99
100
    /**
101
     * @param $block
102
     *
103
     * @return bool
104
     */
105 38
    protected function isEmpty($block)
106
    {
107 38
        if (empty($block->lines)) {
108 34
            foreach ($block->children as $child) {
109 33
                if (!$this->isEmpty($child)) {
110 33
                    return false;
111
                }
112
            }
113
114 10
            return true;
115
        }
116
117 38
        return false;
118
    }
119
120
    /**
121
     * @param $block
122
     *
123
     * @return void
124
     */
125 38
    public function block($block)
126
    {
127 38
        if ($this->isEmpty($block)) {
128 10
            return;
129
        }
130
131 38
        $inner = $pre = $this->indentStr();
132
133 38
        $isSingle = !$this->disableSingle &&
134 38
            $block->type === null && count($block->lines) === 1;
135
136 38
        if (!empty($block->selectors)) {
137 35
            $this->indentLevel++;
138
139 35
            if ($this->breakSelectors) {
140 35
                $selectorSeparator = $this->selectorSeparator . $this->break . $pre;
141
            } else {
142 1
                $selectorSeparator = $this->selectorSeparator;
143
            }
144
145 35
            echo $pre . implode($selectorSeparator, $block->selectors);
146 35
            if ($isSingle) {
147 1
                echo $this->openSingle;
148 1
                $inner = '';
149
            } else {
150 35
                echo $this->open . $this->break;
151 35
                $inner = $this->indentStr();
152
            }
153
        }
154
155 38
        if (!empty($block->lines)) {
156 38
            $glue = $this->break . $inner;
157 38
            echo $inner . implode($glue, $block->lines);
158 38
            if (!$isSingle && !empty($block->children)) {
159 6
                echo $this->break;
160
            }
161
        }
162
163 38
        foreach ($block->children as $child) {
164 35
            $this->block($child);
165
        }
166
167 38
        if (!empty($block->selectors)) {
168 35
            if (!$isSingle && empty($block->children)) {
169 35
                echo $this->break;
170
            }
171
172 35
            if ($isSingle) {
173 1
                echo $this->closeSingle . $this->break;
174
            } else {
175 35
                echo $pre . $this->close . $this->break;
176
            }
177
178 35
            $this->indentLevel--;
179
        }
180 38
    }
181
182
    /**
183
     * @return string
184
     */
185 3
    public function getSelectorSeparator()
186
    {
187 3
        return $this->selectorSeparator;
188
    }
189
190
    /**
191
     * @param string $separator
192
     */
193 49
    protected function setSelectorSeparator($separator)
194
    {
195 49
        $this->selectorSeparator = $separator;
196 49
    }
197
198
    /**
199
     * @return bool
200
     */
201 38
    public function getCompressColors()
202
    {
203 38
        return $this->compressColors;
204
    }
205
206
    /**
207
     * @param bool $compress
208
     */
209 1
    protected function setCompressColors($compress)
210
    {
211 1
        $this->compressColors = $compress;
212 1
    }
213
214
    /**
215
     * @param bool $breakSelectors
216
     */
217 49
    protected function setBreakSelectors($breakSelectors)
218
    {
219 49
        $this->breakSelectors = $breakSelectors;
220 49
    }
221
222
    /**
223
     * @param bool $disableSingle
224
     */
225 49
    protected function setDisabledSingle($disableSingle)
226
    {
227 49
        $this->disableSingle = $disableSingle;
228 49
    }
229
230
    /**
231
     * @param string $breakChar
232
     */
233 1
    protected function setBreakChar($breakChar)
234
    {
235 1
        $this->break = $breakChar;
236 1
    }
237
238
    /**
239
     * @param string $openChar
240
     */
241 1
    protected function setOpenChar($openChar)
242
    {
243 1
        $this->open = $openChar;
244 1
    }
245
246
    /**
247
     * @param string $assignOperator
248
     */
249 49
    protected function setAssignOperator($assignOperator)
250
    {
251 49
        $this->assignSeparator = $assignOperator;
252 49
    }
253
}
254