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 b63c55c

Browse filesBrowse files
GuilhemNxabbuh
authored andcommitted
[Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
1 parent c0d99d1 commit b63c55c
Copy full SHA for b63c55c

File tree

13 files changed

+114
-59
lines changed
Filter options

13 files changed

+114
-59
lines changed

‎UPGRADE-3.4.md

Copy file name to clipboardExpand all lines: UPGRADE-3.4.md
+34-7Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ FrameworkBundle
3030
require symfony/stopwatch` in your `dev` environment.
3131

3232
* Using the `KERNEL_DIR` environment variable or the automatic guessing based
33-
on the `phpunit.xml` / `phpunit.xml.dist` file location is deprecated since 3.4.
33+
on the `phpunit.xml` / `phpunit.xml.dist` file location is deprecated since 3.4.
3434
Set the `KERNEL_CLASS` environment variable to the fully-qualified class name
35-
of your Kernel instead. Not setting the `KERNEL_CLASS` environment variable
36-
will throw an exception on 4.0 unless you override the `KernelTestCase::createKernel()`
35+
of your Kernel instead. Not setting the `KERNEL_CLASS` environment variable
36+
will throw an exception on 4.0 unless you override the `KernelTestCase::createKernel()`
3737
or `KernelTestCase::getKernelClass()` method.
38-
39-
* The `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()`
38+
39+
* The `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()`
4040
methods are deprecated since 3.4 and will be removed in 4.0.
4141

4242
* The `--no-prefix` option of the `translation:update` command is deprecated and
@@ -83,7 +83,7 @@ TwigBridge
8383
* deprecated the `Symfony\Bridge\Twig\Form\TwigRenderer` class, use the `FormRenderer`
8484
class from the Form component instead
8585

86-
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
86+
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
8787
to pass a command name as first argument
8888

8989
* deprecated `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability
@@ -95,7 +95,7 @@ TwigBundle
9595
* deprecated the `Symfony\Bundle\TwigBundle\Command\DebugCommand` class, use the `DebugCommand`
9696
class from the Twig bridge instead
9797

98-
* deprecated relying on the `ContainerAwareInterface` implementation for
98+
* deprecated relying on the `ContainerAwareInterface` implementation for
9999
`Symfony\Bundle\TwigBundle\Command\LintCommand`
100100

101101
Validator
@@ -111,3 +111,30 @@ Yaml
111111

112112
* Using the non-specific tag `!` is deprecated and will have a different
113113
behavior in 4.0. Use a plain integer or `!!float` instead.
114+
115+
* Using the `Yaml::PARSE_KEYS_AS_STRINGS` flag is deprecated as it will be
116+
removed in 4.0.
117+
118+
Before:
119+
120+
```php
121+
$yaml = <<<YAML
122+
null: null key
123+
true: boolean true
124+
2.0: float key
125+
YAML;
126+
127+
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
128+
```
129+
130+
After:
131+
132+
```php
133+
$yaml = <<<YAML
134+
"null": null key
135+
"true": boolean true
136+
"2.0": float key
137+
YAML;
138+
139+
Yaml::parse($yaml);
140+
```

‎UPGRADE-4.0.md

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+31-5Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ FrameworkBundle
338338
class instead.
339339

340340
* Using the `KERNEL_DIR` environment variable and the automatic guessing based
341-
on the `phpunit.xml` file location have been removed from the `KernelTestCase::getKernelClass()`
341+
on the `phpunit.xml` file location have been removed from the `KernelTestCase::getKernelClass()`
342342
method implementation. Set the `KERNEL_CLASS` environment variable to the
343-
fully-qualified class name of your Kernel or override the `KernelTestCase::createKernel()`
343+
fully-qualified class name of your Kernel or override the `KernelTestCase::createKernel()`
344344
or `KernelTestCase::getKernelClass()` method instead.
345345

346346
* The `Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory` class has been removed.
@@ -349,10 +349,10 @@ FrameworkBundle
349349
* The `--no-prefix` option of the `translation:update` command has
350350
been removed.
351351

352-
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass` class has been removed.
352+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass` class has been removed.
353353
Use the `Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass` class instead.
354354

355-
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass` class has been removed.
355+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass` class has been removed.
356356
Use the `Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass` class instead.
357357

358358
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass`
@@ -554,7 +554,7 @@ TwigBridge
554554
* The `TwigRendererEngine::setEnvironment()` method has been removed.
555555
Pass the Twig Environment as second argument of the constructor instead.
556556

557-
* Removed `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
557+
* Removed `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
558558
to pass a command name as first argument.
559559

560560
* Removed `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability
@@ -670,6 +670,32 @@ Yaml
670670
Yaml::parse($yaml);
671671
```
672672

673+
* Removed the `Yaml::PARSE_KEYS_AS_STRINGS` flag.
674+
675+
Before:
676+
677+
```php
678+
$yaml = <<<YAML
679+
null: null key
680+
true: boolean true
681+
2.0: float key
682+
YAML;
683+
684+
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
685+
```
686+
687+
After:
688+
689+
```php
690+
$yaml = <<<YAML
691+
"null": null key
692+
"true": boolean true
693+
"2.0": float key
694+
YAML;
695+
696+
Yaml::parse($yaml);
697+
```
698+
673699
* Omitting the key of a mapping is not supported anymore and throws a `ParseException`.
674700

675701
* Mappings with a colon (`:`) that is not followed by a whitespace are not

‎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
@@ -609,7 +609,7 @@ protected function loadFile($file)
609609
}
610610

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/YamlFileLoader.php
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Yaml\Exception\ParseException;
1818
use Symfony\Component\Yaml\Parser as YamlParser;
1919
use Symfony\Component\Config\Loader\FileLoader;
20-
use Symfony\Component\Yaml\Yaml;
2120

2221
/**
2322
* YamlFileLoader loads Yaml routing files.
@@ -59,7 +58,7 @@ public function load($file, $type = null)
5958
}
6059

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Mapping/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\Serializer\Mapping\AttributeMetadata;
1616
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
1717
use Symfony\Component\Yaml\Parser;
18-
use Symfony\Component\Yaml\Yaml;
1918

2019
/**
2120
* YAML File Loader.
@@ -114,7 +113,7 @@ private function getClassesFromYaml()
114113
$this->yamlParser = new Parser();
115114
}
116115

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

119118
if (empty($classes)) {
120119
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-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function parseNodes(array $nodes)
116116
private function parseFile($path)
117117
{
118118
try {
119-
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS | Yaml::PARSE_CONSTANT);
119+
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_CONSTANT);
120120
} catch (ParseException $e) {
121121
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
122122
}

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

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

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

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

‎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/InlineTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/InlineTest.php
+10-7Lines 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()

0 commit comments

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