Skip to content

Commit c847146

Browse files
committed
feature #22059 [Yaml] deprecate "? " starting unquoted strings (xabbuh)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Yaml] deprecate "? " starting unquoted strings | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #20579 | License | MIT | Doc PR | Commits ------- 731a74e79c [Yaml] deprecate "? " starting unquoted strings
2 parents eca1dd6 + 9399377 commit c847146

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ CHANGELOG
44
3.3.0
55
-----
66

7-
* Deprecated support for implicitly parsing non-string mapping keys as strings. Mapping keys that are no strings will
8-
lead to a `ParseException` in Symfony 4.0. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as
7+
* Starting an unquoted string with a question mark followed by a space is
8+
deprecated and will throw a `ParseException` in Symfony 4.0.
9+
10+
* Deprecated support for implicitly parsing non-string mapping keys as strings.
11+
Mapping keys that are no strings will lead to a `ParseException` in Symfony
12+
4.0. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as
913
strings.
1014

1115
Before:

Parser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public function parse($value, $flags = 0)
144144
$values['value'] = $matches['value'];
145145
}
146146

147+
if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) {
148+
@trigger_error('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', E_USER_DEPRECATED);
149+
}
150+
147151
// array
148152
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
149153
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags);
@@ -304,6 +308,10 @@ public function parse($value, $flags = 0)
304308
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine);
305309
}
306310

311+
if (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1]) {
312+
@trigger_error('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', E_USER_DEPRECATED);
313+
}
314+
307315
// 1-liner optionally followed by newline(s)
308316
if (is_string($value) && $this->lines[0] === trim($value)) {
309317
try {

Tests/ParserTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,52 @@ public function testExceptionWhenUsingUnsuportedBuiltInTags()
16411641
$this->parser->parse('!!foo');
16421642
}
16431643

1644+
/**
1645+
* @group legacy
1646+
* @expectedDeprecation Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.
1647+
*/
1648+
public function testComplexMappingThrowsParseException()
1649+
{
1650+
$yaml = <<<YAML
1651+
? "1"
1652+
:
1653+
name: végétalien
1654+
YAML;
1655+
1656+
$this->parser->parse($yaml);
1657+
}
1658+
1659+
/**
1660+
* @group legacy
1661+
* @expectedDeprecation Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.
1662+
*/
1663+
public function testComplexMappingNestedInMappingThrowsParseException()
1664+
{
1665+
$yaml = <<<YAML
1666+
diet:
1667+
? "1"
1668+
:
1669+
name: végétalien
1670+
YAML;
1671+
1672+
$this->parser->parse($yaml);
1673+
}
1674+
1675+
/**
1676+
* @group legacy
1677+
* @expectedDeprecation Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.
1678+
*/
1679+
public function testComplexMappingNestedInSequenceThrowsParseException()
1680+
{
1681+
$yaml = <<<YAML
1682+
- ? "1"
1683+
:
1684+
name: végétalien
1685+
YAML;
1686+
1687+
$this->parser->parse($yaml);
1688+
}
1689+
16441690
private function loadTestsFromFixtureFiles($testsFile)
16451691
{
16461692
$parser = new Parser();

0 commit comments

Comments
 (0)