Passed
Push — master ( 7a981c...2773c2 )
by stéphane
04:34
created

Builder::litteral()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 8
nop 2
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Dallgoot\Yaml;
4
5
use Dallgoot\Yaml as Y;
6
7
/**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
8
 *
9
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
10
final class Builder
11
{
12
    private static $_root;
13
    private static $_debug;
14
15
    const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d";
16
    const INVALID_DOCUMENT = self::class.": DOCUMENT %d can NOT be a mapping AND a sequence";
17
18
19
    private static function build(object $node, &$parent = null)
1 ignored issue
show
Coding Style introduced by
Private method name "Builder::build" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
20
    {
21
        if ($node instanceof NodeList) return self::buildNodeList($node, $parent);
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
22
        return self::buildNode($node, $parent);
23
    }
24
25
    private static function buildNodeList(NodeList $node, &$parent)
0 ignored issues
show
Coding Style introduced by
Private method name "Builder::buildNodeList" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
26
    {
27
        $type = property_exists($node, "type") ? $node->type : null;
28
        if ($type & (Y\RAW | Y\LITTERALS)) {
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
29
            return self::litteral($node, $type);
30
        }
31
        $p = $parent;
32
        switch ($type) {
33
            case Y\MAPPING: //fall through
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\MAPPING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
34
            case Y\SET:      $p = new \StdClass;break;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SET was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
35
            case Y\SEQUENCE: $p = [];break;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SEQUENCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
36
            // case Y\KEY: $p = $parent;break;
37
        }
38
        $out = null;
39
        foreach ($node as $child) {
40
            $result = self::build($child, $p);
41
            if (!is_null($result)) {
42
                if (is_string($result)) {
43
                    $out .= $result.' ';
44
                } else {
45
                    return $result;
46
                }
47
            }
48
        }
49
        return is_null($out) ? $p : rtrim($out);
50
    }
51
52
    private static function buildNode(Node $node, &$parent)
0 ignored issues
show
Coding Style introduced by
Private method name "Builder::buildNode" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
53
    {
54
        list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier];
55
        switch ($type) {
56
            case Y\COMMENT: self::$_root->addComment($line, $value);return;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
57
            case Y\DIRECTIVE: return;//TODO
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\DIRECTIVE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
58
            case Y\ITEM: self::buildItem($value, $parent);return;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\ITEM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
59
            case Y\KEY:  self::buildKey($node, $parent);return;
2 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
60
            case Y\REF_DEF: //fall through
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\REF_DEF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
61
            case Y\REF_CALL://TODO: self::build returns what ?
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\REF_CALL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
62
                $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue();
63
                if ($type === Y\REF_DEF) self::$_root->addReference($identifier, $tmp);
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
64
                return self::$_root->getReference($identifier);
65
            case Y\SET_KEY:
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SET_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
66
                $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null and string; however, parameter $node of Dallgoot\Yaml\Builder::build() does only seem to accept object, 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

66
                $key = json_encode(self::build(/** @scrutinizer ignore-type */ $value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES);
Loading history...
67
                if (empty($key))
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
68
                    throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1);
69
                $parent->{$key} = null;
70
                return;
71
            case Y\SET_VALUE:
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SET_VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
72
                $prop = array_keys(get_object_vars($parent));
73
                $key = end($prop);
74
                if (property_exists($value, "type") && ($value->type & (Y\ITEM | Y\MAPPING))) {
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\MAPPING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
75
                    $p = $value->type === Y\ITEM  ? [] : new \StdClass;
76
                    self::build($value, $p);
77
                } else {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
78
                    $p = self::build($value, $parent->{$key});
79
                }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
80
                $parent->{$key} = $p;
81
                return;
82
            case Y\TAG:
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\TAG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
83
                if ($parent === self::$_root) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
84
                    $parent->addTag($identifier);return;
85
                } else {//TODO: have somewhere a list of common tags and their treatment
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
86
                    if (in_array($identifier, ['!binary', '!str'])) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
87
                        if ($value->value instanceof NodeList) $value->value->type = Y\RAW;
1 ignored issue
show
Bug introduced by
The property type does not seem to exist on Dallgoot\Yaml\NodeList.
Loading history...
Bug introduced by
The property value does not seem to exist on Dallgoot\Yaml\NodeList.
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
88
                        else $value->type = Y\RAW;
2 ignored issues
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
89
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
90
                    $val = is_null($value) ? null : self::build($value, $node);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string; however, parameter $node of Dallgoot\Yaml\Builder::build() does only seem to accept object, 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

90
                    $val = is_null($value) ? null : self::build(/** @scrutinizer ignore-type */ $value, $node);
Loading history...
91
                    return new Tag($identifier, $val);
92
                }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
93
            default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
94
                return is_object($value) ? self::build($value, $parent) : $node->getPhpValue();
95
        }
96
    }
