Passed
Push — master ( f81cc4...5281ab )
by stéphane
04:50
created

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

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

74
            return TagFactory::transform($this->tag, $this->value)->/** @scrutinizer ignore-call */ build($parent);

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
        }
76 1
        if (!is_null($this->value)) {
77 1
            $tmp = $this->getFinalString($this->value->filterComment());
78 1
            $result = $this->identifier === '-' ? $tmp : $tmp."\n";
79
        }
80 1
        if ($this->_parent instanceof Root) {
81
            $this->_parent->getYamlObject()->setText($result);
82
            return null;
83
        } else {
84 1
            return $result;
85
        }
86
    }
87
88
    /**
89
     * Gets the correct string for child value.
90
     *
91
     * @param      object         $child      The child
92
     * @param      int|null       $refIndent  The reference indent
93
     *
94
     * @return     string  The child value.
95
     * @todo       double check behaviour for KEY and ITEM
96
     */
97 2
    protected function getChildValue(object $child, $refIndent=0):string
98
    {
99 2
        $value = $child->value;
100 2
        $start = '';
101 2
        if (is_null($value)) {
102 2
            if ($child instanceof Quoted) {
103 1
                return $child->build();
104 2
            } elseif ($child instanceof Blank) {
105 1
                return '';
106
            } else {
107 2
                return ltrim($child->raw);
108
            }
109
        } elseif ($value instanceof Scalar) {
110
            $value = new NodeList($value);
111
112
        } elseif ($value instanceof NodeList && !($child instanceof Scalar)) {
113
            $start = ltrim($child->raw)."\n";
114
        }
115
        return $start.$this->getFinalString($value, $refIndent);
116
    }
117
118 1
    public function isAwaitingChild(NodeGeneric $node):bool
119
    {
120 1
        return true;
121
    }
122
}