Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8629ea8

Browse filesBrowse files
committed
[Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
1 parent daac6db commit 8629ea8
Copy full SHA for 8629ea8

File tree

18 files changed

+54
-176
lines changed
Filter options

18 files changed

+54
-176
lines changed

‎UPGRADE-3.3.md

Copy file name to clipboardExpand all lines: UPGRADE-3.3.md
+5-8Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,14 @@ Yaml
365365

366366
* Deprecated support for implicitly parsing non-string mapping keys as strings.
367367
Mapping keys that are no strings will lead to a `ParseException` in Symfony
368-
4.0. Use the `PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as
369-
strings.
368+
4.0. Use quotes to opt-in for keys to be parsed as strings.
370369

371370
Before:
372371

373372
```php
374373
$yaml = <<<YAML
375374
null: null key
376375
true: boolean true
377-
1: integer key
378376
2.0: float key
379377
YAML;
380378

@@ -386,13 +384,12 @@ Yaml
386384
```php
387385

388386
$yaml = <<<YAML
389-
null: null key
390-
true: boolean true
391-
1: integer key
392-
2.0: float key
387+
"null": null key
388+
"true": boolean true
389+
"2.0": float key
393390
YAML;
394391

395-
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
392+
Yaml::parse($yaml);
396393
```
397394

398395
* Omitting the key of a mapping is deprecated and will throw a `ParseException` in Symfony 4.0.

‎UPGRADE-4.0.md

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+8-10Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ FrameworkBundle
326326
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ValidateWorkflowsPass` class
327327
has been removed. Use the `Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass`
328328
class instead.
329-
330-
* The `Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory` class has been removed.
329+
330+
* The `Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory` class has been removed.
331331
Use `Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
332332

333333
HttpFoundation
@@ -555,16 +555,15 @@ Yaml
555555
throws a `ParseException`.
556556

557557
* Removed support for implicitly parsing non-string mapping keys as strings.
558-
Mapping keys that are no strings will result in a `ParseException`. Use the
559-
`PARSE_KEYS_AS_STRINGS` flag to opt-in for keys to be parsed as strings.
558+
Mapping keys that are no strings will result in a `ParseException`. Use
559+
quotes to opt-in for keys to be parsed as strings.
560560

561561
Before:
562562

563563
```php
564564
$yaml = <<<YAML
565565
null: null key
566566
true: boolean true
567-
1: integer key
568567
2.0: float key
569568
YAML;
570569

@@ -576,13 +575,12 @@ Yaml
576575
```php
577576

578577
$yaml = <<<YAML
579-
null: null key
580-
true: boolean true
581-
1: integer key
582-
2.0: float key
578+
"null": null key
579+
"true": boolean true
580+
"2.0": float key
583581
YAML;
584582

585-
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
583+
Yaml::parse($yaml);
586584
```
587585

588586
* Omitting the key of a mapping is not supported anymore and throws a `ParseException`.

‎src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ protected function loadFile($file)
607607
}
608608

609609
try {
610-
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_KEYS_AS_STRINGS);
610+
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
611611
} catch (ParseException $e) {
612612
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
613613
}

‎src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/YamlFileLoader.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function load($file, $type = null)
5959
}
6060

6161
try {
62-
$parsedConfig = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS);
62+
$parsedConfig = $this->yamlParser->parse(file_get_contents($path));
6363
} catch (ParseException $e) {
6464
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
6565
}

‎src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function getClassesFromYaml()
114114
$this->yamlParser = new Parser();
115115
}
116116

117-
$classes = $this->yamlParser->parse(file_get_contents($this->file), Yaml::PARSE_KEYS_AS_STRINGS);
117+
$classes = $this->yamlParser->parse(file_get_contents($this->file));
118118

119119
if (empty($classes)) {
120120
return array();

‎src/Symfony/Component/Translation/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Loader/YamlFileLoader.php
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Translation\Exception\LogicException;
1616
use Symfony\Component\Yaml\Parser as YamlParser;
1717
use Symfony\Component\Yaml\Exception\ParseException;
18-
use Symfony\Component\Yaml\Yaml;
1918

2019
/**
2120
* YamlFileLoader loads translations from Yaml files.
@@ -40,7 +39,7 @@ protected function loadResource($resource)
4039
}
4140

4241
try {
43-
$messages = $this->yamlParser->parse(file_get_contents($resource), Yaml::PARSE_KEYS_AS_STRINGS);
42+
$messages = $this->yamlParser->parse(file_get_contents($resource));
4443
} catch (ParseException $e) {
4544
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
4645
}

‎src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Validator\Mapping\ClassMetadata;
1515
use Symfony\Component\Yaml\Exception\ParseException;
1616
use Symfony\Component\Yaml\Parser as YamlParser;
17-
use Symfony\Component\Yaml\Yaml;
1817

1918
/**
2019
* Loads validation metadata from a YAML file.
@@ -116,7 +115,7 @@ protected function parseNodes(array $nodes)
116115
private function parseFile($path)
117116
{
118117
try {
119-
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS);
118+
$classes = $this->yamlParser->parse(file_get_contents($path));
120119
} catch (ParseException $e) {
121120
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
122121
}

‎src/Symfony/Component/Yaml/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/CHANGELOG.md
+5-8Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ CHANGELOG
99

1010
* Deprecated support for implicitly parsing non-string mapping keys as strings.
1111
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
13-
strings.
12+
4.0. Use quotes to opt-in for keys to be parsed as strings.
1413

1514
Before:
1615

1716
```php
1817
$yaml = <<<YAML
1918
null: null key
2019
true: boolean true
21-
1: integer key
2220
2.0: float key
2321
YAML;
2422

@@ -30,13 +28,12 @@ CHANGELOG
3028
```php
3129

3230
$yaml = <<<YAML
33-
null: null key
34-
true: boolean true
35-
1: integer key
36-
2.0: float key
31+
"null": null key
32+
"true": boolean true
33+
"2.0": float key
3734
YAML;
3835

39-
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
36+
Yaml::parse($yaml);
4037
```
4138

4239
* Omitted mapping values will be parsed as `null`.

‎src/Symfony/Component/Yaml/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,10 @@ 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);
495-
496-
if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey)) {
497-
@trigger_error('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. Pass the PARSE_KEYS_AS_STRING flag to explicitly enable the type casts.', E_USER_DEPRECATED);
495+
if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey) && !is_int($evaluatedKey)) {
496+
@trigger_error('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. Use quotes instead.', E_USER_DEPRECATED);
498497
}
499498
}
500499

