Skip to content

Commit a9d7d07

Browse files
committed
[Yaml] deprecate unquoted indicator characters
1 parent 5805cc5 commit a9d7d07

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
236236
throw new ParseException(sprintf('Malformed inline YAML string (%s).', $scalar));
237237
}
238238

239-
// a non-quoted string cannot start with @ or ` (reserved)
240-
if ($output && ('@' === $output[0] || '`' === $output[0])) {
239+
// a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
240+
if ($output && ('@' === $output[0] || '`' === $output[0] || '|' === $output[0] || '>' === $output[0])) {
241241
@trigger_error(sprintf('Not quoting a scalar starting with "%s" is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $output[0]), E_USER_DEPRECATED);
242242

243243
// to be thrown in 3.0

src/Symfony/Component/Yaml/Parser.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
114114

115115
$data[] = $parser->parse($block, $exceptionOnInvalidType, $objectSupport, $objectForMap);
116116
} else {
117-
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
117+
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
118118
}
119119
}
120120
if ($isRef) {
@@ -230,7 +230,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
230230
}
231231
}
232232
} else {
233-
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap);
233+
$value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
234234
// Spec: Keys MUST be unique; first one wins.
235235
// But overwriting is allowed when a merge node is used in current block.
236236
if ($allowOverwrite || !isset($data[$key])) {
@@ -445,12 +445,13 @@ private function moveToPreviousLine()
445445
* @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
446446
* @param bool $objectSupport True if object support is enabled, false otherwise
447447
* @param bool $objectForMap true if maps should return a stdClass instead of array()
448+
* @param string $context The parser context (either sequence or mapping)
448449
*
449450
* @return mixed A PHP value
450451
*
451452
* @throws ParseException When reference does not exist
452453
*/
453-
private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap)
454+
private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context)
454455
{
455456
if (0 === strpos($value, '*')) {
456457
if (false !== $pos = strpos($value, '#')) {
@@ -472,6 +473,13 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
472473
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
473474
}
474475

476+
if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($value, ': ')) {
477+
@trigger_error(sprintf('Using a colon in an unquoted mapping value in line %d is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
478+
479+
// to be thrown in 3.0
480+
// throw new ParseException('A colon cannot be used in an unquoted mapping value.');
481+
}
482+
475483
try {
476484
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
477485
} catch (ParseException $e) {

src/Symfony/Component/Yaml/Tests/Fixtures/sfQuotes.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ test: Some characters at the beginning of a string must be escaped
33
brief: >
44
Some characters at the beginning of a string must be escaped
55
yaml: |
6-
foo: | bar
6+
foo: '| bar'
77
php: |
88
array('foo' => '| bar')
99
---

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ public function getReservedIndicators()
205205
return array(array('@'), array('`'));
206206
}
207207

208+
/**
209+
* @group legacy
210+
* @dataProvider getScalarIndicators
211+
* throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
212+
*/
213+
public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
214+
{
215+
Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
216+
}
217+
218+
public function getScalarIndicators()
219+
{
220+
return array(array('|'), array('>'));
221+
}
222+
208223
public function getTestsForParse()
209224
{
210225
return array(

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class ParserTest extends \PHPUnit_Framework_TestCase
1818
{
1919
protected $parser;
20+
private $deprecations = array();
2021

2122
protected function setUp()
2223
{
@@ -783,6 +784,34 @@ public function testFloatKeys()
783784

784785
$this->assertEquals($expected, $this->parser->parse($yaml));
785786
}
787+
788+
/**
789+
* @group legacy
790+
* throw ParseException in Symfony 3.0
791+
*/
792+
public function testColonInMappingValueException()
793+
{
794+
$yaml = <<<EOF
795+
foo: bar: baz
796+
EOF;
797+
798+
$this->deprecations = array();
799+
set_error_handler(array($this, 'handleError'));
800+
801+
$this->parser->parse($yaml);
802+
803+
$this->assertCount(1, $this->deprecations);
804+
$this->assertContains('Using a colon in an unquoted mapping value in line 1 is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $this->deprecations[0]);
805+
806+
restore_error_handler();
807+
}
808+
809+
public function handleError($type, $msg)
810+
{
811+
if (E_USER_DEPRECATED === $type) {
812+
$this->deprecations[] = $msg;
813+
}
814+
}
786815
}
787816

788817
class B

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy