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

NodeLiterals::litteralStripTrailing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
    abstract protected function getFinalString(NodeList $list):string;
15
16
    public function __construct(string $nodeString, int $line)
17
    {
18
        parent::__construct($nodeString, $line);
19
        if (isset($nodeString[1]) && in_array($nodeString[1], ['-', '+'])) {
20
            $this->identifier = $nodeString[1];
21
        }
22
    }
23
24
    public function add(Node $child):Node
25
    {
26
        if (is_null($this->value)) $this->value = new NodeList();
27
        $candidate = $child;
28
        if (!isOneOf($child, ['NodeScalar', 'NodeBlank', 'NodeComment', 'NodeQuoted'])) {
29
            $candidate = new NodeScalar($child->raw, $child->line);
0 ignored issues
show
Bug introduced by
It seems like $child->raw can also be of type null; however, parameter $nodeString of Dallgoot\Yaml\NodeScalar::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

29
            $candidate = new NodeScalar(/** @scrutinizer ignore-type */ $child->raw, $child->line);
Loading history...
30
        }
31
        return parent::add($candidate);
32
    }
33
34
    protected static function litteralStripLeading(NodeList &$list)
35
    {
36
        $list->rewind();
37
        while ($list->bottom() instanceof NodeBlank) {//remove leading blank
38
            $list->shift();
39
            $list->rewind();
40
        }
41
        $list->rewind();
42
    }
43
44
    protected static function litteralStripTrailing(NodeList &$list)
45
    {
46
        $list->rewind();
47
        while ($list->top() instanceof NodeBlank) {//remove trailing blank
48
            $list->pop();
49
        }
50
        $list->rewind();
51
    }
52
53
    /**
54
     * Builds a litteral (folded or not)
55
     * As per Documentation : 8.1.1.2. Block Chomping Indicator
56
     * Chomping controls how final line breaks and trailing empty lines are interpreted.
57
     * YAML provides three chomping methods:
58
     *   Clip (default behavior)  : FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
59
     *   Strip (“-” chomping indicator)  NO FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
60
     *   Keep (“+” chomping indicator)  FINAL_LINE_BREAK && TRAILING EMPTY LINES
61
     */
62
    public function build(&$parent = null)
63
    {
64
        $result = '';
65
        if (!is_null($this->_tag)) {
66
            return TagFactory::transform($this->_tag, $this->value)->build($parent);
0 ignored issues
show
Bug introduced by
It seems like $this->value can also be of type null; however, parameter $value of Dallgoot\Yaml\TagFactory::transform() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

66
            return TagFactory::transform($this->_tag, /** @scrutinizer ignore-type */ $this->value)->build($parent);
Loading history...
67
        }
68
        if (!is_null($this->value)) {
69
            $tmp = $this->getFinalString($this->value->filterComment());
0 ignored issues
show
Bug introduced by
The method filterComment() does not exist on Dallgoot\Yaml\Node. ( Ignorable by Annotation )

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

69
            $tmp = $this->getFinalString($this->value->/** @scrutinizer ignore-call */ filterComment());

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...
70
            $result = $this->identifier === '-' ? $tmp : $tmp."\n";
71
        }
72
        if ($this->_parent instanceof NodeRoot) {
73
            $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

73
            $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...
74
        } else {
75
            return $result;
76
        }
77
    }
78
79
    protected function getChildValue(Node $child, int $refIndent):string
80
    {
81
        $value = $child->value;
82
        $start = '';
83
        if (is_null($value)) {
84
            return $child instanceof NodeQuoted ? $child->build() : ltrim($child->raw);
85
        } elseif ($value instanceof Node) {
86
            if ($child instanceof NodeKey || $child instanceof NodeItem) {
87
                $start = ltrim($child->raw)."\n";
0 ignored issues
show
Unused Code introduced by
The assignment to $start is dead and can be removed.
Loading history...
88
            }
89
            return $this->getFinalString(new NodeList($value), $refIndent);
0 ignored issues
show
Unused Code introduced by
The call to Dallgoot\Yaml\NodeLiterals::getFinalString() has too many arguments starting with $refIndent. ( Ignorable by Annotation )

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

89
            return $this->/** @scrutinizer ignore-call */ getFinalString(new NodeList($value), $refIndent);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
90
        } elseif ($value instanceof NodeList) {
0 ignored issues
show
introduced by
$value is always a sub-type of Dallgoot\Yaml\NodeList.
Loading history...
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 '';
97
    }
98
99
    public function isAwaitingChild(Node $node):bool
100
    {
101
        return true;
102
    }
103
}