97
98
    /**
99
     * Builds a key and set the property + value to the parent given
100
     *
101
     * @param Node $node   The node
1 ignored issue
show
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
102
     * @param object|array $parent   The parent
1 ignored issue
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
103
     *
104
     * @throws \ParseError if Key has no name(identifier)
105
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
106
    private static function buildKey($node, &$parent):void
0 ignored issues
show
Coding Style introduced by
Private method name "Builder::buildKey" must be prefixed with an underscore
Loading history...
107
    {
108
        list($identifier, $value) = [$node->identifier, $node->value];
109
        if (is_null($identifier)) {
110
            throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line));
111
        } else {
112
            if ($value instanceof Node && ($value->type & (Y\KEY | Y\ITEM))) {
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\ITEM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
113
                $parent->{$identifier} = $value->type & Y\KEY ? new \StdClass : [];
114
                self::build($value, $parent->{$identifier});
115
            } elseif (is_object($value)) {
116
                $parent->{$identifier} = self::build($value, $parent->{$identifier});
117
            } else {
118
                $parent->{$identifier} = $node->getPhpValue();
119
            }
120
        }
121
    }
122
123
    private static function buildItem($value, &$parent):void
0 ignored issues
show
Coding Style introduced by
Private method name "Builder::buildItem" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
124
    {
125
        if(!is_array($parent) && !($parent instanceof \ArrayIterator)) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
126
            throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1);
127
        }
128
        if ($value instanceof Node && $value->type === Y\KEY) {
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
129
            $parent[$value->identifier] = self::build($value->value, $parent[$value->identifier]);
0 ignored issues
show
Bug introduced by
It seems like $value->value can also be of type null and string; however, parameter $node of Dallgoot\Yaml\Builder::build() does only seem to accept object, 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

129
            $parent[$value->identifier] = self::build(/** @scrutinizer ignore-type */ $value->value, $parent[$value->identifier]);
Loading history...
130
        } else {
131
            $index = count($parent);
132
            $parent[$index] = self::build($value, $parent[$index]);
133
        }
134
    }
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $_debug should have a doc-comment as per coding-style.
Loading history...
137
     * Builds a file.  check multiple documents & split if more than one documents
138
     *
139
     * @param Node $_root   The root node
1 ignored issue
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
140
     * @param int $debug   the level of debugging requested
2 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Doc comment for parameter $debug does not match actual variable name $_debug
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
141
     *
142
     * @return array|YamlObject   list of documents or juste one.
143
     */
144
    public static function buildContent(Node $_root, int $_debug)
145
    {
146
        self::$_debug = $_debug;
147
        $totalDocStart = 0;
148
        $documents = [];
149
        if ($_root->value instanceof Node) {
150
            $q = new NodeList;
151
            $q->push($_root->value);
152
            return [self::buildDocument($q, 0)];
153
        }
154
        $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE);
0 ignored issues
show
Bug introduced by
The method setIteratorMode() 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

154
        $_root->value->/** @scrutinizer ignore-call */ 
155
                       setIteratorMode(NodeList::IT_MODE_DELETE);

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...
155
        foreach ($_root->value as $child) {
156
            if ($child->type & Y\DOC_START) $totalDocStart++;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\DOC_START was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
157
            //if 0 or 1 DOC_START = we are still in first document
158
            $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0;
159
            if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList();
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
160
            $documents[$currentDoc]->push($child);
161
        }
162
        // $_debug >= 2 && var_dump($documents);//var_dump($documents);die("documents");
163
        $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents));
164
        return count($content) === 1 ? $content[0] : $content;
165
    }
166
167
    private static function buildDocument(NodeList $list, int $key):YamlObject
0 ignored issues
show
Coding Style introduced by
Private method name "Builder::buildDocument" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
168
    {
169
        self::$_root = new YamlObject();
170
        $childTypes = self::getChildrenTypes($list);
171
        $isMapping  = count(array_intersect($childTypes, [Y\KEY, Y\MAPPING])) > 0;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\MAPPING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
172
        $isSequence = in_array(Y\ITEM, $childTypes);
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\ITEM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
173
        $isSet      = in_array(Y\SET_VALUE, $childTypes);
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SET_VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
174
        if ($isMapping && $isSequence) {
175
            throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key));
176
        } else {
177
            switch (true) {
178
                case $isSequence: $list->type = Y\SEQUENCE;break;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SEQUENCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The property type does not seem to exist on Dallgoot\Yaml\NodeList.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
179
                case $isSet:      $list->type = Y\SET;break;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SET was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
180
                case $isMapping://fall through
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
181
                default:          $list->type = Y\MAPPING;
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
182
            }
183
        }
184
        // self::$_debug >= 3 && var_dump(self::$_root, $list);
185
        $string = '';
186
        foreach ($list as $child) {
187
            $result = self::build($child, self::$_root);
188
            if (is_string($result)) {
189
                $string .= $result.' ';
190
            }
191
        }
192
        if (!empty($string)) {
193
            self::$_root->setText(rtrim($string));
194
        }
195
        return self::$_root;
196
    }
197
198
    private static function litteral(NodeList $children, $type):string
0 ignored issues
show
Coding Style introduced by
Private method name "Builder::litteral" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
199
    {
200
        $children->rewind();
201
        $refIndent = $children->current()->indent;
202
        $separator = $type === Y\RAW ? '' : "\n";
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
203
        $action = function ($c) { return $c->value; };
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
204
        if ($type & Y\LITT_FOLDED) {
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\LITT_FOLDED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
205
            $separator = ' ';
206
            $action = function ($c) use ($refIndent) {
207
                return $c->indent > $refIndent || ($c->type & Y\BLANK) ? "\n".$c->value : $c->value;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\BLANK was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
208
            };
209
        }
210
        $tmp = [];
211
        $children->rewind();
212
        foreach ($children as $child) {
213
            $tmp[] = $action($child);
214
        }
215
        return implode($separator, $tmp);
216
    }
217
218
    private static function getChildrenTypes(NodeList $children):array
0 ignored issues
show
Coding Style introduced by
Private method name "Builder::getChildrenTypes" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
219
    {
220
        $types = [];
221
        foreach ($children as $child) {
222
            $types[] = $child->type;
223
        }
224
        return array_unique($types);
225
    }
226
}
227