@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | private static function buildNodeList(NodeList $node, &$parent) |
| 29 | 29 | { |
| 30 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
| 30 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
| 31 | 31 | return self::buildLitteral($node, $node->type); |
| 32 | 32 | } |
| 33 | 33 | $p = $parent; |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | private static function buildNode(Node $node, &$parent) |
| 55 | 55 | { |
| 56 | 56 | extract((array) $node, EXTR_REFS); |
| 57 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
| 57 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
| 58 | 58 | if (is_object($value)) { |
| 59 | 59 | $tmp = self::build($value, $parent) ?? $parent; |
| 60 | 60 | } else { |
@@ -150,15 +150,15 @@ discard block |
||
| 150 | 150 | { |
| 151 | 151 | self::$_root = new YamlObject(); |
| 152 | 152 | $childTypes = $list->getTypes(); |
| 153 | - $isaMapping = (bool) (Y::KEY | Y::MAPPING) & $childTypes; |
|
| 153 | + $isaMapping = (bool) (Y::KEY|Y::MAPPING)&$childTypes; |
|
| 154 | 154 | $isaSequence = (bool) Y::ITEM & $childTypes; |
| 155 | 155 | $isaSet = (bool) Y::SET_VALUE & $childTypes; |
| 156 | 156 | if ($isaMapping && $isaSequence) { |
| 157 | 157 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
| 158 | 158 | } else { |
| 159 | 159 | switch (true) { |
| 160 | - case $isaSequence: $list->type = Y::SEQUENCE;break; |
|
| 161 | - case $isaSet: $list->type = Y::SET;break; |
|
| 160 | + case $isaSequence: $list->type = Y::SEQUENCE; break; |
|
| 161 | + case $isaSet: $list->type = Y::SET; break; |
|
| 162 | 162 | default: $list->type = Y::MAPPING; |
| 163 | 163 | } |
| 164 | 164 | } |
@@ -185,15 +185,15 @@ discard block |
||
| 185 | 185 | $lines[] = self::buildLitteral($child->value, $type); |
| 186 | 186 | } else { |
| 187 | 187 | $prefix = ''; |
| 188 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 188 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
| 189 | 189 | $prefix = "\n"; |
| 190 | 190 | } |
| 191 | 191 | $lines[] = $prefix.$child->value; |
| 192 | 192 | } |
| 193 | 193 | } |
| 194 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 195 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 196 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 194 | + if ($type&Y::RAW) return implode('', $lines); |
|
| 195 | + if ($type&Y::LITT) return implode("\n", $lines); |
|
| 196 | + if ($type&Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 197 | 197 | return ''; |
| 198 | 198 | } |
| 199 | 199 | |
@@ -21,7 +21,9 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | private static function build(object $node, &$parent = null) |
| 23 | 23 | { |
| 24 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
| 24 | + if ($node instanceof NodeList) { |
|
| 25 | + return self::buildNodeList($node, $parent); |
|
| 26 | + } |
|
| 25 | 27 | return self::buildNode($node, $parent); |
| 26 | 28 | } |
| 27 | 29 | |
@@ -60,7 +62,9 @@ discard block |
||
| 60 | 62 | } else { |
| 61 | 63 | $tmp = $node->getPhpValue(); |
| 62 | 64 | } |
| 63 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 65 | + if ($type === Y::REF_DEF) { |
|
| 66 | + self::$_root->addReference($identifier, $tmp); |
|
| 67 | + } |
|
| 64 | 68 | return self::$_root->getReference($identifier); |
| 65 | 69 | } |
| 66 | 70 | $typesActions = [Y::COMMENT => 'buildComment', |
@@ -136,10 +140,14 @@ discard block |
||
| 136 | 140 | } |
| 137 | 141 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 138 | 142 | foreach ($_root->value as $child) { |
| 139 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 143 | + if ($child->type & Y::DOC_START) { |
|
| 144 | + $totalDocStart++; |
|
| 145 | + } |
|
| 140 | 146 | //if 0 or 1 DOC_START = we are still in first document |
| 141 | 147 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 142 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 148 | + if (!isset($documents[$currentDoc])) { |
|
| 149 | + $documents[$currentDoc] = new NodeList(); |
|
| 150 | + } |
|
| 143 | 151 | $documents[$currentDoc]->push($child); |
| 144 | 152 | } |
| 145 | 153 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -191,17 +199,24 @@ discard block |
||
| 191 | 199 | $lines[] = $prefix.$child->value; |
| 192 | 200 | } |
| 193 | 201 | } |
| 194 | - if ($type & Y::RAW) return implode('', $lines); |
|
| 195 | - if ($type & Y::LITT) return implode("\n", $lines); |
|
| 196 | - if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
|
| 202 | + if ($type & Y::RAW) { |
|
| 203 | + return implode('', $lines); |
|
| 204 | + } |
|
| 205 | + if ($type & Y::LITT) { |
|
| 206 | + return implode("\n", $lines); |
|
| 207 | + } |
|
| 208 | + if ($type & Y::LITT_FOLDED) { |
|
| 209 | + return implode(' ', $lines); |
|
| 210 | + } |
|
| 197 | 211 | return ''; |
| 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); |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | } |
| 95 | 95 | $this->value->push($child); |
| 96 | 96 | //modify type according to child |
| 97 | - if ($child->type & (Y::COMMENT | Y::KEY)) $this->value->type = Y::MAPPING; |
|
| 97 | + if ($child->type & (Y::COMMENT|Y::KEY)) $this->value->type = Y::MAPPING; |
|
| 98 | 98 | if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
| 99 | 99 | if ($this->type & Y::LITTERALS) $this->value->type = $this->type; |
| 100 | 100 | } |
@@ -158,8 +158,8 @@ discard block |
||
| 158 | 158 | if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
| 159 | 159 | if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
| 160 | 160 | // Note : php don't like '?' as an array key -_- |
| 161 | - if($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 162 | - $characters = [ '#' => [Y::COMMENT, ltrim($v)], |
|
| 161 | + if ($first === '?') return [Y::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
|
| 162 | + $characters = ['#' => [Y::COMMENT, ltrim($v)], |
|
| 163 | 163 | "-" => $this->onHyphen($nodeValue), |
| 164 | 164 | '%' => [Y::DIRECTIVE, ltrim($v)], |
| 165 | 165 | ':' => [Y::SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)], |
@@ -277,8 +277,8 @@ discard block |
||
| 277 | 277 | { |
| 278 | 278 | $v = &$this->value; |
| 279 | 279 | if (is_null($v)) return null; |
| 280 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 281 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 280 | + if ($this->type & (Y::REF_CALL|Y::SCALAR)) return self::getScalar($v); |
|
| 281 | + if ($this->type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 282 | 282 | $expected = [Y::JSON => json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), |
| 283 | 283 | Y::QUOTED => substr($v, 1, -1), |
| 284 | 284 | Y::RAW => strval($v)]; |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | //TODO : this should handle references present inside the string |
| 341 | 341 | $out = new Compact(); |
| 342 | 342 | if ($type === Y::COMPACT_SEQUENCE) { |
| 343 | - $f = function ($e) { return self::getScalar(trim($e));}; |
|
| 343 | + $f = function($e) { return self::getScalar(trim($e)); }; |
|
| 344 | 344 | //TODO : that's not robust enough, improve it |
| 345 | 345 | foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) { |
| 346 | 346 | $out[$key] = $value; |