@@ -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 | |
@@ -62,7 +64,9 @@ discard block |
||
| 62 | 64 | } else { |
| 63 | 65 | $tmp = $node->getPhpValue(); |
| 64 | 66 | } |
| 65 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
| 67 | + if ($type === Y::REF_DEF) { |
|
| 68 | + self::$_root->addReference($identifier, $tmp); |
|
| 69 | + } |
|
| 66 | 70 | return self::$_root->getReference($identifier); |
| 67 | 71 | } |
| 68 | 72 | switch ($type) { |
@@ -72,8 +76,9 @@ discard block |
||
| 72 | 76 | case Y::KEY: self::buildKey($node, $parent); return; |
| 73 | 77 | case Y::SET_KEY: |
| 74 | 78 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 75 | - if (empty($key)) |
|
| 76 | - throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 79 | + if (empty($key)) { |
|
| 80 | + throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
|
| 81 | + } |
|
| 77 | 82 | $parent->{$key} = null; |
| 78 | 83 | return; |
| 79 | 84 | case Y::SET_VALUE: |
@@ -92,8 +97,11 @@ discard block |
||
| 92 | 97 | $parent->addTag($identifier); return; |
| 93 | 98 | } else {//TODO: have somewhere a list of common tags and their treatment |
| 94 | 99 | if (in_array($identifier, ['!binary', '!str'])) { |
| 95 | - if ($value->value instanceof NodeList) $value->value->type = Y::RAW; |
|
| 96 | - else $value->type = Y::RAW; |
|
| 100 | + if ($value->value instanceof NodeList) { |
|
| 101 | + $value->value->type = Y::RAW; |
|
| 102 | + } else { |
|
| 103 | + $value->type = Y::RAW; |
|
| 104 | + } |
|
| 97 | 105 | } |
| 98 | 106 | $val = is_null($value) ? null : self::build(/** @scrutinizer ignore-type */ $value, $node); |
| 99 | 107 | return new Tag($identifier, $val); |
@@ -162,10 +170,14 @@ discard block |
||
| 162 | 170 | } |
| 163 | 171 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
| 164 | 172 | foreach ($_root->value as $child) { |
| 165 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
| 173 | + if ($child->type & Y::DOC_START) { |
|
| 174 | + $totalDocStart++; |
|
| 175 | + } |
|
| 166 | 176 | //if 0 or 1 DOC_START = we are still in first document |
| 167 | 177 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
| 168 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
| 178 | + if (!isset($documents[$currentDoc])) { |
|
| 179 | + $documents[$currentDoc] = new NodeList(); |
|
| 180 | + } |
|
| 169 | 181 | $documents[$currentDoc]->push($child); |
| 170 | 182 | } |
| 171 | 183 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
@@ -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; |
@@ -150,8 +152,12 @@ discard block |
||
| 150 | 152 | $type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL; |
| 151 | 153 | return [$type, $nodeValue]; |
| 152 | 154 | } |
| 153 | - if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
|
| 154 | - if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
|
| 155 | + if (in_array($first, ['{', '['])) { |
|
| 156 | + return $this->onObject($nodeValue); |
|
| 157 | + } |
|
| 158 | + if (in_array($first, ['!', '&', '*'])) { |
|
| 159 | + return $this->onNodeAction($nodeValue); |
|
| 160 | + } |
|
| 155 | 161 | switch ($first) { |
| 156 | 162 | case '#': return [Y::COMMENT, ltrim($v)]; |
| 157 | 163 | case "-": return $this->onHyphen($nodeValue); |
@@ -203,9 +209,15 @@ discard block |
||
| 203 | 209 | private function onObject($value):array |
| 204 | 210 | { |
| 205 | 211 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 206 | - if (json_last_error() === JSON_ERROR_NONE) return [Y::JSON, $value]; |
|
| 207 | - if (preg_match(R::MAPPING, $value)) return [Y::COMPACT_MAPPING, $value]; |
|
| 208 | - if (preg_match(R::SEQUENCE, $value)) return [Y::COMPACT_SEQUENCE, $value]; |
|
| 212 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
| 213 | + return [Y::JSON, $value]; |
|
| 214 | + } |
|
| 215 | + if (preg_match(R::MAPPING, $value)) { |
|
| 216 | + return [Y::COMPACT_MAPPING, $value]; |
|
| 217 | + } |
|
| 218 | + if (preg_match(R::SEQUENCE, $value)) { |
|
| 219 | + return [Y::COMPACT_SEQUENCE, $value]; |
|
| 220 | + } |
|
| 209 | 221 | return [Y::PARTIAL, $value]; |
| 210 | 222 | } |
| 211 | 223 | |
@@ -220,7 +232,9 @@ discard block |
||
| 220 | 232 | { |
| 221 | 233 | if (substr($nodeValue, 0, 3) === '---') { |
| 222 | 234 | $rest = trim(substr($nodeValue, 3)); |
| 223 | - if (empty($rest)) return [Y::DOC_START, null]; |
|
| 235 | + if (empty($rest)) { |
|
| 236 | + return [Y::DOC_START, null]; |
|
| 237 | + } |
|
| 224 | 238 | $n = new Node($rest, $this->line); |
| 225 | 239 | $n->indent = $this->indent + 4; |
| 226 | 240 | return [Y::DOC_START, $n->setParent($this)]; |
@@ -262,9 +276,15 @@ discard block |
||
| 262 | 276 | public function getPhpValue() |
| 263 | 277 | { |
| 264 | 278 | $v = &$this->value; |
| 265 | - if (is_null($v)) return null; |
|
| 266 | - if ($this->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($v); |
|
| 267 | - if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 279 | + if (is_null($v)) { |
|
| 280 | + return null; |
|
| 281 | + } |
|
| 282 | + if ($this->type & (Y::REF_CALL | Y::SCALAR)) { |
|
| 283 | + return self::getScalar($v); |
|
| 284 | + } |
|
| 285 | + if ($this->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) { |
|
| 286 | + return self::getCompact(substr($v, 1, -1), $this->type); |
|
| 287 | + } |
|
| 268 | 288 | switch ($this->type) { |
| 269 | 289 | case Y::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
| 270 | 290 | case Y::QUOTED: return substr($v, 1, -1); |
@@ -293,9 +313,15 @@ discard block |
||
| 293 | 313 | '-.inf' => -INF, |
| 294 | 314 | '.nan' => NAN |
| 295 | 315 | ]; |
| 296 | - if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
|
| 297 | - if (R::isDate($v)) return date_create($v); |
|
| 298 | - if (R::isNumber($v)) return self::getNumber($v); |
|
| 316 | + if (isset($types[strtolower($v)])) { |
|
| 317 | + return $types[strtolower($v)]; |
|
| 318 | + } |
|
| 319 | + if (R::isDate($v)) { |
|
| 320 | + return date_create($v); |
|
| 321 | + } |
|
| 322 | + if (R::isNumber($v)) { |
|
| 323 | + return self::getNumber($v); |
|
| 324 | + } |
|
| 299 | 325 | return strval($v); |
| 300 | 326 | } |
| 301 | 327 | |
@@ -308,8 +334,12 @@ discard block |
||
| 308 | 334 | */ |
| 309 | 335 | private static function getNumber(string $v) |
| 310 | 336 | { |
| 311 | - if (preg_match("/^(0o\d+)$/i", $v)) return intval(base_convert($v, 8, 10)); |
|
| 312 | - if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); |
|
| 337 | + if (preg_match("/^(0o\d+)$/i", $v)) { |
|
| 338 | + return intval(base_convert($v, 8, 10)); |
|
| 339 | + } |
|
| 340 | + if (preg_match("/^(0x[\da-f]+)$/i", $v)) { |
|
| 341 | + return intval(base_convert($v, 16, 10)); |
|
| 342 | + } |
|
| 313 | 343 | // TODO: remove these if not needed |
| 314 | 344 | // if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through |
| 315 | 345 | // if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v): |
@@ -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; |
| 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; |