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

NodeList::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Dallgoot\Yaml;
3
4
/**
5
 * TODO
6
 *
7
 * @author  Stéphane Rebai <[email protected]>
8
 * @license Apache 2.0
9
 * @link    TODO : url to specific online doc
10
 */
11
class NodeList extends \SplDoublyLinkedList
12
{
13
    const MAPPING   = 1;
14
    const MULTILINE = 2;
15
    const SEQUENCE  = 4;
16
    const SET       = 8;
17
18
    public $type;
19
20
    /**
21
     * NodeList constructor
22
     *
23
     * @param Node|null $node (optional) a node that will be pushed as first element
24
     */
25
    public function __construct(Node $node = null)
26
    {
27
        if (!is_null($node)) {
28
            $this->push($node);
29
        }
30
    }
31
32
    public function has(string $nodeType):bool
33
    {
34
        $tmp = clone $this;
35
        $tmp->rewind();
36
        $fqn = __NAMESPACE__."\\$nodeType";
37
        foreach ($tmp as $child) {
38
            if ($child instanceof $fqn) return true;
39
        }
40
        return false;
41
    }
42
43
    public function hasContent():bool
44
    {
45
        $tmp = clone $this;
46
        $tmp->rewind();
47
        foreach ($tmp as $child) {
48
            if (!($child instanceof NodeComment)
49
                && !($child instanceof NodeDirective)
50
                && !($child instanceof NodeDocstart && is_null($child->value)) ) return true;
51
        }
52
        return false;
53
    }
54
55
    public function push($node)
56
    {
57
        $type = null;
58
        if     ($node instanceof NodeItem )    $type = self::SEQUENCE;
59
        elseif ($node instanceof NodeKey)      $type = self::MAPPING;
60
        elseif ($node instanceof NodeSetKey
61
             || $node instanceof NodeSetValue) {
62
            $type = self::SET;
63
        } elseif ($node instanceof NodeScalar ){
64
            $type = self::MULTILINE;
65
        }
66
        if (!is_null($type) && $this->checkTypeCoherence($type)) {
67
            $this->type = $type;
68
        }
69
        parent::push($node);
70
    }
71
72
    /**
73
     * Verify that the estimated type is coherent with this list current $type
74
     *
75
     * @param      int      $estimatedType  The estimated type
76
     *
77
     * @return     boolean  True if coherent, False otherwise
78
     * @todo       implement invalid cases
79
     */
80
    public function checkTypeCoherence($estimatedType):bool
0 ignored issues
show
Unused Code introduced by
The parameter $estimatedType is not used and could be removed. ( Ignorable by Annotation )

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

80
    public function checkTypeCoherence(/** @scrutinizer ignore-unused */ $estimatedType):bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
       // if ($this->type === self::MAPPING) {
83
       //     if ($estimatedType === self::SEQUENCE) {
84
       //         throw new \ParseError("Error : no coherence in types", 1);
85
       //     }
86
       // }
87
       return true;
88
    }
89
90
    public function build(&$parent = null)
91
    {
92
        switch ($this->type) {
93
            case self::MAPPING:  //fall through
94
            case self::SET:
95
                $collect = $parent ?? new \StdClass;
96
                return $this->buildList($collect);
97
            case self::SEQUENCE:
98
                $collect = $parent ?? [];
99
                return $this->buildList($collect);
100
            default:
101
                $this->filterComment();
102
                return Builder::getScalar($this->buildMultiline());
103
        }
104
    }
105
106
    public function buildList(&$collector)
107
    {
108
        $this->rewind();
109
        foreach ($this as $child) {
110
            $child->build($collector);
111
        }
112
        return $collector;
113
    }
114
115
    public function buildMultiline():string
116
    {
117
        $collect = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $collect is dead and can be removed.
Loading history...
118
        $output = '';
119
        if ($this->count() > 0) {
120
            $this->rewind();
121
            $first = $this->shift();
122
            $output = trim($first->raw);
123
            $indent = $first->indent;
0 ignored issues
show
Unused Code introduced by
The assignment to $indent is dead and can be removed.
Loading history...
124
            foreach ($this as $child) {
125
                if ($child instanceof NodeScalar) {
126
                    $separator = $output[-1] === "\n" ? '' : ' ';
127
                    $output .= $separator.trim($child->raw);
128
                } elseif ($child instanceof NodeBlank) {
129
                    $output .= "\n";
130
                } else {
131
                    $child->build();
132
                }
133
            }
134
        }
135
        return trim($output);
136
    }
137
138
    public function filterComment():NodeList
139
    {
140
        $this->rewind();
141
        $out = new NodeList;
142
        foreach ($this as $index => $child) {
143
            if ($child instanceof NodeComment) {
144
                $child->build();
145
            } else {
146
                if($child->value instanceof NodeComment) {
147
                    $child->value->build();
148
                    $child->value = null;
149
                } elseif($child->value instanceof NodeList) {
150
                    $child->value = $child->value->filterComment();
151
                }
152
                $out->push($child);
153
            }
154
        }
155
        $out->rewind();
156
        return $out;
157
    }
158
159
    /**
160
     * Provides a slimmer output when using var_dump Note: currently PHP ignores it on SPL types
161
     * @todo activate when PHP supports it
162
     */
163
    // public function __debugInfo()
164
    // {
165
    //     return ['type'=> Y::getName($this->type), 'dllist'=> $this->dllist];
166
    // }
167
}
168