Skip to content

Commit 240ab5d

Browse files
GuilhemNxabbuh
authored andcommitted
[Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
1 parent 8ccc032 commit 240ab5d

File tree

6 files changed

+44
-39
lines changed

6 files changed

+44
-39
lines changed

Inline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
490490
@trigger_error('Omitting the key of a mapping is deprecated and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
491491
}
492492

493-
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags)) {
493+
if (!$isKeyQuoted) {
494494
$evaluatedKey = self::evaluateScalar($key, $flags, $references);
495495

496496
if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey) && !is_int($evaluatedKey)) {

Parser.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public function parse($value, $flags = 0)
8686
}
8787
}
8888

89+
if (Yaml::PARSE_KEYS_AS_STRINGS & $flags) {
90+
@trigger_error('Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since version 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable instead.', E_USER_DEPRECATED);
91+
}
92+
8993
if (false === preg_match('//u', $value)) {
9094
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
9195
}
@@ -236,7 +240,7 @@ private function doParse($value, $flags)
236240
throw $e;
237241
}
238242

239-
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags) && !is_string($key) && !is_int($key)) {
243+
if (!is_string($key) && !is_int($key)) {
240244
$keyType = is_numeric($key) ? 'numeric key' : 'non-string key';
241245
@trigger_error(sprintf('Implicit casting of %s to string is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.', $keyType), E_USER_DEPRECATED);
242246
}

Tests/DumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testSpecifications()
125125
// TODO
126126
} else {
127127
eval('$expected = '.trim($test['php']).';');
128-
$this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10), Yaml::PARSE_KEYS_AS_STRINGS), $test['test']);
128+
$this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
129129
}
130130
}
131131
}

Tests/InlineTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ public function getTestsForParse()
385385
array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
386386

387387
// mappings
388-
array('{foo: bar,bar: foo,false: false,null: null,integer: 12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_KEYS_AS_STRINGS),
389-
array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_KEYS_AS_STRINGS),
388+
array('{foo: bar,bar: foo,"false": false, "null": null,integer: 12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
389+
array('{ foo : bar, bar : foo, "false" : false, "null" : null, integer : 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
390390
array('{foo: \'bar\', bar: \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
391391
array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
392392
array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
@@ -456,8 +456,8 @@ public function getTestsForParseWithMapObjects()
456456
array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
457457

458458
// mappings
459-
array('{foo: bar,bar: foo,false: false,null: null,integer: 12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_KEYS_AS_STRINGS),
460-
array('{ foo : bar, bar : foo, false : false, null : null, integer : 12 }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_KEYS_AS_STRINGS),
459+
array('{foo: bar,bar: foo,"false": false,"null": null,integer: 12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_OBJECT_FOR_MAP),
460+
array('{ foo : bar, bar : foo, "false" : false, "null" : null, integer : 12 }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_OBJECT_FOR_MAP),
461461
array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
462462
array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
463463
array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
@@ -538,7 +538,7 @@ public function getTestsForDump()
538538
array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
539539

540540
// mappings
541-
array('{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), Yaml::PARSE_KEYS_AS_STRINGS),
541+
array('{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
542542
array('{ foo: bar, bar: \'foo: bar\' }', array('foo' => 'bar', 'bar' => 'foo: bar')),
543543

544544
// nested sequences and mappings
@@ -554,7 +554,7 @@ public function getTestsForDump()
554554

555555
array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
556556

557-
array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3))), Yaml::PARSE_KEYS_AS_STRINGS),
557+
array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
558558
);
559559
}
560560

@@ -739,11 +739,14 @@ public function testImplicitStringCastingOfMappingKeysIsDeprecated($yaml, $expec
739739
}
740740

741741
/**
742+
* @group legacy
743+
* @expectedDeprecation Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since version 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable instead.
744+
* @expectedDeprecation Implicit casting of incompatible mapping keys to strings is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.
742745
* @dataProvider getNotPhpCompatibleMappingKeyData
743746
*/
744747
public function testExplicitStringCastingOfMappingKeys($yaml, $expected)
745748
{
746-
$this->assertSame($expected, Inline::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS));
749+
$this->assertSame($expected, Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS));
747750
}
748751

749752
public function getNotPhpCompatibleMappingKeyData()

Tests/ParserTest.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function getDataFormSpecifications()
7171
}
7272

