Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Parser
private $skippedLineNumbers = [];
private $locallySkippedLineNumbers = [];
private $refsBeingParsed = [];
private $openMappingCount = 0;

/**
* Parses a YAML file into a PHP value.
Expand Down Expand Up @@ -217,7 +218,7 @@ private function doParse(string $value, int $flags)
}

if (!\is_string($key) && !\is_int($key)) {
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);
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);
}

// Convert float keys to strings, to avoid being converted to integers by PHP
Expand All @@ -232,7 +233,7 @@ private function doParse(string $value, int $flags)
$refName = substr(rtrim($values['value']), 1);
if (!\array_key_exists($refName, $this->refs)) {
if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) {
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);
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);
}

throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
Expand Down Expand Up @@ -708,7 +709,7 @@ private function parseValue(string $value, int $flags, string $context)

if (!\array_key_exists($value, $this->refs)) {
if (false !== $pos = array_search($value, $this->refsBeingParsed, true)) {
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);
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);
}

throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
Expand Down Expand Up @@ -1208,18 +1209,12 @@ private function lexInlineMapping(string $yaml): string
if ('' === $yaml || '{' !== $yaml[0]) {
throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml));
}

for ($i = 1; isset($yaml[$i]) && '}' !== $yaml[$i]; ++$i) {
}

if (isset($yaml[$i]) && '}' === $yaml[$i]) {
return $yaml;
}

$lines = [$yaml];

while ($this->moveToNextLine()) {
$lines[] = $this->currentLine;
$this->openMappingCount = 0;
$lines = [
$this->calculateLineOpenMappings($yaml),
];
while (!$this->allMappingIsClosed() && $this->moveToNextLine()) {
$lines[] = $this->calculateLineOpenMappings($this->currentLine);
}

return implode("\n", $lines);
Expand Down Expand Up @@ -1253,4 +1248,23 @@ private function lexInlineSequence(string $yaml): string

return $value;
}

private function allMappingIsClosed(): bool
{
return 0 === $this->openMappingCount;
}

private function calculateLineOpenMappings(string $yamlLine): string
{
for ($i = 0; isset($yamlLine[$i]); ++$i) {
if ('{' === $yamlLine[$i]) {
++$this->openMappingCount;
}
if ('}' === $yamlLine[$i]) {
--$this->openMappingCount;
}
}

return trim($yamlLine);
}
}
47 changes: 47 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,53 @@ public function testParseValueWithNegativeModifiers()
$this->parser->parse($yaml)
);
}

/**
* Test that covers #37788 issue.
*
* @dataProvider validMappingSequenceProvider
*/
public function testParametersAfterMappingSequence(string $yaml, array $parsed): void
{
self::assertSame($parsed, $this->parser->parse($yaml));
}

public function validMappingSequenceProvider(): iterable
{
$expected = [
'map' => [
'key' => 'value',
'a' => 'b',
],
'param' => 'some',
];

yield 'multiline syntax' => [
<<<YAML
map: {
key: "value",
a: "b"
}
param: "some"
YAML,
$expected,
];
yield 'inline syntax' => [
<<<YAML
map: {key: "value", a: "b"}
param: "some"
YAML,
$expected,
];
yield 'mixed syntax' => [
<<<YAML
map: {key: "value",
a: "b"}
param: "some"
YAML,
$expected,
];
}
}

class B
Expand Down