| Conditions | 130 |
| Paths | 3558 |
| Total Lines | 396 |
| Code Lines | 240 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 102 | private function doParse(string $value, int $flags): mixed |
||
| 103 | { |
||
| 104 | $this->currentLineNb = -1; |
||
| 105 | $this->currentLine = ''; |
||
| 106 | $value = $this->cleanup($value); |
||
| 107 | $this->lines = explode("\n", $value); |
||
| 108 | $this->numberOfParsedLines = \count($this->lines); |
||
| 109 | $this->locallySkippedLineNumbers = []; |
||
| 110 | $this->totalNumberOfLines ??= $this->numberOfParsedLines; |
||
| 111 | |||
| 112 | if (!$this->moveToNextLine()) { |
||
| 113 | return null; |
||
| 114 | } |
||
| 115 | |||
| 116 | $data = []; |
||
| 117 | $context = null; |
||
| 118 | $allowOverwrite = false; |
||
| 119 | |||
| 120 | while ($this->isCurrentLineEmpty()) { |
||
| 121 | if (!$this->moveToNextLine()) { |
||
| 122 | return null; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // Resolves the tag and returns if end of the document |
||
| 127 | if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) { |
||
| 128 | return new TaggedValue($tag, ''); |
||
| 129 | } |
||
| 130 | |||
| 131 | do { |
||
| 132 | if ($this->isCurrentLineEmpty()) { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | |||
| 136 | // tab? |
||
| 137 | if ("\t" === $this->currentLine[0]) { |
||
| 138 | throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 139 | } |
||
| 140 | |||
| 141 | Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename); |
||
| 142 | |||
| 143 | $isRef = $mergeNode = false; |
||
| 144 | if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) { |
||
| 145 | if ($context && 'mapping' == $context) { |
||
| 146 | throw new ParseException('You cannot define a sequence item when in a mapping.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 147 | } |
||
| 148 | $context = 'sequence'; |
||
| 149 | |||
| 150 | if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) { |
||
| 151 | $isRef = $matches['ref']; |
||
| 152 | $this->refsBeingParsed[] = $isRef; |
||
| 153 | $values['value'] = $matches['value']; |
||
| 154 | } |
||
| 155 | |||
| 156 | if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) { |
||
| 157 | throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 158 | } |
||
| 159 | |||
| 160 | // array |
||
| 161 | if (isset($values['value']) && str_starts_with(ltrim($values['value'], ' '), '-')) { |
||
| 162 | // Inline first child |
||
| 163 | $currentLineNumber = $this->getRealCurrentLineNb(); |
||
| 164 | |||
| 165 | $sequenceIndentation = \strlen($values['leadspaces']) + 1; |
||
| 166 | $sequenceYaml = substr($this->currentLine, $sequenceIndentation); |
||
| 167 | $sequenceYaml .= "\n".$this->getNextEmbedBlock($sequenceIndentation, true); |
||
| 168 | |||
| 169 | $data[] = $this->parseBlock($currentLineNumber, rtrim($sequenceYaml), $flags); |
||
| 170 | } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || str_starts_with(ltrim($values['value'], ' '), '#')) { |
||
| 171 | $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags); |
||
| 172 | } elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) { |
||
| 173 | $data[] = new TaggedValue( |
||
| 174 | $subTag, |
||
| 175 | $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags) |
||
| 176 | ); |
||
| 177 | } else { |
||
| 178 | if ( |
||
| 179 | isset($values['leadspaces']) |
||
| 180 | && ( |
||
| 181 | '!' === $values['value'][0] |
||
| 182 | || self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches) |
||
| 183 | ) |
||
| 184 | ) { |
||
| 185 | $block = $values['value']; |
||
| 186 | if ($this->isNextLineIndented() || isset($matches['value']) && '>-' === $matches['value']) { |
||
| 187 | $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + \strlen($values['leadspaces']) + 1); |
||
| 188 | } |
||
| 189 | |||
| 190 | $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $flags); |
||
| 191 | } else { |
||
| 192 | $data[] = $this->parseValue($values['value'], $flags, $context); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | if ($isRef) { |
||
| 196 | $this->refs[$isRef] = end($data); |
||
| 197 | array_pop($this->refsBeingParsed); |
||
| 198 | } |
||
| 199 | } elseif ( |
||
| 200 | self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values) |
||
| 201 | && (!str_contains($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"])) |
||
| 202 | ) { |
||
| 203 | if ($context && 'sequence' == $context) { |
||
| 204 | throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
| 205 | } |
||
| 206 | $context = 'mapping'; |
||
| 207 | |||
| 208 | try { |
||
| 209 | $key = Inline::parseScalar($values['key']); |
||
| 210 | } catch (ParseException $e) { |
||
| 211 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 212 | $e->setSnippet($this->currentLine); |
||
| 213 | |||
| 214 | throw $e; |
||
| 215 | } |
||
| 216 | |||
| 217 | if (!\is_string($key) && !\is_int($key)) { |
||
| 218 | throw new ParseException((is_numeric($key) ? 'Numeric' : 'Non-string').' keys are not supported. Quote your evaluable mapping keys instead.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 219 | } |
||
| 220 | |||
| 221 | // Convert float keys to strings, to avoid being converted to integers by PHP |
||
| 222 | if (\is_float($key)) { |
||
| 223 | $key = (string) $key; |
||
| 224 | } |
||
| 225 | |||
| 226 | if ('<<' === $key && (!isset($values['value']) || '&' !== $values['value'][0] || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) { |
||
| 227 | $mergeNode = true; |
||
| 228 | $allowOverwrite = true; |
||
| 229 | if (isset($values['value'][0]) && '*' === $values['value'][0]) { |
||
| 230 | $refName = substr(rtrim($values['value']), 1); |
||
| 231 | if (!\array_key_exists($refName, $this->refs)) { |
||
| 232 | if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) { |
||
| 233 | throw new ParseException(sprintf('Circular reference [%s] detected for reference "%s".', implode(', ', array_merge(\array_slice($this->refsBeingParsed, $pos), [$refName])), $refName), $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
|
|
|||
| 234 | } |
||
| 235 | |||
| 236 | throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 237 | } |
||
| 238 | |||
| 239 | $refValue = $this->refs[$refName]; |
||
| 240 | |||
| 241 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $refValue instanceof \stdClass) { |
||
| 242 | $refValue = (array) $refValue; |
||
| 243 | } |
||
| 244 | |||
| 245 | if (!\is_array($refValue)) { |
||
| 246 | throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 247 | } |
||
| 248 | |||
| 249 | $data += $refValue; // array union |
||
| 250 | } else { |
||
| 251 | if (isset($values['value']) && '' !== $values['value']) { |
||
| 252 | $value = $values['value']; |
||
| 253 | } else { |
||
| 254 | $value = $this->getNextEmbedBlock(); |
||
| 255 | } |
||
| 256 | $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags); |
||
| 257 | |||
| 258 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsed instanceof \stdClass) { |
||
| 259 | $parsed = (array) $parsed; |
||
| 260 | } |
||
| 261 | |||
| 262 | if (!\is_array($parsed)) { |
||
| 263 | throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 264 | } |
||
| 265 | |||
| 266 | if (isset($parsed[0])) { |
||
| 267 | // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes |
||
| 268 | // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier |
||
| 269 | // in the sequence override keys specified in later mapping nodes. |
||
| 270 | foreach ($parsed as $parsedItem) { |
||
| 271 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsedItem instanceof \stdClass) { |
||
| 272 | $parsedItem = (array) $parsedItem; |
||
| 273 | } |
||
| 274 | |||
| 275 | if (!\is_array($parsedItem)) { |
||
| 276 | throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem, $this->filename); |
||
| 277 | } |
||
| 278 | |||
| 279 | $data += $parsedItem; // array union |
||
| 280 | } |
||
| 281 | } else { |
||
| 282 | // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the |
||
| 283 | // current mapping, unless the key already exists in it. |
||
| 284 | $data += $parsed; // array union |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) { |
||
| 288 | $isRef = $matches['ref']; |
||
| 289 | $this->refsBeingParsed[] = $isRef; |
||
| 290 | $values['value'] = $matches['value']; |
||
| 291 | } |
||
| 292 | |||
| 293 | $subTag = null; |
||
| 294 | if ($mergeNode) { |
||
| 295 | // Merge keys |
||
| 296 | } elseif (!isset($values['value']) || '' === $values['value'] || str_starts_with($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) { |
||
| 297 | // hash |
||
| 298 | // if next line is less indented or equal, then it means that the current value is null |
||
| 299 | if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) { |
||
| 300 | // Spec: Keys MUST be unique; first one wins. |
||
| 301 | // But overwriting is allowed when a merge node is used in current block. |
||
| 302 | if ($allowOverwrite || !isset($data[$key])) { |
||
| 303 | if (null !== $subTag) { |
||
| 304 | $data[$key] = new TaggedValue($subTag, ''); |
||
| 305 | } else { |
||
| 306 | $data[$key] = null; |
||
| 307 | } |
||
| 308 | } else { |
||
| 309 | throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 310 | } |
||
| 311 | } else { |
||
| 312 | // remember the parsed line number here in case we need it to provide some contexts in error messages below |
||
| 313 | $realCurrentLineNbKey = $this->getRealCurrentLineNb(); |
||
| 314 | $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags); |
||
| 315 | if ('<<' === $key) { |
||
| 316 | $this->refs[$refMatches['ref']] = $value; |
||
| 317 | |||
| 318 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $value instanceof \stdClass) { |
||
| 319 | $value = (array) $value; |
||
| 320 | } |
||
| 321 | |||
| 322 | $data += $value; |
||
| 323 | } elseif ($allowOverwrite || !isset($data[$key])) { |
||
| 324 | // Spec: Keys MUST be unique; first one wins. |
||
| 325 | // But overwriting is allowed when a merge node is used in current block. |
||
| 326 | if (null !== $subTag) { |
||
| 327 | $data[$key] = new TaggedValue($subTag, $value); |
||
| 328 | } else { |
||
| 329 | $data[$key] = $value; |
||
| 330 | } |
||
| 331 | } else { |
||
| 332 | throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $realCurrentLineNbKey + 1, $this->currentLine); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | } else { |
||
| 336 | $value = $this->parseValue(rtrim($values['value']), $flags, $context); |
||
| 337 | // Spec: Keys MUST be unique; first one wins. |
||
| 338 | // But overwriting is allowed when a merge node is used in current block. |
||
| 339 | if ($allowOverwrite || !isset($data[$key])) { |
||
| 340 | $data[$key] = $value; |
||
| 341 | } else { |
||
| 342 | throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | if ($isRef) { |
||
| 346 | $this->refs[$isRef] = $data[$key]; |
||
| 347 | array_pop($this->refsBeingParsed); |
||
| 348 | } |
||
| 349 | } elseif ('"' === $this->currentLine[0] || "'" === $this->currentLine[0]) { |
||
| 350 | if (null !== $context) { |
||
| 351 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 352 | } |
||
| 353 | |||
| 354 | try { |
||
| 355 | return Inline::parse($this->lexInlineQuotedString(), $flags, $this->refs); |
||
| 356 | } catch (ParseException $e) { |
||
| 357 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 358 | $e->setSnippet($this->currentLine); |
||
| 359 | |||
| 360 | throw $e; |
||
| 361 | } |
||
| 362 | } elseif ('{' === $this->currentLine[0]) { |
||
| 363 | if (null !== $context) { |
||
| 364 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 365 | } |
||
| 366 | |||
| 367 | try { |
||
| 368 | $parsedMapping = Inline::parse($this->lexInlineMapping(), $flags, $this->refs); |
||
| 369 | |||
| 370 | while ($this->moveToNextLine()) { |
||
| 371 | if (!$this->isCurrentLineEmpty()) { |
||
| 372 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | return $parsedMapping; |
||
| 377 | } catch (ParseException $e) { |
||
| 378 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 379 | $e->setSnippet($this->currentLine); |
||
| 380 | |||
| 381 | throw $e; |
||
| 382 | } |
||
| 383 | } elseif ('[' === $this->currentLine[0]) { |
||
| 384 | if (null !== $context) { |
||
| 385 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 386 | } |
||
| 387 | |||
| 388 | try { |
||
| 389 | $parsedSequence = Inline::parse($this->lexInlineSequence(), $flags, $this->refs); |
||
| 390 | |||
| 391 | while ($this->moveToNextLine()) { |
||
| 392 | if (!$this->isCurrentLineEmpty()) { |
||
| 393 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | return $parsedSequence; |
||
| 398 | } catch (ParseException $e) { |
||
| 399 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 400 | $e->setSnippet($this->currentLine); |
||
| 401 | |||
| 402 | throw $e; |
||
| 403 | } |
||
| 404 | } else { |
||
| 405 | // multiple documents are not supported |
||
| 406 | if ('---' === $this->currentLine) { |
||
| 407 | throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
| 408 | } |
||
| 409 | |||
| 410 | if (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1]) { |
||
| 411 | throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
| 412 | } |
||
| 413 | |||
| 414 | // 1-liner optionally followed by newline(s) |
||
| 415 | if (\is_string($value) && $this->lines[0] === trim($value)) { |
||
| 416 | try { |
||
| 417 | $value = Inline::parse($this->lines[0], $flags, $this->refs); |
||
| 418 | } catch (ParseException $e) { |
||
| 419 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 420 | $e->setSnippet($this->currentLine); |
||
| 421 | |||
| 422 | throw $e; |
||
| 423 | } |
||
| 424 | |||
| 425 | return $value; |
||
| 426 | } |
||
| 427 | |||
| 428 | // try to parse the value as a multi-line string as a last resort |
||
| 429 | if (0 === $this->currentLineNb) { |
||
| 430 | $previousLineWasNewline = false; |
||
| 431 | $previousLineWasTerminatedWithBackslash = false; |
||
| 432 | $value = ''; |
||
| 433 | |||
| 434 | foreach ($this->lines as $line) { |
||
| 435 | $trimmedLine = trim($line); |
||
| 436 | if ('#' === ($trimmedLine[0] ?? '')) { |
||
| 437 | continue; |
||
| 438 | } |
||
| 439 | // If the indentation is not consistent at offset 0, it is to be considered as a ParseError |
||
| 440 | if (0 === $this->offset && isset($line[0]) && ' ' === $line[0]) { |
||
| 441 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 442 | } |
||
| 443 | |||
| 444 | if (str_contains($line, ': ')) { |
||
| 445 | throw new ParseException('Mapping values are not allowed in multi-line blocks.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 446 | } |
||
| 447 | |||
| 448 | if ('' === $trimmedLine) { |
||
| 449 | $value .= "\n"; |
||
| 450 | } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) { |
||
| 451 | $value .= ' '; |
||
| 452 | } |
||
| 453 | |||
| 454 | if ('' !== $trimmedLine && str_ends_with($line, '\\')) { |
||
| 455 | $value .= ltrim(substr($line, 0, -1)); |
||
| 456 | } elseif ('' !== $trimmedLine) { |
||
| 457 | $value .= $trimmedLine; |
||
| 458 | } |
||
| 459 | |||
| 460 | if ('' === $trimmedLine) { |
||
| 461 | $previousLineWasNewline = true; |
||
| 462 | $previousLineWasTerminatedWithBackslash = false; |
||
| 463 | } elseif (str_ends_with($line, '\\')) { |
||
| 464 | $previousLineWasNewline = false; |
||
| 465 | $previousLineWasTerminatedWithBackslash = true; |
||
| 466 | } else { |
||
| 467 | $previousLineWasNewline = false; |
||
| 468 | $previousLineWasTerminatedWithBackslash = false; |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | try { |
||
| 473 | return Inline::parse(trim($value)); |
||
| 474 | } catch (ParseException) { |
||
| 475 | // fall-through to the ParseException thrown below |
||
| 476 | } |
||
| 477 | } |
||
| 478 | |||
| 479 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 480 | } |
||
| 481 | } while ($this->moveToNextLine()); |
||
| 482 | |||
| 483 | if (null !== $tag) { |
||
| 484 | $data = new TaggedValue($tag, $data); |
||
| 485 | } |
||
| 486 | |||
| 487 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && 'mapping' === $context && !\is_object($data)) { |
||
| 488 | $object = new \stdClass(); |
||
| 489 | |||
| 490 | foreach ($data as $key => $value) { |
||
| 491 | $object->$key = $value; |
||
| 492 | } |
||
| 493 | |||
| 494 | $data = $object; |
||
| 495 | } |
||
| 496 | |||
| 497 | return $data ?: null; |
||
| 498 | } |
||
| 1245 |