Test Failed
Branch master (bee4a6)
by stéphane
14:37
created

Literals::getChildValue()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 2
1
<?php
2
3
namespace Dallgoot\Yaml\Nodes\Generic;
4
5
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric;
6
use Dallgoot\Yaml\NodeList;
7
use Dallgoot\Yaml\Tag\TagFactory;
8
use Dallgoot\Yaml\Types\Tagged;
9
use Dallgoot\Yaml\Nodes\Blank;
10
use Dallgoot\Yaml\Nodes\Quoted;
11
use Dallgoot\Yaml\Nodes\Scalar;
12
use Dallgoot\Yaml\Nodes\Root;
13
14
/**
15
 *
16
 * @author  Stéphane Rebai <[email protected]>
17
 * @license Apache 2.0
18
 * @link    https://github.com/dallgoot/yaml
19
 * @todo implement  Indentation indicator 8.1.1
20
 */
21
abstract class Literals extends NodeGeneric
22
{
23
    /** @var NodeList */
24
    public $value;
25
    public $identifier;
26
27
    abstract protected function getFinalString(NodeList $list, int $refIndent = null): string;
28
29
    public function __construct(string $nodeString, int $line)
30
    {
31
        parent::__construct($nodeString, $line);
32
        if (isset($nodeString[1]) && in_array($nodeString[1], ['-', '+'])) {
33
            $this->identifier = $nodeString[1];
34
        }
35
    }
36
37
    public function add(NodeGeneric $child): NodeGeneric
38
    {
39
        if (is_null($this->value)) $this->value = new NodeList();
40
        $candidate = $child;
41
        if (!$child->isOneOf('Scalar', 'Blank', 'Comment', 'Quoted')) {
42
            $candidate = new Scalar((string) $child->raw, $child->line);
43
        }
44
        return parent::add($candidate);
45
    }
46
47
    protected static function litteralStripLeading(NodeList &$list): void
48
    {
49
        $list->rewind();
50
        while (!$list->isEmpty() && $list->bottom() instanceof Blank) { //remove leading blank
51
            $list->shift();
52
        }
53
        $list->rewind();
54
    }
55
56
    protected static function litteralStripTrailing(NodeList &$list): void
57
    {
58
        $list->rewind();
59
        while (!$list->isEmpty() && $list->top() instanceof Blank) { //remove trailing blank
60
            $list->pop();
61
        }
62
        $list->rewind();
63
    }
64
65
    /**
66
     * Builds a litteral (folded or not)
67
     * As per Documentation : 8.1.1.2. Block Chomping Indicator
68
     * Chomping controls how final line breaks and trailing empty lines are interpreted.
69
     * YAML provides three chomping methods:
70
     *   Clip (default behavior)  : FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
71
     *   Strip (“-” chomping indicator)  NO FINAL_LINE_BREAK, NO TRAILING EMPTY LINES
72
     *   Keep (“+” chomping indicator)  FINAL_LINE_BREAK && TRAILING EMPTY LINES
73
     */
74
    public function build(&$parent = null)
75
    {
76
        $result = '';
77
        if (!is_null($this->tag)) {
78
            $output = TagFactory::transform($this->tag, $this->value);
79
            return $output instanceof Tagged ? $output : $output->build($parent);
80
        }
81
        if (!is_null($this->value)) {
82
            $tmp = $this->getFinalString($this->value->filterComment());
83
            $result = $this->identifier === '-' ? $tmp : $tmp . "\n";
84
        }
85
        if ($this->_parent instanceof Root) {
86
            $this->_parent->getYamlObject()->setText($result);
0 ignored issues
show
Bug introduced by
The method getYamlObject() does not exist on Dallgoot\Yaml\Nodes\Generic\NodeGeneric. It seems like you code against a sub-type of Dallgoot\Yaml\Nodes\Generic\NodeGeneric such as Dallgoot\Yaml\Nodes\Root. ( Ignorable by Annotation )

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

86
            $this->_parent->/** @scrutinizer ignore-call */ 
87
                            getYamlObject()->setText($result);
Loading history...
Bug introduced by
The method getYamlObject() does not exist on null. ( Ignorable by Annotation )

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

86
            $this->_parent->/** @scrutinizer ignore-call */ 
87
                            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...
87
            return null;
88
        } else {
89
            return $result;
90
        }
91
    }
92
93
    /**
94
     * Gets the correct string for child value.
95
     *
96
     * @todo       double check behaviour for KEY and ITEM
97
     */
98
    protected function getChildValue(NodeGeneric $child, ?int $refIndent = 0): string
99
    {
100
        $value = $child->value;
101
        $start = '';
102
        if (is_null($value)) {
103
            if ($child instanceof Quoted) {
104
                return $child->build();
105
            } elseif ($child instanceof Blank) {
106
                return '';
107
            } else {
108
                return ltrim($child->raw);
109
            }
110
        } elseif ($value instanceof Scalar) {
111
            $value = new NodeList($value);
112
        } elseif ($value instanceof NodeList && !($child instanceof Scalar)) {
113
            $start = ltrim($child->raw) . "\n";
114
        }
115
        return $start . $this->getFinalString($value, $refIndent);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type Dallgoot\Yaml\Nodes\Generic\NodeGeneric; however, parameter $list of Dallgoot\Yaml\Nodes\Gene...erals::getFinalString() does only seem to accept Dallgoot\Yaml\NodeList, 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

115
        return $start . $this->getFinalString(/** @scrutinizer ignore-type */ $value, $refIndent);
Loading history...
116
    }
117
118
    public function isAwaitingChild(NodeGeneric $node): bool
119
    {
120
        return true;
121
    }
122
}
123