‎src/Symfony/Component/Yaml/Parser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+3-11Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private function doParse($value, $flags)
208208
$this->refs[$isRef] = end($data);
209209
}
210210
} elseif (
211-
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?(?:![^\s]++\s++)?[^ \'"\[\{!].*?) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
211+
self::preg_match('#^(?P<key>(![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
212212
&& (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
213213
) {
214214
if ($context && 'sequence' == $context) {
@@ -220,23 +220,15 @@ private function doParse($value, $flags)
220220
Inline::parse(null, $flags, $this->refs);
221221
try {
222222
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
223-
$i = 0;
224-
$evaluateKey = !(Yaml::PARSE_KEYS_AS_STRINGS & $flags);
225-
226-
// constants in key will be evaluated anyway
227-
if (isset($values['key'][0]) && '!' === $values['key'][0] && Yaml::PARSE_CONSTANT & $flags) {
228-
$evaluateKey = true;
229-
}
230-
231-
$key = Inline::parseScalar($values['key'], 0, null, $i, $evaluateKey);
223+
$key = Inline::parseScalar($values['key']);
232224
} catch (ParseException $e) {
233225
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
234226
$e->setSnippet($this->currentLine);
235227

236228
throw $e;
237229
}
238230

239-
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags) && !is_string($key)) {
231+
if (!is_string($key) && !is_int($key)) {
240232
@trigger_error('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. Pass the PARSE_KEYS_AS_STRING flag to explicitly enable the type casts.', E_USER_DEPRECATED);
241233
}
242234

‎src/Symfony/Component/Yaml/Tests/DumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+1-1Lines 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
}

‎src/Symfony/Component/Yaml/Tests/Fixtures/booleanMappingKeys.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/Fixtures/booleanMappingKeys.yml
-11Lines changed: 0 additions & 11 deletions
This file was deleted.

‎src/Symfony/Component/Yaml/Tests/Fixtures/nonStringKeys.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/Fixtures/nonStringKeys.yml
-3Lines changed: 0 additions & 3 deletions
This file was deleted.

‎src/Symfony/Component/Yaml/Tests/Fixtures/nullMappingKey.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/Fixtures/nullMappingKey.yml
-9Lines changed: 0 additions & 9 deletions
This file was deleted.

‎src/Symfony/Component/Yaml/Tests/Fixtures/numericMappingKeys.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/Fixtures/numericMappingKeys.yml
-23Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/InlineTest.php
+7-15Lines changed: 7 additions & 15 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

@@ -730,22 +730,14 @@ public function testTheEmptyStringIsAValidMappingKey()
730730

731731
/**
732732
* @group legacy
733-
* @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. Pass the PARSE_KEYS_AS_STRING flag to explicitly enable the type casts.
733+
* @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. Use quotes instead.
734734
* @dataProvider getNotPhpCompatibleMappingKeyData
735735
*/
736736
public function testImplicitStringCastingOfMappingKeysIsDeprecated($yaml, $expected)
737737
{
738738
$this->assertSame($expected, Inline::parse($yaml));
739739
}
740740

741-
/**
742-
* @dataProvider getNotPhpCompatibleMappingKeyData
743-
*/
744-
public function testExplicitStringCastingOfMappingKeys($yaml, $expected)
745-
{
746-
$this->assertSame($expected, Inline::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS));
747-
}
748-
749741
public function getNotPhpCompatibleMappingKeyData()
750742
{
751743
return array(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.