7373
/**
74+
* @group legacy
75+
* @expectedDeprecationMessage Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since version 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable
7476
* @dataProvider getNonStringMappingKeysData
7577
*/
7678
public function testNonStringMappingKeys($expected, $yaml, $comment)
@@ -510,14 +512,10 @@ public function testObjectSupportDisabledButNoExceptions($input)
510512
/**
511513
* @dataProvider getObjectForMapTests
512514
*/
513-
public function testObjectForMap($yaml, $expected, $explicitlyParseKeysAsStrings = false)
515+
public function testObjectForMap($yaml, $expected)
514516
{
515517
$flags = Yaml::PARSE_OBJECT_FOR_MAP;
516518

517-
if ($explicitlyParseKeysAsStrings) {
518-
$flags |= Yaml::PARSE_KEYS_AS_STRINGS;
519-
}
520-
521519
$this->assertEquals($expected, $this->parser->parse($yaml, $flags));
522520
}
523521

@@ -577,18 +575,18 @@ public function getObjectForMapTests()
577575
$expected->map = new \stdClass();
578576
$expected->map->{1} = 'one';
579577
$expected->map->{2} = 'two';
580-
$tests['numeric-keys'] = array($yaml, $expected, true);
578+
$tests['numeric-keys'] = array($yaml, $expected);
581579

582580
$yaml = <<<'YAML'
583581
map:
584-
0: one
585-
1: two
582+
'0': one
583+
'1': two
586584
YAML;
587585
$expected = new \stdClass();
588586
$expected->map = new \stdClass();
589587
$expected->map->{0} = 'one';
590588
$expected->map->{1} = 'two';
591-
$tests['zero-indexed-numeric-keys'] = array($yaml, $expected, true);
589+
$tests['zero-indexed-numeric-keys'] = array($yaml, $expected);
592590

593591
return $tests;
594592
}
@@ -1120,37 +1118,29 @@ public function testBooleanKeys()
11201118
$this->assertEquals($expected, $this->parser->parse($yaml));
11211119
}
11221120

1123-
public function testExplicitStringCastingOfFloatKeys()
1121+
public function testExplicitStringCasting()
11241122
{
11251123
$yaml = <<<'EOF'
1126-
foo:
1127-
1.2: "bar"
1128-
1.3: "baz"
1129-
EOF;
1124+
'1.2': "bar"
1125+
!!str 1.3: "baz"
11301126
1131-
$expected = array(
1132-
'foo' => array(
1133-
'1.2' => 'bar',
1134-
'1.3' => 'baz',
1135-
),
1136-
);
1137-
1138-
$this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS));
1139-
}
1127+
'true': foo
1128+
!!str false: bar
11401129
1141-
public function testExplicitStringCastingOfBooleanKeys()
1142-
{
1143-
$yaml = <<<'EOF'
1144-
true: foo
1145-
false: bar
1130+
!!str null: 'null'
1131+
'~': 'null'
11461132
EOF;
11471133

11481134
$expected = array(
1135+
'1.2' => 'bar',
1136+
'1.3' => 'baz',
11491137
'true' => 'foo',
11501138
'false' => 'bar',
1139+
'null' => 'null',
1140+
'~' => 'null',
11511141
);
11521142

1153-
$this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS));
1143+
$this->assertEquals($expected, $this->parser->parse($yaml));
11541144
}
11551145

11561146
/**
@@ -1843,6 +1833,10 @@ public function testPhpConstantTagMappingKey()
18431833
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
18441834
}
18451835

1836+
/**
1837+
* @group legacy
1838+
* @expectedDeprecation Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since version 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable instead.
1839+
*/
18461840
public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
18471841
{
18481842
$yaml = <<<YAML

Yaml.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class Yaml
3131
const PARSE_CONSTANT = 256;
3232
const PARSE_CUSTOM_TAGS = 512;
3333
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
34+
35+
/**
36+
* @deprecated since version 3.4, to be removed in 4.0. Quote your evaluable keys instead.
37+
*/
3438
const PARSE_KEYS_AS_STRINGS = 2048;
3539

3640
/**

0 commit comments

Comments
 (0)