Literals   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 26
eloc 46
dl 0
loc 100
ccs 39
cts 48
cp 0.8125
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A litteralStripTrailing() 0 7 3
A add() 0 8 3
A build() 0 16 6
A isAwaitingChild() 0 3 1
A __construct() 0 5 3
B getChildValue() 0 18 7
A litteralStripLeading() 0 7 3
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 7
    public function __construct(string $nodeString, int $line)
30
    {
31 7
        parent::__construct($nodeString, $line);
32 7
        if (isset($nodeString[1]) && in_array($nodeString[1], ['-', '+'])) {
33 7
            $this->identifier = $nodeString[1];
34
        }
35
    }
36
37 2
    public function add(NodeGeneric $child): NodeGeneric
38
    {
39 2
        if (is_null($this->value)) $this->value = new NodeList();
40 2
        $candidate = $child;
41 2
        if (!$child->isOneOf('Scalar', 'Blank', 'Comment', 'Quoted')) {
42 1
            $candidate = new Scalar((string) $child->raw, $child->line);
43
        }
44 2
        return parent::add($candidate);
45
    }
46
47 2
    protected static function litteralStripLeading(NodeList &$list): void
48
    {
49 2
        $list->rewind();
50 2
        while (!$list->isEmpty() && $list->bottom() instanceof Blank) { //remove leading blank
51 1
            $list->shift();
52
        }
53 2
        $list->rewind();
54
    }
55
56 2
    protected static function litteralStripTrailing(NodeList &$list): void
57
    {
58 2
        $list->rewind();
59 2
        while (!$list->isEmpty() && $list->top() instanceof Blank) { //remove trailing blank
60 1
            $list->pop();
61
        }
62 2
        $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 1
    public function build(&$parent = null)
75
    {
76 1
        $result = '';
77 1
        if (!is_null($this->tag)) {
78
            $output = TagFactory::transform($this->tag, $this->value);
79
            return $output instanceof Tagged ? $output : $output->build($parent);
80
        }
81 1
        if (!is_null($this->value)) {
82 1
            $tmp = $this->getFinalString($this->value->filterComment());
83 1
            $result = $this->identifier === '-' ? $tmp : $tmp . "\n";
84
        }
85 1
        if ($this->_parent instanceof Root) {
86
            $this->_parent->/** @scrutinizer ignore-call */ getYamlObject()->setText($result);
87
            return null;
88
        } else {
89 1
            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 2
    protected function getChildValue(NodeGeneric $child, ?int $refIndent = 0): string
99
    {
100 2
        $value = $child->value;
101 2
        $start = '';
102 2
        if (is_null($value)) {
103 2
            if ($child instanceof Quoted) {
104 1
                return $child->build();
105 2
            } elseif ($child instanceof Blank) {
106 1
                return '';
107
            } else {
108 2
                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(/** @scrutinizer ignore-type */$value, $refIndent);
116
    }
117
118 1
    public function isAwaitingChild(NodeGeneric $node): bool
119
    {
120 1
        return true;
121
    }
122
}
123