Skip to content

Commit 8ee21c7

Browse files
committed
minor #19758 [Yaml] improve deprecation message and changelog (xabbuh)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Yaml] improve deprecation message and changelog | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Commits ------- a7128f8 [Yaml] improve deprecation message and changelog
2 parents 30ef6ff + a7128f8 commit 8ee21c7

File tree

6 files changed

+27
-21
lines changed

6 files changed

+27
-21
lines changed

UPGRADE-3.2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ Validator
7474
Yaml
7575
----
7676

77-
* Support for silently ignoring duplicate keys in YAML has been deprecated and
78-
will lead to a `ParseException` in Symfony 4.0.
77+
* Support for silently ignoring duplicate mapping keys in YAML has been
78+
deprecated and will lead to a `ParseException` in Symfony 4.0.
7979

8080
* Mappings with a colon (`:`) that is not followed by a whitespace are deprecated
8181
and will lead to a `ParseException` in Symfony 4.0 (e.g. `foo:bar` must be

UPGRADE-4.0.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ Yaml
234234
* The `!!php/object` tag to indicate dumped PHP objects was removed in favor of
235235
the `!php/object` tag.
236236

237-
* Duplicate keys in YAML leads to a `ParseException`.
238-
237+
* Duplicate mapping keys lead to a `ParseException`.
239238

240239
Validator
241240
---------

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ CHANGELOG
1414
Yaml::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_CONSTANT);
1515
```
1616

17-
* Support for silently ignoring duplicate keys in YAML has been deprecated and
18-
will lead to a `ParseException` in Symfony 4.0.
17+
* Support for silently ignoring duplicate mapping keys in YAML has been
18+
deprecated and will lead to a `ParseException` in Symfony 4.0.
1919

2020
3.1.0
2121
-----

src/Symfony/Component/Yaml/Inline.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Inline
2525
{
2626
const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
2727

28+
public static $parsedLineNumber;
29+
2830
private static $exceptionOnInvalidType = false;
2931
private static $objectSupport = false;
3032
private static $objectForMap = false;
@@ -476,7 +478,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
476478
if (!isset($output[$key])) {
477479
$output[$key] = $value;
478480
} else {
479-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
481+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
480482
}
481483
$done = true;
482484
break;
@@ -489,7 +491,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
489491
if (!isset($output[$key])) {
490492
$output[$key] = $value;
491493
} else {
492-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
494+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
493495
}
494496
$done = true;
495497
break;
@@ -504,7 +506,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
504506
if (!isset($output[$key])) {
505507
$output[$key] = $value;
506508
} else {
507-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
509+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
508510
}
509511
$done = true;
510512
--$i;

src/Symfony/Component/Yaml/Parser.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public function parse($value, $flags = 0)
156156
// force correct settings
157157
Inline::parse(null, $flags, $this->refs);
158158
try {
159+
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
159160
$key = Inline::parseScalar($values['key']);
160161
} catch (ParseException $e) {
161162
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
@@ -242,16 +243,18 @@ public function parse($value, $flags = 0)
242243
if ($allowOverwrite || !isset($data[$key])) {
243244
$data[$key] = null;
244245
} else {
245-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
246+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
246247
}
247248
} else {
249+
// remember the parsed line number here in case we need it to provide some contexts in error messages below
250+
$realCurrentLineNbKey = $this->getRealCurrentLineNb();
248251
$value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags);
249252
// Spec: Keys MUST be unique; first one wins.
250253
// But overwriting is allowed when a merge node is used in current block.
251254
if ($allowOverwrite || !isset($data[$key])) {
252255
$data[$key] = $value;
253256
} else {
254-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
257+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, $realCurrentLineNbKey + 1), E_USER_DEPRECATED);
255258
}
256259
}
257260
} else {
@@ -261,7 +264,7 @@ public function parse($value, $flags = 0)
261264
if ($allowOverwrite || !isset($data[$key])) {
262265
$data[$key] = $value;
263266
} else {
264-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
267+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
265268
}
266269
}
267270
if ($isRef) {
@@ -276,6 +279,7 @@ public function parse($value, $flags = 0)
276279
// 1-liner optionally followed by newline(s)
277280
if (is_string($value) && $this->lines[0] === trim($value)) {
278281
try {
282+
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
279283
$value = Inline::parse($this->lines[0], $flags, $this->refs);
280284
} catch (ParseException $e) {
281285
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
@@ -578,6 +582,7 @@ private function parseValue($value, $flags, $context)
578582
}
579583

580584
try {
585+
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
581586
$parsedValue = Inline::parse($value, $flags, $this->refs);
582587

583588
if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,9 @@ public function testMappingDuplicateKeyFlow()
813813
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
814814
* throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
815815
*/
816-
public function testParseExceptionOnDuplicate($input, $duplicate_key)
816+
public function testParseExceptionOnDuplicate($input, $duplicateKey, $lineNumber)
817817
{
818-
ErrorAssert::assertDeprecationsAreTriggered(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicates in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $duplicate_key), function () use ($input) {
818+
ErrorAssert::assertDeprecationsAreTriggered(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $duplicateKey, $lineNumber), function () use ($input) {
819819
Yaml::parse($input);
820820
});
821821
}
@@ -827,25 +827,25 @@ public function getParseExceptionOnDuplicateData()
827827
$yaml = <<<EOD
828828
parent: { child: first, child: duplicate }
829829
EOD;
830-
$tests[] = array($yaml, 'child');
830+
$tests[] = array($yaml, 'child', 1);
831831

832832
$yaml = <<<EOD
833833
parent:
834834
child: first,
835835
child: duplicate
836836
EOD;
837-
$tests[] = array($yaml, 'child');
837+
$tests[] = array($yaml, 'child', 3);
838838

839839
$yaml = <<<EOD
840840
parent: { child: foo }
841841
parent: { child: bar }
842842
EOD;
843-
$tests[] = array($yaml, 'parent');
843+
$tests[] = array($yaml, 'parent', 2);
844844

845845
$yaml = <<<EOD
846846
parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
847847
EOD;
848-
$tests[] = array($yaml, 'child_mapping');
848+
$tests[] = array($yaml, 'child_mapping', 1);
849849

850850
$yaml = <<<EOD
851851
parent:
@@ -854,12 +854,12 @@ public function getParseExceptionOnDuplicateData()
854854
child_mapping:
855855
value: bar
856856
EOD;
857-
$tests[] = array($yaml, 'child_mapping');
857+
$tests[] = array($yaml, 'child_mapping', 4);
858858

859859
$yaml = <<<EOD
860860
parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
861861
EOD;
862-
$tests[] = array($yaml, 'child_sequence');
862+
$tests[] = array($yaml, 'child_sequence', 1);
863863

864864
$yaml = <<<EOD
865865
parent:
@@ -872,7 +872,7 @@ public function getParseExceptionOnDuplicateData()
872872
- key2
873873
- key3
874874
EOD;
875-
$tests[] = array($yaml, 'child_sequence');
875+
$tests[] = array($yaml, 'child_sequence', 6);
876876

877877
return $tests;
878878
}

0 commit comments

Comments
 (0)