Passed
Push — master ( 3b6a73...d590ba )
by stéphane
02:55
created

NodeLiterals::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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

74
            $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...
75
        } else {
76 1
            return $result;
77
        }
78
    }
79
80
    /**
81
     * Gets the correct string for child value.
82
     *
83
     * @param      object         $child      The child
84
     * @param      integer      $refIndent  The reference indent
85
     *
86
     * @return     Node|string  The child value.
87
     * @todo       double check behaviour for KEY and ITEM
88
     */
89 2
    protected function getChildValue(object $child, int $refIndent):string
90
    {
91 2
        $value = $child->value;
92 2
        $start = '';
93 2
        if (is_null($value)) {
94 2
            if ($child instanceof NodeQuoted) {
95 1
                return $child->build();
96 2
            } elseif ($child instanceof NodeBlank) {
97 1
                return '';
98
            } else {
99 2
                return ltrim($child->raw);
100
            }
101
        } elseif ($value instanceof NodeScalar) {
102
            $value = new NodeList($value);
103
104
        } elseif ($value instanceof NodeList && !($child instanceof NodeScalar)) {
105
            $start = ltrim($child->raw)."\n";
106
        }
107
        return $start.$this->getFinalString($value, $refIndent);
108
    }
109
110 1
    public function isAwaitingChild(Node $node):bool
111
    {
112 1
        return true;
113
    }
114
}