| Total Complexity | 334 |
| Total Lines | 1236 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Parser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Parser |
||
| 25 | { |
||
| 26 | const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)'; |
||
| 27 | const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?'; |
||
| 28 | |||
| 29 | private $filename; |
||
| 30 | private $offset = 0; |
||
| 31 | private $numberOfParsedLines = 0; |
||
| 32 | private $totalNumberOfLines; |
||
| 33 | private $lines = []; |
||
| 34 | private $currentLineNb = -1; |
||
| 35 | private $currentLine = ''; |
||
| 36 | private $refs = []; |
||
| 37 | private $skippedLineNumbers = []; |
||
| 38 | private $locallySkippedLineNumbers = []; |
||
| 39 | private $refsBeingParsed = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Parses a YAML file into a PHP value. |
||
| 43 | * |
||
| 44 | * @param string $filename The path to the YAML file to be parsed |
||
| 45 | * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior |
||
| 46 | * |
||
| 47 | * @return mixed The YAML converted to a PHP value |
||
| 48 | * |
||
| 49 | * @throws ParseException If the file could not be read or the YAML is not valid |
||
| 50 | */ |
||
| 51 | public function parseFile(string $filename, int $flags = 0) |
||
| 52 | { |
||
| 53 | if (!is_file($filename)) { |
||
| 54 | throw new ParseException(sprintf('File "%s" does not exist.', $filename)); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!is_readable($filename)) { |
||
| 58 | throw new ParseException(sprintf('File "%s" cannot be read.', $filename)); |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->filename = $filename; |
||
| 62 | |||
| 63 | try { |
||
| 64 | return $this->parse(file_get_contents($filename), $flags); |
||
| 65 | } finally { |
||
| 66 | $this->filename = null; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Parses a YAML string to a PHP value. |
||
| 72 | * |
||
| 73 | * @param string $value A YAML string |
||
| 74 | * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior |
||
| 75 | * |
||
| 76 | * @return mixed A PHP value |
||
| 77 | * |
||
| 78 | * @throws ParseException If the YAML is not valid |
||
| 79 | */ |
||
| 80 | public function parse(string $value, int $flags = 0) |
||
| 81 | { |
||
| 82 | if (false === preg_match('//u', $value)) { |
||
| 83 | throw new ParseException('The YAML value does not appear to be valid UTF-8.', -1, null, $this->filename); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->refs = []; |
||
| 87 | |||
| 88 | $mbEncoding = null; |
||
| 89 | |||
| 90 | if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) { |
||
| 91 | $mbEncoding = mb_internal_encoding(); |
||
| 92 | mb_internal_encoding('UTF-8'); |
||
| 93 | } |
||
| 94 | |||
| 95 | try { |
||
| 96 | $data = $this->doParse($value, $flags); |
||
| 97 | } finally { |
||
| 98 | if (null !== $mbEncoding) { |
||
| 99 | mb_internal_encoding($mbEncoding); |
||
| 100 | } |
||
| 101 | $this->lines = []; |
||
| 102 | $this->currentLine = ''; |
||
| 103 | $this->numberOfParsedLines = 0; |
||
| 104 | $this->refs = []; |
||
| 105 | $this->skippedLineNumbers = []; |
||
| 106 | $this->locallySkippedLineNumbers = []; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $data; |
||
| 110 | } |
||
| 111 | |||
| 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 | } |
||
| 500 | |||
| 501 | private function parseBlock(int $offset, string $yaml, int $flags) |
||
| 502 | { |
||
| 503 | $skippedLineNumbers = $this->skippedLineNumbers; |
||
| 504 | |||
| 505 | foreach ($this->locallySkippedLineNumbers as $lineNumber) { |
||
| 506 | if ($lineNumber < $offset) { |
||
| 507 | continue; |
||
| 508 | } |
||
| 509 | |||
| 510 | $skippedLineNumbers[] = $lineNumber; |
||
| 511 | } |
||
| 512 | |||
| 513 | $parser = new self(); |
||
| 514 | $parser->offset = $offset; |
||
| 515 | $parser->totalNumberOfLines = $this->totalNumberOfLines; |
||
| 516 | $parser->skippedLineNumbers = $skippedLineNumbers; |
||
| 517 | $parser->refs = &$this->refs; |
||
| 518 | $parser->refsBeingParsed = $this->refsBeingParsed; |
||
| 519 | |||
| 520 | return $parser->doParse($yaml, $flags); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Returns the current line number (takes the offset into account). |
||
| 525 | * |
||
| 526 | * @internal |
||
| 527 | * |
||
| 528 | * @return int The current line number |
||
| 529 | */ |
||
| 530 | public function getRealCurrentLineNb(): int |
||
| 531 | { |
||
| 532 | $realCurrentLineNumber = $this->currentLineNb + $this->offset; |
||
| 533 | |||
| 534 | foreach ($this->skippedLineNumbers as $skippedLineNumber) { |
||
| 535 | if ($skippedLineNumber > $realCurrentLineNumber) { |
||
| 536 | break; |
||
| 537 | } |
||
| 538 | |||
| 539 | ++$realCurrentLineNumber; |
||
| 540 | } |
||
| 541 | |||
| 542 | return $realCurrentLineNumber; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Returns the current line indentation. |
||
| 547 | * |
||
| 548 | * @return int The current line indentation |
||
| 549 | */ |
||
| 550 | private function getCurrentLineIndentation(): int |
||
| 551 | { |
||
| 552 | if (' ' !== ($this->currentLine[0] ?? '')) { |
||
| 553 | return 0; |
||
| 554 | } |
||
| 555 | |||
| 556 | return \strlen($this->currentLine) - \strlen(ltrim($this->currentLine, ' ')); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Returns the next embed block of YAML. |
||
| 561 | * |
||
| 562 | * @param int|null $indentation The indent level at which the block is to be read, or null for default |
||
| 563 | * @param bool $inSequence True if the enclosing data structure is a sequence |
||
| 564 | * |
||
| 565 | * @return string A YAML string |
||
| 566 | * |
||
| 567 | * @throws ParseException When indentation problem are detected |
||
| 568 | */ |
||
| 569 | private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): string |
||
| 570 | { |
||
| 571 | $oldLineIndentation = $this->getCurrentLineIndentation(); |
||
| 572 | |||
| 573 | if (!$this->moveToNextLine()) { |
||
| 574 | return ''; |
||
| 575 | } |
||
| 576 | |||
| 577 | if (null === $indentation) { |
||
| 578 | $newIndent = null; |
||
| 579 | $movements = 0; |
||
| 580 | |||
| 581 | do { |
||
| 582 | $EOF = false; |
||
| 583 | |||
| 584 | // empty and comment-like lines do not influence the indentation depth |
||
| 585 | if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) { |
||
| 586 | $EOF = !$this->moveToNextLine(); |
||
| 587 | |||
| 588 | if (!$EOF) { |
||
| 589 | ++$movements; |
||
| 590 | } |
||
| 591 | } else { |
||
| 592 | $newIndent = $this->getCurrentLineIndentation(); |
||
| 593 | } |
||
| 594 | } while (!$EOF && null === $newIndent); |
||
| 595 | |||
| 596 | for ($i = 0; $i < $movements; ++$i) { |
||
| 597 | $this->moveToPreviousLine(); |
||
| 598 | } |
||
| 599 | |||
| 600 | $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem(); |
||
| 601 | |||
| 602 | if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) { |
||
| 603 | throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 604 | } |
||
| 605 | } else { |
||
| 606 | $newIndent = $indentation; |
||
| 607 | } |
||
| 608 | |||
| 609 | $data = []; |
||
| 610 | if ($this->getCurrentLineIndentation() >= $newIndent) { |
||
| 611 | $data[] = substr($this->currentLine, $newIndent); |
||
| 612 | } elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) { |
||
| 613 | $data[] = $this->currentLine; |
||
| 614 | } else { |
||
| 615 | $this->moveToPreviousLine(); |
||
| 616 | |||
| 617 | return ''; |
||
| 618 | } |
||
| 619 | |||
| 620 | if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) { |
||
| 621 | // the previous line contained a dash but no item content, this line is a sequence item with the same indentation |
||
| 622 | // and therefore no nested list or mapping |
||
| 623 | $this->moveToPreviousLine(); |
||
| 624 | |||
| 625 | return ''; |
||
| 626 | } |
||
| 627 | |||
| 628 | $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem(); |
||
| 629 | $isItComment = $this->isCurrentLineComment(); |
||
| 630 | |||
| 631 | while ($this->moveToNextLine()) { |
||
| 632 | if ($isItComment && !$isItUnindentedCollection) { |
||
| 633 | $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem(); |
||
| 634 | $isItComment = $this->isCurrentLineComment(); |
||
| 635 | } |
||
| 636 | |||
| 637 | $indent = $this->getCurrentLineIndentation(); |
||
| 638 | |||
| 639 | if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) { |
||
| 640 | $this->moveToPreviousLine(); |
||
| 641 | break; |
||
| 642 | } |
||
| 643 | |||
| 644 | if ($this->isCurrentLineBlank()) { |
||
| 645 | $data[] = substr($this->currentLine, $newIndent); |
||
| 646 | continue; |
||
| 647 | } |
||
| 648 | |||
| 649 | if ($indent >= $newIndent) { |
||
| 650 | $data[] = substr($this->currentLine, $newIndent); |
||
| 651 | } elseif ($this->isCurrentLineComment()) { |
||
| 652 | $data[] = $this->currentLine; |
||
| 653 | } elseif (0 == $indent) { |
||
| 654 | $this->moveToPreviousLine(); |
||
| 655 | |||
| 656 | break; |
||
| 657 | } else { |
||
| 658 | throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
| 659 | } |
||
| 660 | } |
||
| 661 | |||
| 662 | return implode("\n", $data); |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Moves the parser to the next line. |
||
| 667 | */ |
||
| 668 | private function moveToNextLine(): bool |
||
| 669 | { |
||
| 670 | if ($this->currentLineNb >= $this->numberOfParsedLines - 1) { |
||
| 671 | return false; |
||
| 672 | } |
||
| 673 | |||
| 674 | $this->currentLine = $this->lines[++$this->currentLineNb]; |
||
| 675 | |||
| 676 | return true; |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Moves the parser to the previous line. |
||
| 681 | */ |
||
| 682 | private function moveToPreviousLine(): bool |
||
| 683 | { |
||
| 684 | if ($this->currentLineNb < 1) { |
||
| 685 | return false; |
||
| 686 | } |
||
| 687 | |||
| 688 | $this->currentLine = $this->lines[--$this->currentLineNb]; |
||
| 689 | |||
| 690 | return true; |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Parses a YAML value. |
||
| 695 | * |
||
| 696 | * @param string $value A YAML value |
||
| 697 | * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior |
||
| 698 | * @param string $context The parser context (either sequence or mapping) |
||
| 699 | * |
||
| 700 | * @return mixed A PHP value |
||
| 701 | * |
||
| 702 | * @throws ParseException When reference does not exist |
||
| 703 | */ |
||
| 704 | private function parseValue(string $value, int $flags, string $context) |
||
| 705 | { |
||
| 706 | if ('*' === ($value[0] ?? '')) { |
||
| 707 | if (false !== $pos = strpos($value, '#')) { |
||
| 708 | $value = substr($value, 1, $pos - 2); |
||
| 709 | } else { |
||
| 710 | $value = substr($value, 1); |
||
| 711 | } |
||
| 712 | |||
| 713 | if (!\array_key_exists($value, $this->refs)) { |
||
| 714 | if (false !== $pos = array_search($value, $this->refsBeingParsed, true)) { |
||
| 715 | throw new ParseException(sprintf('Circular reference [%s, %s] detected for reference "%s".', implode(', ', \array_slice($this->refsBeingParsed, $pos)), $value, $value), $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
| 716 | } |
||
| 717 | |||
| 718 | throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
| 719 | } |
||
| 720 | |||
| 721 | return $this->refs[$value]; |
||
| 722 | } |
||
| 723 | |||
| 724 | if (\in_array($value[0], ['!', '|', '>'], true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) { |
||
| 725 | $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; |
||
| 726 | |||
| 727 | $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs((int) $modifiers)); |
||
| 728 | |||
| 729 | if ('' !== $matches['tag'] && '!' !== $matches['tag']) { |
||
| 730 | if ('!!binary' === $matches['tag']) { |
||
| 731 | return Inline::evaluateBinaryScalar($data); |
||
| 732 | } |
||
| 733 | |||
| 734 | return new TaggedValue(substr($matches['tag'], 1), $data); |
||
| 735 | } |
||
| 736 | |||
| 737 | return $data; |
||
| 738 | } |
||
| 739 | |||
| 740 | try { |
||
| 741 | if ('' !== $value && '{' === $value[0]) { |
||
| 742 | return Inline::parse($this->lexInlineMapping($value), $flags, $this->refs); |
||
| 743 | } elseif ('' !== $value && '[' === $value[0]) { |
||
| 744 | return Inline::parse($this->lexInlineSequence($value), $flags, $this->refs); |
||
| 745 | } |
||
| 746 | |||
| 747 | $quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null; |
||
| 748 | |||
| 749 | // do not take following lines into account when the current line is a quoted single line value |
||
| 750 | if (null !== $quotation && self::preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) { |
||
| 751 | return Inline::parse($value, $flags, $this->refs); |
||
| 752 | } |
||
| 753 | |||
| 754 | $lines = []; |
||
| 755 | |||
| 756 | while ($this->moveToNextLine()) { |
||
| 757 | // unquoted strings end before the first unindented line |
||
| 758 | if (null === $quotation && 0 === $this->getCurrentLineIndentation()) { |
||
| 759 | $this->moveToPreviousLine(); |
||
| 760 | |||
| 761 | break; |
||
| 762 | } |
||
| 763 | |||
| 764 | $lines[] = trim($this->currentLine); |
||
| 765 | |||
| 766 | // quoted string values end with a line that is terminated with the quotation character |
||
| 767 | $escapedLine = str_replace(['\\\\', '\\"'], '', $this->currentLine); |
||
| 768 | if ('' !== $escapedLine && $escapedLine[-1] === $quotation) { |
||
| 769 | break; |
||
| 770 | } |
||
| 771 | } |
||
| 772 | |||
| 773 | for ($i = 0, $linesCount = \count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) { |
||
| 774 | if ('' === $lines[$i]) { |
||
| 775 | $value .= "\n"; |
||
| 776 | $previousLineBlank = true; |
||
| 777 | } elseif ($previousLineBlank) { |
||
| 778 | $value .= $lines[$i]; |
||
| 779 | $previousLineBlank = false; |
||
| 780 | } else { |
||
| 781 | $value .= ' '.$lines[$i]; |
||
| 782 | $previousLineBlank = false; |
||
| 783 | } |
||
| 784 | } |
||
| 785 | |||
| 786 | Inline::$parsedLineNumber = $this->getRealCurrentLineNb(); |
||
| 787 | |||
| 788 | $parsedValue = Inline::parse($value, $flags, $this->refs); |
||
| 789 | |||
| 790 | if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) { |
||
| 791 | throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename); |
||
| 792 | } |
||
| 793 | |||
| 794 | return $parsedValue; |
||
| 795 | } catch (ParseException $e) { |
||
| 796 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
| 797 | $e->setSnippet($this->currentLine); |
||
| 798 | |||
| 799 | throw $e; |
||
| 800 | } |
||
| 801 | } |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Parses a block scalar. |
||
| 805 | * |
||
| 806 | * @param string $style The style indicator that was used to begin this block scalar (| or >) |
||
| 807 | * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -) |
||
| 808 | * @param int $indentation The indentation indicator that was used to begin this block scalar |
||
| 809 | */ |
||
| 810 | private function parseBlockScalar(string $style, string $chomping = '', int $indentation = 0): string |
||
| 811 | { |
||
| 812 | $notEOF = $this->moveToNextLine(); |
||
| 813 | if (!$notEOF) { |
||
| 814 | return ''; |
||
| 815 | } |
||
| 816 | |||
| 817 | $isCurrentLineBlank = $this->isCurrentLineBlank(); |
||
| 818 | $blockLines = []; |
||
| 819 | |||
| 820 | // leading blank lines are consumed before determining indentation |
||
| 821 | while ($notEOF && $isCurrentLineBlank) { |
||
| 822 | // newline only if not EOF |
||
| 823 | if ($notEOF = $this->moveToNextLine()) { |
||
| 824 | $blockLines[] = ''; |
||
| 825 | $isCurrentLineBlank = $this->isCurrentLineBlank(); |
||
| 826 | } |
||
| 827 | } |
||
| 828 | |||
| 829 | // determine indentation if not specified |
||
| 830 | if (0 === $indentation) { |
||
| 831 | $currentLineLength = \strlen($this->currentLine); |
||
| 832 | |||
| 833 | for ($i = 0; $i < $currentLineLength && ' ' === $this->currentLine[$i]; ++$i) { |
||
| 834 | ++$indentation; |
||
| 835 | } |
||
| 836 | } |
||
| 837 | |||
| 838 | if ($indentation > 0) { |
||
| 839 | $pattern = sprintf('/^ {%d}(.*)$/', $indentation); |
||
| 840 | |||
| 841 | while ( |
||
| 842 | $notEOF && ( |
||
| 843 | $isCurrentLineBlank || |
||
| 844 | self::preg_match($pattern, $this->currentLine, $matches) |
||
| 845 | ) |
||
| 846 | ) { |
||
| 847 | if ($isCurrentLineBlank && \strlen($this->currentLine) > $indentation) { |
||
| 848 | $blockLines[] = substr($this->currentLine, $indentation); |
||
| 849 | } elseif ($isCurrentLineBlank) { |
||
| 850 | $blockLines[] = ''; |
||
| 851 | } else { |
||
| 852 | $blockLines[] = $matches[1]; |
||
| 853 | } |
||
| 854 | |||
| 855 | // newline only if not EOF |
||
| 856 | if ($notEOF = $this->moveToNextLine()) { |
||
| 857 | $isCurrentLineBlank = $this->isCurrentLineBlank(); |
||
| 858 | } |
||
| 859 | } |
||
| 860 | } elseif ($notEOF) { |
||
| 861 | $blockLines[] = ''; |
||
| 862 | } |
||
| 863 | |||
| 864 | if ($notEOF) { |
||
| 865 | $blockLines[] = ''; |
||
| 866 | $this->moveToPreviousLine(); |
||
| 867 | } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) { |
||
| 868 | $blockLines[] = ''; |
||
| 869 | } |
||
| 870 | |||
| 871 | // folded style |
||
| 872 | if ('>' === $style) { |
||
| 873 | $text = ''; |
||
| 874 | $previousLineIndented = false; |
||
| 875 | $previousLineBlank = false; |
||
| 876 | |||
| 877 | for ($i = 0, $blockLinesCount = \count($blockLines); $i < $blockLinesCount; ++$i) { |
||
| 878 | if ('' === $blockLines[$i]) { |
||
| 879 | $text .= "\n"; |
||
| 880 | $previousLineIndented = false; |
||
| 881 | $previousLineBlank = true; |
||
| 882 | } elseif (' ' === $blockLines[$i][0]) { |
||
| 883 | $text .= "\n".$blockLines[$i]; |
||
| 884 | $previousLineIndented = true; |
||
| 885 | $previousLineBlank = false; |
||
| 886 | } elseif ($previousLineIndented) { |
||
| 887 | $text .= "\n".$blockLines[$i]; |
||
| 888 | $previousLineIndented = false; |
||
| 889 | $previousLineBlank = false; |
||
| 890 | } elseif ($previousLineBlank || 0 === $i) { |
||
| 891 | $text .= $blockLines[$i]; |
||
| 892 | $previousLineIndented = false; |
||
| 893 | $previousLineBlank = false; |
||
| 894 | } else { |
||
| 895 | $text .= ' '.$blockLines[$i]; |
||
| 896 | $previousLineIndented = false; |
||
| 897 | $previousLineBlank = false; |
||
| 898 | } |
||
| 899 | } |
||
| 900 | } else { |
||
| 901 | $text = implode("\n", $blockLines); |
||
| 902 | } |
||
| 903 | |||
| 904 | // deal with trailing newlines |
||
| 905 | if ('' === $chomping) { |
||
| 906 | $text = preg_replace('/\n+$/', "\n", $text); |
||
| 907 | } elseif ('-' === $chomping) { |
||
| 908 | $text = preg_replace('/\n+$/', '', $text); |
||
| 909 | } |
||
| 910 | |||
| 911 | return $text; |
||
| 912 | } |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Returns true if the next line is indented. |
||
| 916 | * |
||
| 917 | * @return bool Returns true if the next line is indented, false otherwise |
||
| 918 | */ |
||
| 919 | private function isNextLineIndented(): bool |
||
| 920 | { |
||
| 921 | $currentIndentation = $this->getCurrentLineIndentation(); |
||
| 922 | $movements = 0; |
||
| 923 | |||
| 924 | do { |
||
| 925 | $EOF = !$this->moveToNextLine(); |
||
| 926 | |||
| 927 | if (!$EOF) { |
||
| 928 | ++$movements; |
||
| 929 | } |
||
| 930 | } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment())); |
||
| 931 | |||
| 932 | if ($EOF) { |
||
| 933 | return false; |
||
| 934 | } |
||
| 935 | |||
| 936 | $ret = $this->getCurrentLineIndentation() > $currentIndentation; |
||
| 937 | |||
| 938 | for ($i = 0; $i < $movements; ++$i) { |
||
| 939 | $this->moveToPreviousLine(); |
||
| 940 | } |
||
| 941 | |||
| 942 | return $ret; |
||
| 943 | } |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Returns true if the current line is blank or if it is a comment line. |
||
| 947 | * |
||
| 948 | * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise |
||
| 949 | */ |
||
| 950 | private function isCurrentLineEmpty(): bool |
||
| 951 | { |
||
| 952 | return $this->isCurrentLineBlank() || $this->isCurrentLineComment(); |
||
| 953 | } |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Returns true if the current line is blank. |
||
| 957 | * |
||
| 958 | * @return bool Returns true if the current line is blank, false otherwise |
||
| 959 | */ |
||
| 960 | private function isCurrentLineBlank(): bool |
||
| 961 | { |
||
| 962 | return '' === $this->currentLine || '' === trim($this->currentLine, ' '); |
||
| 963 | } |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Returns true if the current line is a comment line. |
||
| 967 | * |
||
| 968 | * @return bool Returns true if the current line is a comment line, false otherwise |
||
| 969 | */ |
||
| 970 | private function isCurrentLineComment(): bool |
||
| 971 | { |
||
| 972 | //checking explicitly the first char of the trim is faster than loops or strpos |
||
| 973 | $ltrimmedLine = '' !== $this->currentLine && ' ' === $this->currentLine[0] ? ltrim($this->currentLine, ' ') : $this->currentLine; |
||
| 974 | |||
| 975 | return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0]; |
||
| 976 | } |
||
| 977 | |||
| 978 | private function isCurrentLineLastLineInDocument(): bool |
||
| 979 | { |
||
| 980 | return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1); |
||
| 981 | } |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Cleanups a YAML string to be parsed. |
||
| 985 | * |
||
| 986 | * @param string $value The input YAML string |
||
| 987 | * |
||
| 988 | * @return string A cleaned up YAML string |
||
| 989 | */ |
||
| 990 | private function cleanup(string $value): string |
||
| 991 | { |
||
| 992 | $value = str_replace(["\r\n", "\r"], "\n", $value); |
||
| 993 | |||
| 994 | // strip YAML header |
||
| 995 | $count = 0; |
||
| 996 | $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count); |
||
| 997 | $this->offset += $count; |
||
| 998 | |||
| 999 | // remove leading comments |
||
| 1000 | $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count); |
||
| 1001 | if (1 === $count) { |
||
| 1002 | // items have been removed, update the offset |
||
| 1003 | $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n"); |
||
| 1004 | $value = $trimmedValue; |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | // remove start of the document marker (---) |
||
| 1008 | $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count); |
||
| 1009 | if (1 === $count) { |
||
| 1010 | // items have been removed, update the offset |
||
| 1011 | $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n"); |
||
| 1012 | $value = $trimmedValue; |
||
| 1013 | |||
| 1014 | // remove end of the document marker (...) |
||
| 1015 | $value = preg_replace('#\.\.\.\s*$#', '', $value); |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | return $value; |
||
| 1019 | } |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Returns true if the next line starts unindented collection. |
||
| 1023 | * |
||
| 1024 | * @return bool Returns true if the next line starts unindented collection, false otherwise |
||
| 1025 | */ |
||
| 1026 | private function isNextLineUnIndentedCollection(): bool |
||
| 1027 | { |
||
| 1028 | $currentIndentation = $this->getCurrentLineIndentation(); |
||
| 1029 | $movements = 0; |
||
| 1030 | |||
| 1031 | do { |
||
| 1032 | $EOF = !$this->moveToNextLine(); |
||
| 1033 | |||
| 1034 | if (!$EOF) { |
||
| 1035 | ++$movements; |
||
| 1036 | } |
||
| 1037 | } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment())); |
||
| 1038 | |||
| 1039 | if ($EOF) { |
||
| 1040 | return false; |
||
| 1041 | } |
||
| 1042 | |||
| 1043 | $ret = $this->getCurrentLineIndentation() === $currentIndentation && $this->isStringUnIndentedCollectionItem(); |
||
| 1044 | |||
| 1045 | for ($i = 0; $i < $movements; ++$i) { |
||
| 1046 | $this->moveToPreviousLine(); |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | return $ret; |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | /** |
||
| 1053 | * Returns true if the string is un-indented collection item. |
||
| 1054 | * |
||
| 1055 | * @return bool Returns true if the string is un-indented collection item, false otherwise |
||
| 1056 | */ |
||
| 1057 | private function isStringUnIndentedCollectionItem(): bool |
||
| 1058 | { |
||
| 1059 | return 0 === strncmp($this->currentLine, '- ', 2) || '-' === rtrim($this->currentLine); |
||
| 1060 | } |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * A local wrapper for "preg_match" which will throw a ParseException if there |
||
| 1064 | * is an internal error in the PCRE engine. |
||
| 1065 | * |
||
| 1066 | * This avoids us needing to check for "false" every time PCRE is used |
||
| 1067 | * in the YAML engine |
||
| 1068 | * |
||
| 1069 | * @throws ParseException on a PCRE internal error |
||
| 1070 | * |
||
| 1071 | * @see preg_last_error() |
||
| 1072 | * |
||
| 1073 | * @internal |
||
| 1074 | */ |
||
| 1075 | public static function preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int |
||
| 1076 | { |
||
| 1077 | if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) { |
||
| 1078 | switch (preg_last_error()) { |
||
| 1079 | case PREG_INTERNAL_ERROR: |
||
| 1080 | $error = 'Internal PCRE error.'; |
||
| 1081 | break; |
||
| 1082 | case PREG_BACKTRACK_LIMIT_ERROR: |
||
| 1083 | $error = 'pcre.backtrack_limit reached.'; |
||
| 1084 | break; |
||
| 1085 | case PREG_RECURSION_LIMIT_ERROR: |
||
| 1086 | $error = 'pcre.recursion_limit reached.'; |
||
| 1087 | break; |
||
| 1088 | case PREG_BAD_UTF8_ERROR: |
||
| 1089 | $error = 'Malformed UTF-8 data.'; |
||
| 1090 | break; |
||
| 1091 | case PREG_BAD_UTF8_OFFSET_ERROR: |
||
| 1092 | $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.'; |
||
| 1093 | break; |
||
| 1094 | default: |
||
| 1095 | $error = 'Error.'; |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | throw new ParseException($error); |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | return $ret; |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Trim the tag on top of the value. |
||
| 1106 | * |
||
| 1107 | * Prevent values such as "!foo {quz: bar}" to be considered as |
||
| 1108 | * a mapping block. |
||
| 1109 | */ |
||
| 1110 | private function trimTag(string $value): string |
||
| 1111 | { |
||
| 1112 | if ('!' === $value[0]) { |
||
| 1113 | return ltrim(substr($value, 1, strcspn($value, " \r\n", 1)), ' '); |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | return $value; |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | private function getLineTag(string $value, int $flags, bool $nextLineCheck = true): ?string |
||
| 1120 | { |
||
| 1121 | if ('' === $value || '!' !== $value[0] || 1 !== self::preg_match('/^'.self::TAG_PATTERN.' *( +#.*)?$/', $value, $matches)) { |
||
| 1122 | return null; |
||
| 1123 | } |
||
| 1124 | |||
| 1125 | if ($nextLineCheck && !$this->isNextLineIndented()) { |
||
| 1126 | return null; |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | $tag = substr($matches['tag'], 1); |
||
| 1130 | |||
| 1131 | // Built-in tags |
||
| 1132 | if ($tag && '!' === $tag[0]) { |
||
| 1133 | throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), $this->getRealCurrentLineNb() + 1, $value, $this->filename); |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | if (Yaml::PARSE_CUSTOM_TAGS & $flags) { |
||
| 1137 | return $tag; |
||
| 1138 | } |
||
| 1139 | |||
| 1140 | throw new ParseException(sprintf('Tags support is not enabled. You must use the flag "Yaml::PARSE_CUSTOM_TAGS" to use "%s".', $matches['tag']), $this->getRealCurrentLineNb() + 1, $value, $this->filename); |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | private function parseQuotedString(string $yaml): ?string |
||
| 1144 | { |
||
| 1145 | if ('' === $yaml || ('"' !== $yaml[0] && "'" !== $yaml[0])) { |
||
| 1146 | throw new \InvalidArgumentException(sprintf('"%s" is not a quoted string.', $yaml)); |
||
| 1147 | } |
||
| 1148 | |||
| 1149 | $lines = [$yaml]; |
||
| 1150 | |||
| 1151 | while ($this->moveToNextLine()) { |
||
| 1152 | $lines[] = $this->currentLine; |
||
| 1153 | |||
| 1154 | if (!$this->isCurrentLineEmpty() && $yaml[0] === $this->currentLine[-1]) { |
||
| 1155 | break; |
||
| 1156 | } |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | $value = ''; |
||
| 1160 | |||
| 1161 | for ($i = 0, $linesCount = \count($lines), $previousLineWasNewline = false, $previousLineWasTerminatedWithBackslash = false; $i < $linesCount; ++$i) { |
||
| 1162 | $trimmedLine = trim($lines[$i]); |
||
| 1163 | if ('' === $trimmedLine) { |
||
| 1164 | $value .= "\n"; |
||
| 1165 | } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) { |
||
| 1166 | $value .= ' '; |
||
| 1167 | } |
||
| 1168 | |||
| 1169 | if ('' !== $trimmedLine && '\\' === $lines[$i][-1]) { |
||
| 1170 | $value .= ltrim(substr($lines[$i], 0, -1)); |
||
| 1171 | } elseif ('' !== $trimmedLine) { |
||
| 1172 | $value .= $trimmedLine; |
||
| 1173 | } |
||
| 1174 | |||
| 1175 | if ('' === $trimmedLine) { |
||
| 1176 | $previousLineWasNewline = true; |
||
| 1177 | $previousLineWasTerminatedWithBackslash = false; |
||
| 1178 | } elseif ('\\' === $lines[$i][-1]) { |
||
| 1179 | $previousLineWasNewline = false; |
||
| 1180 | $previousLineWasTerminatedWithBackslash = true; |
||
| 1181 | } else { |
||
| 1182 | $previousLineWasNewline = false; |
||
| 1183 | $previousLineWasTerminatedWithBackslash = false; |
||
| 1184 | } |
||
| 1185 | } |
||
| 1186 | |||
| 1187 | return $value; |
||
| 1188 | |||
| 1189 | for ($i = 1; isset($yaml[$i]) && $quotation !== $yaml[$i]; ++$i) { |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | // quoted single line string |
||
| 1193 | if (isset($yaml[$i]) && $quotation === $yaml[$i]) { |
||
| 1194 | return $yaml; |
||
| 1195 | } |
||
| 1196 | |||
| 1197 | $lines = [$yaml]; |
||
| 1198 | |||
| 1199 | while ($this->moveToNextLine()) { |
||
| 1200 | for ($i = 1; isset($this->currentLine[$i]) && $quotation !== $this->currentLine[$i]; ++$i) { |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | $lines[] = trim($this->currentLine); |
||
| 1204 | |||
| 1205 | if (isset($this->currentLine[$i]) && $quotation === $this->currentLine[$i]) { |
||
| 1206 | break; |
||
| 1207 | } |
||
| 1208 | } |
||
| 1209 | } |
||
| 1210 | |||
| 1211 | private function lexInlineMapping(string $yaml): string |
||
| 1212 | { |
||
| 1213 | if ('' === $yaml || '{' !== $yaml[0]) { |
||
| 1214 | throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml)); |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | for ($i = 1; isset($yaml[$i]) && '}' !== $yaml[$i]; ++$i) { |
||
| 1218 | } |
||
| 1219 | |||
| 1220 | if (isset($yaml[$i]) && '}' === $yaml[$i]) { |
||
| 1221 | return $yaml; |
||
| 1222 | } |
||
| 1223 | |||
| 1224 | $lines = [$yaml]; |
||
| 1225 | |||
| 1226 | while ($this->moveToNextLine()) { |
||
| 1227 | $lines[] = $this->currentLine; |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | return implode("\n", $lines); |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | private function lexInlineSequence(string $yaml): string |
||
| 1234 | { |
||
| 1235 | if ('' === $yaml || '[' !== $yaml[0]) { |
||
| 1236 | throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml)); |
||
| 1237 | } |
||
| 1238 | |||
| 1239 | for ($i = 1; isset($yaml[$i]) && ']' !== $yaml[$i]; ++$i) { |
||
| 1240 | } |
||
| 1241 | |||
| 1242 | if (isset($yaml[$i]) && ']' === $yaml[$i]) { |
||
| 1243 | return $yaml; |
||
| 1244 | } |
||
| 1245 | |||
| 1246 | $value = $yaml; |
||
| 1247 | |||
| 1248 | while ($this->moveToNextLine()) { |
||
| 1249 | for ($i = 1; isset($this->currentLine[$i]) && ']' !== $this->currentLine[$i]; ++$i) { |
||
| 1250 | } |
||
| 1251 | |||
| 1252 | $value .= trim($this->currentLine); |
||
| 1253 | |||
| 1254 | if (isset($this->currentLine[$i]) && ']' === $this->currentLine[$i]) { |
||
| 1255 | break; |
||
| 1256 | } |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | return $value; |
||
| 1260 | } |
||
| 1262 |