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