@@ -261,8 +261,8 @@ |
||
261 | 261 | if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
262 | 262 | if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
263 | 263 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
264 | - Y::QUOTED => substr($v, 1, -1), |
|
265 | - Y::RAW => strval($v)]; |
|
264 | + Y::QUOTED => substr($v, 1, -1), |
|
265 | + Y::RAW => strval($v)]; |
|
266 | 266 | if (isset($expected[$this->type])) { |
267 | 267 | return $expected[$this->type]; |
268 | 268 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | } |
89 | 89 | $this->value->push($child); |
90 | 90 | //modify type according to child |
91 | - if ($child->type & (Y::COMMENT | Y::KEY)) $this->value->type = Y::MAPPING; |
|
91 | + if ($child->type & (Y::COMMENT|Y::KEY)) $this->value->type = Y::MAPPING; |
|
92 | 92 | if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
93 | 93 | if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
94 | 94 | } |
@@ -146,8 +146,8 @@ discard block |
||
146 | 146 | if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
147 | 147 | if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
148 | 148 | // Note : php don't like '?' as an array key -_- |
149 | - if($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
150 | - $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
|
149 | + if ($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
150 | + $characters = ['#' => [Y::COMMENT, ltrim($v)], |
|
151 | 151 | "-" => $this->onHyphen($nodeValue), |
152 | 152 | '%' => [Y::DIRECTIVE, ltrim($v)], |
153 | 153 | ':' => [Y::SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)], |
@@ -258,8 +258,8 @@ discard block |
||
258 | 258 | { |
259 | 259 | $v = &$this->value; |
260 | 260 | if (is_null($v)) return null; |
261 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
262 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
261 | + if ($this->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar($v); |
|
262 | + if ($this->type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
263 | 263 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
264 | 264 | Y::QUOTED => substr($v, 1, -1), |
265 | 265 | Y::RAW => strval($v)]; |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | { |
313 | 313 | $out = new Compact(); |
314 | 314 | if ($type === Y::COMPACT_SEQUENCE) { |
315 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
315 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
316 | 316 | //TODO : that's not robust enough, improve it |
317 | 317 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
318 | 318 | $out[$key] = $value; |
@@ -59,7 +59,9 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function getParent(int $indent = null):Node |
61 | 61 | { |
62 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
62 | + if (!is_int($indent)) { |
|
63 | + return $this->parent ?? $this; |
|
64 | + } |
|
63 | 65 | $cursor = $this; |
64 | 66 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
65 | 67 | $cursor = $cursor->parent; |
@@ -88,9 +90,15 @@ discard block |
||
88 | 90 | } |
89 | 91 | $this->value->push($child); |
90 | 92 | //modify type according to child |
91 | - if ($child->type & (Y::COMMENT | Y::KEY)) $this->value->type = Y::MAPPING; |
|
92 | - if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
|
93 | - if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
|
93 | + if ($child->type & (Y::COMMENT | Y::KEY)) { |
|
94 | + $this->value->type = Y::MAPPING; |
|
95 | + } |
|
96 | + if ($child->type & Y::ITEM) { |
|
97 | + $this->value->type = Y::SEQUENCE; |
|
98 | + } |
|
99 | + if ($this->type & Y::LITTERALS) { |
|
100 | + $this->value->type = $this->type; |
|
101 | + } |
|
94 | 102 | } |
95 | 103 | } |
96 | 104 | |
@@ -143,10 +151,16 @@ discard block |
||
143 | 151 | $type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL; |
144 | 152 | return [$type, $nodeValue]; |
145 | 153 | } |
146 | - if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
147 | - if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
154 | + if (in_array($first, ['{', '['])) { |
|
155 | + return $this->onObject($nodeValue); |
|
156 | + } |
|
157 | + if (in_array($first, ['!', '&', '*'])) { |
|
158 | + return $this->onNodeAction($nodeValue); |
|
159 | + } |
|
148 | 160 | // Note : php don't like '?' as an array key -_- |
149 | - if($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
161 | + if($first === '?') { |
|
162 | + return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
163 | + } |
|
150 | 164 | $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
151 | 165 | "-" => $this->onHyphen($nodeValue), |
152 | 166 | '%' => [Y::DIRECTIVE, ltrim($v)], |
@@ -198,9 +212,15 @@ discard block |
||
198 | 212 | private function onObject($value):array |
199 | 213 | { |
200 | 214 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
201 | - if (json_last_error() === JSON_ERROR_NONE) return [Y::JSON, $value]; |
|
202 | - if (preg_match(R::MAPPING, $value)) return [Y::COMPACT_MAPPING, $value]; |
|
203 | - if (preg_match(R::SEQUENCE, $value)) return [Y::COMPACT_SEQUENCE, $value]; |
|
215 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
216 | + return [Y::JSON, $value]; |
|
217 | + } |
|
218 | + if (preg_match(R::MAPPING, $value)) { |
|
219 | + return [Y::COMPACT_MAPPING, $value]; |
|
220 | + } |
|
221 | + if (preg_match(R::SEQUENCE, $value)) { |
|
222 | + return [Y::COMPACT_SEQUENCE, $value]; |
|
223 | + } |
|
204 | 224 | return [Y::PARTIAL, $value]; |
205 | 225 | } |
206 | 226 | |
@@ -215,7 +235,9 @@ discard block |
||
215 | 235 | { |
216 | 236 | if (substr($nodeValue, 0, 3) === '---') { |
217 | 237 | $rest = trim(substr($nodeValue, 3)); |
218 | - if (empty($rest)) return [Y::DOC_START, null]; |
|
238 | + if (empty($rest)) { |
|
239 | + return [Y::DOC_START, null]; |
|
240 | + } |
|
219 | 241 | $n = new Node($rest, $this->line); |
220 | 242 | $n->indent = $this->indent + 4; |
221 | 243 | return [Y::DOC_START, $n->setParent($this)]; |
@@ -257,9 +279,15 @@ discard block |
||
257 | 279 | public function getPhpValue() |
258 | 280 | { |
259 | 281 | $v = &$this->value; |
260 | - if (is_null($v)) return null; |
|
261 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
262 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
282 | + if (is_null($v)) { |
|
283 | + return null; |
|
284 | + } |
|
285 | + if ($this->type & (Y::REF_CALL | Y::SCALAR)) { |
|
286 | + return self::getScalar($v); |
|
287 | + } |
|
288 | + if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) { |
|
289 | + return self::getCompact(substr($v, 1, -1), $this->type); |
|
290 | + } |
|
263 | 291 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
264 | 292 | Y::QUOTED => substr($v, 1, -1), |
265 | 293 | Y::RAW => strval($v)]; |
@@ -288,9 +316,15 @@ discard block |
||
288 | 316 | '-.inf' => -INF, |
289 | 317 | '.nan' => NAN |
290 | 318 | ]; |
291 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
292 | - if (R::isDate($v)) return date_create($v); |
|
293 | - if (R::isNumber($v)) return self::getNumber($v); |
|
319 | + if (isset($types[strtolower($v)])) { |
|
320 | + return $types[strtolower($v)]; |
|
321 | + } |
|
322 | + if (R::isDate($v)) { |
|
323 | + return date_create($v); |
|
324 | + } |
|
325 | + if (R::isNumber($v)) { |
|
326 | + return self::getNumber($v); |
|
327 | + } |
|
294 | 328 | return strval($v); |
295 | 329 | } |
296 | 330 | |
@@ -303,8 +337,12 @@ discard block |
||
303 | 337 | */ |
304 | 338 | private static function getNumber(string $v) |
305 | 339 | { |
306 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
307 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
340 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
341 | + return intval(base_convert($v, 8, 10)); |
|
342 | + } |
|
343 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
344 | + return intval(base_convert($v, 16, 10)); |
|
345 | + } |
|
308 | 346 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
309 | 347 | } |
310 | 348 |
@@ -65,12 +65,12 @@ |
||
65 | 65 | return self::$_root->getReference($identifier); |
66 | 66 | } |
67 | 67 | $typesActions = [Y::COMMENT => 'buildComment', |
68 | - Y::DIRECTIVE => 'buildDirective', |
|
69 | - Y::ITEM => 'buildItem', |
|
70 | - Y::KEY => 'buildKey', |
|
71 | - Y::SET_KEY => 'buildSetKey', |
|
72 | - Y::SET_VALUE => 'buildSetValue', |
|
73 | - Y::TAG => 'buildTag', |
|
68 | + Y::DIRECTIVE => 'buildDirective', |
|
69 | + Y::ITEM => 'buildItem', |
|
70 | + Y::KEY => 'buildKey', |
|
71 | + Y::SET_KEY => 'buildSetKey', |
|
72 | + Y::SET_VALUE => 'buildSetValue', |
|
73 | + Y::TAG => 'buildTag', |
|
74 | 74 | ]; |
75 | 75 | if (isset($typesActions[$type])) { |
76 | 76 | return self::{$typesActions[$type]}($node, $parent); |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | |
29 | 29 | private static function buildNodeList(NodeList $node, &$parent) |
30 | 30 | { |
31 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
31 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
32 | 32 | return self::buildLitteral($node, $node->type); |
33 | 33 | } |
34 | 34 | $p = $parent; |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | private static function buildNode(Node $node, &$parent) |
56 | 56 | { |
57 | 57 | extract((array) $node, EXTR_REFS); |
58 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
58 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
59 | 59 | if (is_object($value)) { |
60 | 60 | $tmp = self::build($value, $parent) ?? $parent; |
61 | 61 | } else { |
@@ -151,15 +151,15 @@ discard block |
||
151 | 151 | { |
152 | 152 | self::$_root = new YamlObject(); |
153 | 153 | $childTypes = $list->getTypes(); |
154 | - $isaMapping = (bool) (Y::KEY | Y::MAPPING) & $childTypes; |
|
154 | + $isaMapping = (bool) (Y::KEY|Y::MAPPING)&$childTypes; |
|
155 | 155 | $isaSequence = (bool) Y::ITEM & $childTypes; |
156 | 156 | $isaSet = (bool) Y::SET_VALUE & $childTypes; |
157 | 157 | if ($isaMapping && $isaSequence) { |
158 | 158 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
159 | 159 | } else { |
160 | 160 | switch (true) { |
161 | - case $isaSequence: $list->type = Y::SEQUENCE;break; |
|
162 | - case $isaSet: $list->type = Y::SET;break; |
|
161 | + case $isaSequence: $list->type = Y::SEQUENCE; break; |
|
162 | + case $isaSet: $list->type = Y::SET; break; |
|
163 | 163 | default: $list->type = Y::MAPPING; |
164 | 164 | } |
165 | 165 | } |
@@ -186,15 +186,15 @@ discard block |
||
186 | 186 | $lines[] = self::buildLitteral($child->value, $type); |
187 | 187 | } else { |
188 | 188 | $prefix = ''; |
189 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
189 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
190 | 190 | $prefix = "\n"; |
191 | 191 | } |
192 | 192 | $lines[] = $prefix.$child->value; |
193 | 193 | } |
194 | 194 | } |
195 | - if ($type & Y::RAW) return implode('', $lines); |
|
196 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
197 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
195 | + if ($type&Y::RAW) return implode('', $lines); |
|
196 | + if ($type&Y::LITT) return implode("\n", $lines); |
|
197 | + if ($type&Y::LITT_FOLDED) return implode(' ', $lines); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | private function buildSetKey(Node $node, $parent):void |
@@ -22,7 +22,9 @@ discard block |
||
22 | 22 | |
23 | 23 | private static function build(object $node, &$parent = null) |
24 | 24 | { |
25 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
25 | + if ($node instanceof NodeList) { |
|
26 | + return self::buildNodeList($node, $parent); |
|
27 | + } |
|
26 | 28 | return self::buildNode($node, $parent); |
27 | 29 | } |
28 | 30 | |
@@ -61,7 +63,9 @@ discard block |
||
61 | 63 | } else { |
62 | 64 | $tmp = $node->getPhpValue(); |
63 | 65 | } |
64 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
66 | + if ($type === Y::REF_DEF) { |
|
67 | + self::$_root->addReference($identifier, $tmp); |
|
68 | + } |
|
65 | 69 | return self::$_root->getReference($identifier); |
66 | 70 | } |
67 | 71 | $typesActions = [Y::COMMENT => 'buildComment', |
@@ -137,10 +141,14 @@ discard block |
||
137 | 141 | } |
138 | 142 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
139 | 143 | foreach ($_root->value as $child) { |
140 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
144 | + if ($child->type & Y::DOC_START) { |
|
145 | + $totalDocStart++; |
|
146 | + } |
|
141 | 147 | //if 0 or 1 DOC_START = we are still in first document |
142 | 148 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
143 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
149 | + if (!isset($documents[$currentDoc])) { |
|
150 | + $documents[$currentDoc] = new NodeList(); |
|
151 | + } |
|
144 | 152 | $documents[$currentDoc]->push($child); |
145 | 153 | } |
146 | 154 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -192,16 +200,23 @@ discard block |
||
192 | 200 | $lines[] = $prefix.$child->value; |
193 | 201 | } |
194 | 202 | } |
195 | - if ($type & Y::RAW) return implode('', $lines); |
|
196 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
197 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
203 | + if ($type & Y::RAW) { |
|
204 | + return implode('', $lines); |
|
205 | + } |
|
206 | + if ($type & Y::LITT) { |
|
207 | + return implode("\n", $lines); |
|
208 | + } |
|
209 | + if ($type & Y::LITT_FOLDED) { |
|
210 | + return implode(' ', $lines); |
|
211 | + } |
|
198 | 212 | } |
199 | 213 | |
200 | 214 | private function buildSetKey(Node $node, $parent):void |
201 | 215 | { |
202 | 216 | $key = json_encode(self::build($node->value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
203 | - if (empty($key)) |
|
204 | - throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
217 | + if (empty($key)) { |
|
218 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
219 | + } |
|
205 | 220 | $parent->{$key} = null; |
206 | 221 | } |
207 | 222 | |
@@ -226,8 +241,11 @@ discard block |
||
226 | 241 | } |
227 | 242 | //TODO: have somewhere a list of common tags and their treatment |
228 | 243 | if (in_array($node->identifier, ['!binary', '!str'])) { |
229 | - if ($node->value->value instanceof NodeList) $node->value->value->type = Y::RAW; |
|
230 | - else $node->value->type = Y::RAW; |
|
244 | + if ($node->value->value instanceof NodeList) { |
|
245 | + $node->value->value->type = Y::RAW; |
|
246 | + } else { |
|
247 | + $node->value->type = Y::RAW; |
|
248 | + } |
|
231 | 249 | } |
232 | 250 | $val = is_null($node->value) ? null : self::build(/** @scrutinizer ignore-type */ $node->value, $node); |
233 | 251 | return new Tag($node->identifier, $val); |
@@ -87,7 +87,9 @@ discard block |
||
87 | 87 | { |
88 | 88 | $source = $this->content ?? preg_split("/([^\n\r]+)/um", $strContent, 0, PREG_SPLIT_DELIM_CAPTURE); |
89 | 89 | //TODO : be more permissive on $strContent values |
90 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
90 | + if (!is_array($source) || !count($source)) { |
|
91 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
92 | + } |
|
91 | 93 | $previous = $root = new Node(); |
92 | 94 | $emptyLines = []; |
93 | 95 | try { |
@@ -99,7 +101,9 @@ discard block |
||
99 | 101 | foreach ($gen() as $lineNb => $lineString) { |
100 | 102 | $n = new Node($lineString, $lineNb); |
101 | 103 | if ($n->type & (Y::LITTERALS|Y::BLANK)) { |
102 | - if ($this->onSpecialType($n, $previous, $emptyLines)) continue; |
|
104 | + if ($this->onSpecialType($n, $previous, $emptyLines)) { |
|
105 | + continue; |
|
106 | + } |
|
103 | 107 | } else { |
104 | 108 | foreach ($emptyLines as $blankNode) { |
105 | 109 | $blankNode->getParent()->add($blankNode); |
@@ -113,11 +117,15 @@ discard block |
||
113 | 117 | break; |
114 | 118 | default: $target = $previous->type & Y::SCALAR ? $previous->getParent() : $previous; |
115 | 119 | } |
116 | - if ($this->onContextType($n, $target, $lineString)) continue; |
|
120 | + if ($this->onContextType($n, $target, $lineString)) { |
|
121 | + continue; |
|
122 | + } |
|
117 | 123 | $target->add($n); |
118 | 124 | $previous = $n; |
119 | 125 | } |
120 | - if ($this->debug === 2) echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
126 | + if ($this->debug === 2) { |
|
127 | + echo "\033[33mParsed Structure\033[0m\n",var_export($root, true); |
|
128 | + } |
|
121 | 129 | $out = Builder::buildContent($root, $this->debug); |
122 | 130 | return $out; |
123 | 131 | } catch (\Error|\Exception|\ParseError $e) { |
@@ -137,8 +145,12 @@ discard block |
||
137 | 145 | { |
138 | 146 | $deepest = $previous->getDeepestNode(); |
139 | 147 | if ($n->type & Y::BLANK) { |
140 | - if ($previous->type & Y::SCALAR) $emptyLines[] = $n->setParent($previous->getParent()); |
|
141 | - if ($deepest->type & Y::LITTERALS) $emptyLines[] = $n->setParent($deepest); |
|
148 | + if ($previous->type & Y::SCALAR) { |
|
149 | + $emptyLines[] = $n->setParent($previous->getParent()); |
|
150 | + } |
|
151 | + if ($deepest->type & Y::LITTERALS) { |
|
152 | + $emptyLines[] = $n->setParent($deepest); |
|
153 | + } |
|
142 | 154 | return true; |
143 | 155 | } |
144 | 156 | return false; |