Passed
Push — master ( 844759...f0c5ab )
by stéphane
07:52
created

NodeLitFolded::getFinalString()   B

Complexity

Conditions 10
Paths 3

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 30
rs 7.6666
c 0
b 0
f 0
cc 10
nc 3
nop 1

How to fix   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 Dallgoot\Yaml;
4
5
/**
6
 *
7
 * @author  Stéphane Rebai <[email protected]>
8
 * @license Apache 2.0
9
 * @link    TODO : url to specific online doc
10
 */
11
class NodeLitFolded extends NodeLiterals
12
{
13
    /**
14
     * @param NodeList $list The children
15
     *
16
     * @return string    The litteral.
17
     * @todo   Example 6.1. Indentation Spaces  spaces must be considered as content
18
     */
19
    public function getFinalString(NodeList $list):string
20
    {
21
        $result = '';
22
        if ($list->count()) {
23
            if ($this->identifier !== '+') {
24
                 self::litteralStripLeading($list);
25
                 self::litteralStripTrailing($list);
26
            }
27
            $first = $list->shift();
28
            $refIndent = $first->indent ?? 0;
29
            $refSeparator = ' ';
30
            $result = substr($first->raw, $first->indent);
31
            foreach ($list as $child) {
32
                if($child->indent > $refIndent || ($child instanceof NodeBlank)) {
33
                    $separator = "\n";
34
                } else {
35
                    $separator = !empty($result) && $result[-1] === "\n" ? '' : $refSeparator;
36
                }
37
                $val = '';
38
                if ($child->value instanceof NodeList) {
39
                    $val = "\n".$this->getFinalString($child->value);
40
                } else {
41
                    if ($child instanceof NodeScalar) {
42
                        $val = $child->build();
43
                    }
44
                }
45
                $result .= $separator .$val;
46
            }
47
        }
48
        return $result;
49
    }
50
}