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