Passed
Push — master ( f0c5ab...d6277d )
by stéphane
01:45
created

NodeLiterals::add()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
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
 * @todo implement  Indentation indicator 8.1.1
11
 */
12
abstract class NodeLiterals extends Node
13
{
14
    /** @var NodeList */
15
    public $value;
16
    abstract protected function getFinalString(NodeList $list, int $refIndent = null):string;
17
18
    public function __construct(string $nodeString, int $line)
19
    {
20
        parent::__construct($nodeString, $line);
21
        if (isset($nodeString[1]) && in_array($nodeString[1], ['-', '+'])) {
22
            $this->identifier = $nodeString[1];
23
        }
24
    }
25
26
    public function add(Node $child):Node
27
    {
28
        if (is_null($this->value)) $this->value = new NodeList();
29
        $candidate = $child;
30
        if (!isOneOf($child, ['NodeScalar', 'NodeBlank', 'NodeComment', 'NodeQuoted'])) {
31
            $candidate = new NodeScalar((string) $child->raw, $child->line);
32
        }
33
        return parent::add($candidate);
34
    }
35
36
    protected static function litteralStripLeading(NodeList &$list)
37
    {
38
        $list->rewind();
39
        while ($list->bottom() instanceof NodeBlank) {//remove leading blank
40
            $list->shift();
41
            $list->rewind();
42
        }
43
        $list->rewind();
44
    }
45
46
    protected static function litteralStripTrailing(NodeList &$list)
47
    {
48
        $list->rewind();
49
        while ($list->top() instanceof NodeBlank) {//remove trailing blank
50
            $list->pop();
51
        }
52
        $list->rewind();
53
    }
54
55
    /**
56
     * Builds a litteral (folded or not)
57
     * As per Documentation : 8.1.1.2. Block Chomping Indicator
58
     * Chomping controls how final line breaks and trailing empty lines are interpreted.
59
     * YAML provides three chomping methods:
60
     *   Clip (default behavior)  : FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
61
     *   Strip (“-” chomping indicator)  NO FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
62
     *   Keep (“+” chomping indicator)  FINAL_LINE_BREAK && TRAILING EMPTY LINES
63
     */
64
    public function build(&$parent = null)
65
    {
66
        $result = '';
67
        if (!is_null($this->_tag)) {
68
            return TagFactory::transform($this->_tag, $this->value)->build($parent);
69
        }
70
        if (!is_null($this->value)) {
71
            $tmp = $this->getFinalString($this->value->filterComment());
72
            $result = $this->identifier === '-' ? $tmp : $tmp."\n";
73
        }
74
        if ($this->_parent instanceof NodeRoot) {
75
            $this->getRoot()->getYamlObject()->setText($result);
0 ignored issues
show
Bug introduced by
The method getYamlObject() does not exist on Dallgoot\Yaml\NodeLiterals. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            $this->getRoot()->/** @scrutinizer ignore-call */ getYamlObject()->setText($result);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
        } else {
77
            return $result;
78
        }
79
    }
80
81
    protected function getChildValue(Node $child, int $refIndent):string
82
    {
83
        $value = $child->value;
84
        if (is_null($value)) {
85
            return $child instanceof NodeQuoted ? $child->build() : ltrim($child->raw);
86
        } else {
87
            if ($value instanceof Node) {
88
                $value = new NodeList($value);
89
            }
90
            $start = '';
91
            if ($child instanceof NodeKey || $child instanceof NodeItem) {
92
                $start = ltrim($child->raw)."\n";
93
            }
94
            return $start.$this->getFinalString($value, $refIndent);
95
        }
96
        return '';
0 ignored issues
show
Unused Code introduced by
return '' is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
97
    }
98
99
    public function isAwaitingChild(Node $node):bool
100
    {
101
        return true;
102
    }
103
}