@@ -92,10 +92,13 @@ discard block |
||
| 92 | 92 | final private static function onCompact(string $nodeString, int $line):Node |
| 93 | 93 | { |
| 94 | 94 | $json = json_decode($nodeString, false, 512, self::JSON_OPTIONS); |
| 95 | - if (json_last_error() === \JSON_ERROR_NONE) return new NodeJSON($nodeString, $line, $json); |
|
| 96 | - elseif (preg_match(Regex::MAPPING, $nodeString)) return new NodeCompactMapping($nodeString, $line); |
|
| 97 | - elseif (preg_match(Regex::SEQUENCE, $nodeString)) return new NodeCompactSequence($nodeString, $line); |
|
| 98 | - else { |
|
| 95 | + if (json_last_error() === \JSON_ERROR_NONE) { |
|
| 96 | + return new NodeJSON($nodeString, $line, $json); |
|
| 97 | + } elseif (preg_match(Regex::MAPPING, $nodeString)) { |
|
| 98 | + return new NodeCompactMapping($nodeString, $line); |
|
| 99 | + } elseif (preg_match(Regex::SEQUENCE, $nodeString)) { |
|
| 100 | + return new NodeCompactSequence($nodeString, $line); |
|
| 101 | + } else { |
|
| 99 | 102 | return new NodePartial($nodeString, $line); |
| 100 | 103 | } |
| 101 | 104 | } |
@@ -110,9 +113,11 @@ discard block |
||
| 110 | 113 | */ |
| 111 | 114 | final private static function onHyphen(string $nodeString, int $line):Node |
| 112 | 115 | { |
| 113 | - if (substr($nodeString, 0, 3) === '---') return new NodeDocStart($nodeString, $line); |
|
| 114 | - elseif (preg_match(Regex::ITEM, $nodeString)) return new NodeItem($nodeString, $line); |
|
| 115 | - else { |
|
| 116 | + if (substr($nodeString, 0, 3) === '---') { |
|
| 117 | + return new NodeDocStart($nodeString, $line); |
|
| 118 | + } elseif (preg_match(Regex::ITEM, $nodeString)) { |
|
| 119 | + return new NodeItem($nodeString, $line); |
|
| 120 | + } else { |
|
| 116 | 121 | return new NodeScalar($nodeString, $line); |
| 117 | 122 | } |
| 118 | 123 | } |
@@ -126,10 +131,13 @@ discard block |
||
| 126 | 131 | */ |
| 127 | 132 | final private static function onNodeAction(string $nodeString, int $line):Node |
| 128 | 133 | { |
| 129 | - if ($nodeString[0] === '!') return new NodeTag($nodeString, $line); |
|
| 130 | - elseif ($nodeString[0] === '&') return new NodeRefDef($nodeString, $line); |
|
| 131 | - elseif ($nodeString[0] === '*') return new NodeRefCall($nodeString, $line); |
|
| 132 | - else { |
|
| 134 | + if ($nodeString[0] === '!') { |
|
| 135 | + return new NodeTag($nodeString, $line); |
|
| 136 | + } elseif ($nodeString[0] === '&') { |
|
| 137 | + return new NodeRefDef($nodeString, $line); |
|
| 138 | + } elseif ($nodeString[0] === '*') { |
|
| 139 | + return new NodeRefCall($nodeString, $line); |
|
| 140 | + } else { |
|
| 133 | 141 | throw new \ParseError("Not a action node !! $nodeString[0]"); |
| 134 | 142 | } |
| 135 | 143 | } |
@@ -137,9 +145,11 @@ discard block |
||
| 137 | 145 | |
| 138 | 146 | final private static function onLiteral(string $nodeString, int $line):Node |
| 139 | 147 | { |
| 140 | - if ($nodeString[0] === '>') return new NodeLitFolded($nodeString, $line); |
|
| 141 | - elseif ($nodeString[0] === '|') return new NodeLit($nodeString, $line); |
|
| 142 | - else { |
|
| 148 | + if ($nodeString[0] === '>') { |
|
| 149 | + return new NodeLitFolded($nodeString, $line); |
|
| 150 | + } elseif ($nodeString[0] === '|') { |
|
| 151 | + return new NodeLit($nodeString, $line); |
|
| 152 | + } else { |
|
| 143 | 153 | throw new \ParseError("Not a literal node !! $nodeString[0]"); |
| 144 | 154 | } |
| 145 | 155 | } |
@@ -45,7 +45,9 @@ discard block |
||
| 45 | 45 | } |
| 46 | 46 | $this->tagName = $tagName; |
| 47 | 47 | $this->raw = $raw; |
| 48 | - if (empty(self::$registeredHandlers)) $this->registerLegacyTags(); |
|
| 48 | + if (empty(self::$registeredHandlers)) { |
|
| 49 | + $this->registerLegacyTags(); |
|
| 50 | + } |
|
| 49 | 51 | } |
| 50 | 52 | |
| 51 | 53 | /** |
@@ -143,7 +145,9 @@ discard block |
||
| 143 | 145 | private final static function strHandler(object $node, object &$parent = null) |
| 144 | 146 | { |
| 145 | 147 | if ($node instanceof Node) { |
| 146 | - if ($node instanceof NodeKey) $node->build($parent); |
|
| 148 | + if ($node instanceof NodeKey) { |
|
| 149 | + $node->build($parent); |
|
| 150 | + } |
|
| 147 | 151 | return ltrim($node->raw); |
| 148 | 152 | // } elseif ($node instanceof NodeList) { |
| 149 | 153 | // return Builder::buildLitteral($node); |
@@ -41,7 +41,9 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public static function toString($dataType, int $options = null):string |
| 43 | 43 | { |
| 44 | - if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 44 | + if (is_null($dataType)) { |
|
| 45 | + throw new \Exception(self::class.": No content to convert to Yaml"); |
|
| 46 | + } |
|
| 45 | 47 | self::$options = is_int($options) ? $options : self::OPTIONS; |
| 46 | 48 | self::$result = new DLL; |
| 47 | 49 | if ($dataType instanceof YamlObject) { |
@@ -83,8 +85,12 @@ discard block |
||
| 83 | 85 | private static function dump($dataType, int $indent) |
| 84 | 86 | { |
| 85 | 87 | if (is_scalar($dataType)) { |
| 86 | - if ($dataType === INF) return '.inf'; |
|
| 87 | - if ($dataType === -INF) return '-.inf'; |
|
| 88 | + if ($dataType === INF) { |
|
| 89 | + return '.inf'; |
|
| 90 | + } |
|
| 91 | + if ($dataType === -INF) { |
|
| 92 | + return '-.inf'; |
|
| 93 | + } |
|
| 88 | 94 | switch (gettype($dataType)) { |
| 89 | 95 | case 'boolean': return $dataType ? 'true' : 'false'; |
| 90 | 96 | case 'float': //fall through |
@@ -106,7 +112,9 @@ discard block |
||
| 106 | 112 | */ |
| 107 | 113 | private static function dumpYamlObject(YamlObject $obj) |
| 108 | 114 | { |
| 109 | - if ($obj->hasDocStart() && self::$result instanceof DLL) self::$result->push("---"); |
|
| 115 | + if ($obj->hasDocStart() && self::$result instanceof DLL) { |
|
| 116 | + self::$result->push("---"); |
|
| 117 | + } |
|
| 110 | 118 | // self::dump($obj, 0); |
| 111 | 119 | if (count($obj) > 0) { |
| 112 | 120 | self::dumpArray($obj->getArrayCopy(), 0); |
@@ -182,9 +190,13 @@ discard block |
||
| 182 | 190 | self::add(self::dump($obj->value, $indent + self::INDENT), $indent + self::INDENT); |
| 183 | 191 | } |
| 184 | 192 | } |
| 185 | - if ($obj instanceof Compact) return self::dumpCompact($obj, $indent); |
|
| 193 | + if ($obj instanceof Compact) { |
|
| 194 | + return self::dumpCompact($obj, $indent); |
|
| 195 | + } |
|
| 186 | 196 | //TODO: consider dumping datetime as date strings according to a format provided by user or default |
| 187 | - if ($obj instanceof \DateTime) return $obj->format(self::DATE_FORMAT); |
|
| 197 | + if ($obj instanceof \DateTime) { |
|
| 198 | + return $obj->format(self::DATE_FORMAT); |
|
| 199 | + } |
|
| 188 | 200 | $propList = get_object_vars($obj); |
| 189 | 201 | foreach ($propList as $property => $value) { |
| 190 | 202 | if (is_scalar($value) || $value instanceof Compact || $value instanceof \DateTime) { |
@@ -31,7 +31,9 @@ |
||
| 31 | 31 | $built = is_object($this->value) ? $this->value->build($parent) : null; |
| 32 | 32 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
| 33 | 33 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
| 34 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 34 | + if (empty($key)) { |
|
| 35 | + throw new \Exception("Cant serialize complex key: ".var_export($this->value, true)); |
|
| 36 | + } |
|
| 35 | 37 | $parent->{trim($key, '\'" ')} = null; |
| 36 | 38 | } |
| 37 | 39 | |
@@ -21,8 +21,12 @@ |
||
| 21 | 21 | // } else { |
| 22 | 22 | // $this->specialProcess($previous, $emptyLines); |
| 23 | 23 | // } |
| 24 | - if ($previous instanceof NodeScalar) $emptyLines[] = $this->setParent($previous->getParent()); |
|
| 25 | - if ($deepest instanceof NodeLiterals) $emptyLines[] = $this->setParent($deepest); |
|
| 24 | + if ($previous instanceof NodeScalar) { |
|
| 25 | + $emptyLines[] = $this->setParent($previous->getParent()); |
|
| 26 | + } |
|
| 27 | + if ($deepest instanceof NodeLiterals) { |
|
| 28 | + $emptyLines[] = $this->setParent($deepest); |
|
| 29 | + } |
|
| 26 | 30 | return true; |
| 27 | 31 | } |
| 28 | 32 | |
@@ -28,7 +28,9 @@ |
||
| 28 | 28 | public static function buildReference($node, $parent) |
| 29 | 29 | { |
| 30 | 30 | $tmp = is_null($node->value) ? null : $node->value->build($parent); |
| 31 | - if ($node instanceof NodeRefDef) Builder::$_root->addReference($node->identifier, $tmp); |
|
| 31 | + if ($node instanceof NodeRefDef) { |
|
| 32 | + Builder::$_root->addReference($node->identifier, $tmp); |
|
| 33 | + } |
|
| 32 | 34 | return Builder::$_root->getReference($node->identifier); |
| 33 | 35 | } |
| 34 | 36 | } |
| 35 | 37 | \ No newline at end of file |
@@ -68,7 +68,9 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function getParent(int $indent = null, $type = 0):Node |
| 70 | 70 | { |
| 71 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
| 71 | + if (!is_int($indent)) { |
|
| 72 | + return $this->parent ?? $this; |
|
| 73 | + } |
|
| 72 | 74 | $cursor = $this; |
| 73 | 75 | while ($cursor instanceof Node |
| 74 | 76 | && $cursor->indent !== $indent |
@@ -212,11 +214,15 @@ discard block |
||
| 212 | 214 | |
| 213 | 215 | public function getValue(&$parent = null) |
| 214 | 216 | { |
| 215 | - if (is_null($this->value)) return null; |
|
| 216 | - elseif (is_string($this->value)) return $this->getScalar($this->value); |
|
| 217 | - elseif ($this->value instanceof Node) return $this->value->getValue($parent); |
|
| 218 | - elseif ($this->value instanceof NodeList) return Builder::buildNodeList($this->value, $parent); |
|
| 219 | - else { |
|
| 217 | + if (is_null($this->value)) { |
|
| 218 | + return null; |
|
| 219 | + } elseif (is_string($this->value)) { |
|
| 220 | + return $this->getScalar($this->value); |
|
| 221 | + } elseif ($this->value instanceof Node) { |
|
| 222 | + return $this->value->getValue($parent); |
|
| 223 | + } elseif ($this->value instanceof NodeList) { |
|
| 224 | + return Builder::buildNodeList($this->value, $parent); |
|
| 225 | + } else { |
|
| 220 | 226 | throw new \ParseError("Error trying to getValue of ".gettype($this->value)); |
| 221 | 227 | } |
| 222 | 228 | } |
@@ -231,8 +237,12 @@ discard block |
||
| 231 | 237 | */ |
| 232 | 238 | public static function getScalar(string $v) |
| 233 | 239 | { |
| 234 | - if (Regex::isDate($v)) return self::$dateAsString ? $v : date_create($v); |
|
| 235 | - if (Regex::isNumber($v)) return self::getNumber($v); |
|
| 240 | + if (Regex::isDate($v)) { |
|
| 241 | + return self::$dateAsString ? $v : date_create($v); |
|
| 242 | + } |
|
| 243 | + if (Regex::isNumber($v)) { |
|
| 244 | + return self::getNumber($v); |
|
| 245 | + } |
|
| 236 | 246 | $types = ['yes' => true, |
| 237 | 247 | 'no' => false, |
| 238 | 248 | 'true' => true, |
@@ -255,8 +265,12 @@ discard block |
||
| 255 | 265 | */ |
| 256 | 266 | private static function getNumber(string $v) |
| 257 | 267 | { |
| 258 | - if (preg_match(Regex::OCTAL_NUM, $v)) return intval(base_convert($v, 8, 10)); |
|
| 259 | - if (preg_match(Regex::HEX_NUM, $v)) return intval(base_convert($v, 16, 10)); |
|
| 268 | + if (preg_match(Regex::OCTAL_NUM, $v)) { |
|
| 269 | + return intval(base_convert($v, 8, 10)); |
|
| 270 | + } |
|
| 271 | + if (preg_match(Regex::HEX_NUM, $v)) { |
|
| 272 | + return intval(base_convert($v, 16, 10)); |
|
| 273 | + } |
|
| 260 | 274 | return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); |
| 261 | 275 | } |
| 262 | 276 | |
@@ -288,7 +302,9 @@ discard block |
||
| 288 | 302 | 'value' => $this->value, |
| 289 | 303 | 'raw' => $this->raw, |
| 290 | 304 | ]; |
| 291 | - if ($this->identifier) $props['identifier'] = "($this->identifier)"; |
|
| 305 | + if ($this->identifier) { |
|
| 306 | + $props['identifier'] = "($this->identifier)"; |
|
| 307 | + } |
|
| 292 | 308 | return $props; |
| 293 | 309 | } |
| 294 | 310 | } |
@@ -28,7 +28,9 @@ discard block |
||
| 28 | 28 | $tmp = clone $this; |
| 29 | 29 | $tmp->rewind(); |
| 30 | 30 | foreach ($tmp as $child) { |
| 31 | - if (is_a($child, $nodeType)) return true; |
|
| 31 | + if (is_a($child, $nodeType)) { |
|
| 32 | + return true; |
|
| 33 | + } |
|
| 32 | 34 | } |
| 33 | 35 | return false; |
| 34 | 36 | } |
@@ -40,7 +42,9 @@ discard block |
||
| 40 | 42 | foreach ($tmp as $child) { |
| 41 | 43 | if (!($child instanceof NodeComment) |
| 42 | 44 | && !($child instanceof NodeDirective) |
| 43 | - && !($child instanceof NodeDocstart && is_null($child->value)) ) return true; |
|
| 45 | + && !($child instanceof NodeDocstart && is_null($child->value)) ) { |
|
| 46 | + return true; |
|
| 47 | + } |
|
| 44 | 48 | } |
| 45 | 49 | return false; |
| 46 | 50 | } |
@@ -91,7 +91,9 @@ discard block |
||
| 91 | 91 | { |
| 92 | 92 | $source = $this->content ?? preg_split("/\n/m", preg_replace('/(\r\n|\r)/', "\n", $strContent), 0, PREG_SPLIT_DELIM_CAPTURE); |
| 93 | 93 | //TODO : be more permissive on $strContent values |
| 94 | - if (!is_array($source) || !count($source)) throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 94 | + if (!is_array($source) || !count($source)) { |
|
| 95 | + throw new \Exception(self::EXCEPTION_LINE_SPLIT); |
|
| 96 | + } |
|
| 95 | 97 | return function () use($source) { |
| 96 | 98 | foreach ($source as $key => $value) { |
| 97 | 99 | yield ++$key => $value; |
@@ -119,7 +121,9 @@ discard block |
||
| 119 | 121 | foreach ($sourceIterator as $lineNb => $lineString) { |
| 120 | 122 | $currentLine = $lineNb; |
| 121 | 123 | $node = NodeFactory::get($lineString, $lineNb); |
| 122 | - if ($node->needsSpecialProcess($previous, $emptyLines)) continue; |
|
| 124 | + if ($node->needsSpecialProcess($previous, $emptyLines)) { |
|
| 125 | + continue; |
|
| 126 | + } |
|
| 123 | 127 | $this->attachBlankLines($emptyLines, $previous); |
| 124 | 128 | $emptyLines = []; |
| 125 | 129 | switch ($node->indent <=> $previous->indent) { |
@@ -129,11 +133,16 @@ discard block |
||
| 129 | 133 | break; |
| 130 | 134 | default: $target = $node->getTargetOnMoreIndent($previous); |
| 131 | 135 | } |
| 132 | - if ($node->skipOnContext($target)) continue;//var_dump(get_class($target)); |
|
| 136 | + if ($node->skipOnContext($target)) { |
|
| 137 | + continue; |
|
| 138 | + } |
|
| 139 | + //var_dump(get_class($target)); |
|
| 133 | 140 | $target->add($node); |
| 134 | 141 | $previous = $node; |
| 135 | 142 | } |
| 136 | - if ($this->debug === 2) print_r($root); |
|
| 143 | + if ($this->debug === 2) { |
|
| 144 | + print_r($root); |
|
| 145 | + } |
|
| 137 | 146 | return Builder::buildContent($root, $this->debug); |
| 138 | 147 | } catch (\Error|\Exception|\ParseError $e) { |
| 139 | 148 | $file = $this->filePath ? realpath($this->filePath) : '#YAML STRING#'; |