8000 Add Yaml::PARSE_EXCEPTION_ON_DUPLICATE to throw exceptions on duplicates by alexpott · Pull Request #19529 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Add Yaml::PARSE_EXCEPTION_ON_DUPLICATE to throw exceptions on duplicates #19529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
FIx coding standards
  • Loading branch information
alexpott committed Aug 4, 2016
commit 72b47b07630849b3efb55ff38864af8d4a49d541
9 changes: 3 additions & 6 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,7 @@ private static function parseMapping($mapping, $flag 8000 s, &$i = 0, $references = ar
// are processed sequentially.
if (!isset($output[$key])) {
$output[$key] = $value;
}
elseif (static::$exceptionOnDuplicate) {
} elseif (static::$exceptionOnDuplicate) {
throw new ParseException(sprintf('Duplicate key "%s" detected whilst parsing YAML', $key));
}
$done = true;
Expand All @@ -491,8 +490,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
// are processed sequentially.
if (!isset($output[$key])) {
$output[$key] = $value;
}
elseif (static::$exceptionOnDuplicate) {
} elseif (static::$exceptionOnDuplicate) {
throw new ParseException(sprintf('Duplicate key "%s" detected whilst parsing YAML', $key));
}
$done = true;
Expand All @@ -507,8 +505,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
// are processed sequentially.
if (!isset($output[$key])) {
$output[$key] = $value;
}
elseif (static::$exceptionOnDuplicate) {
} elseif (static::$exceptionOnDuplicate) {
throw new ParseException(sprintf('Duplicate key "%s" detected whilst parsing YAML', $key));
}
$done = true;
Expand Down
9 changes: 3 additions & 6 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ public function parse($value, $flags = 0)
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !isset($data[$key])) {
$data[$key] = null;
}
elseif ($exceptionOnDuplicate) {
} elseif ($exceptionOnDuplicate) {
throw new ParseException(sprintf('Duplicate key "%s" detected whilst parsing YAML', $key));
}
} else {
Expand All @@ -252,8 +251,7 @@ public function parse($value, $flags = 0)
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !isset($data[$key])) {
$data[$key] = $value;
}
elseif ($exceptionOnDuplicate) {
} elseif ($exceptionOnDuplicate) {
throw new ParseException(sprintf('Duplicate key "%s" detected whilst parsing YAML', $key));
}
}
Expand All @@ -263,8 +261,7 @@ public function parse($value, $flags = 0)
// But overwriting is allowed when a merge node is used in current block.
if ($allowOverwrite || !isset($data[$key])) {
$data[$key] = $value;
}
elseif ($exceptionOnDuplicate) {
} elseif ($exceptionOnDuplicate) {
throw new ParseException(sprintf('Duplicate key "%s" detected whilst parsing YAML', $key));
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,36 +806,38 @@ public function testMappingDuplicateKeyFlow()
/**
* @dataProvider getParseExceptionOnDuplicateData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing note saying that this should turned to an @expectedException assertion on 4.0, isn't it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need it, the test will fail anyway when the deprecation will be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need it, but it may help the one doing the cleanup when preparing 4.0...

*/
public function testParseExceptionOnDuplicate($input, $duplicate_key) {
$this->setExpectedException('\Symfony\Component\Yaml\Exception\ParseException', 'Duplicate key "' . $duplicate_key . '" detected whilst parsing YAML');
public function testParseExceptionOnDuplicate($input, $duplicate_key)
{
$this->setExpectedException('\Symfony\Component\Yaml\Exception\ParseException', 'Duplicate key "'.$duplicate_key.'" detected whilst parsing YAML');
Yaml::parse($input, Yaml::PARSE_EXCEPTION_ON_DUPLICATE);
}

public function getParseExceptionOnDuplicateData() {
public function getParseExceptionOnDuplicateData()
{
$tests = array();

$yaml = <<<EOD
parent: { child: first, child: duplicate }
EOD;
$tests[] = [$yaml, 'child'];
$tests[] = array($yaml, 'child');

$yaml = <<<EOD
parent:
child: first,
child: duplicate
EOD;
$tests[] = [$yaml, 'child'];
$tests[] = array($yaml, 'child');

$yaml = <<<EOD
parent: { child: foo }
parent: { child: bar }
EOD;
$tests[] = [$yaml, 'parent'];
$tests[] = array($yaml, 'parent');

$yaml = <<<EOD
parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
EOD;
$tests[] = [$yaml, 'child_mapping'];
$tests[] = array($yaml, 'child_mapping');

$yaml = <<<EOD
parent:
Expand All @@ -844,12 +846,12 @@ public function getParseExceptionOnDuplicateData() {
child_mapping:
value: bar
EOD;
$tests[] = [$yaml, 'child_mapping'];
$tests[] = array($yaml, 'child_mapping');

$yaml = <<<EOD
parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
EOD;
$tests[] = [$yaml, 'child_sequence'];
$tests[] = array($yaml, 'child_sequence');

$yaml = <<<EOD
parent:
Expand All @@ -862,7 +864,7 @@ public function getParseExceptionOnDuplicateData() {
- key2
- key3
EOD;
$tests[] = [$yaml, 'child_sequence'];
$tests[] = array($yaml, 'child_sequence');

return $tests;
}
Expand Down
0