Skip to content

Commit b63c55c

Browse files
GuilhemNxabbuh
authored andcommitted
[Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
1 parent c0d99d1 commit b63c55c

File tree

13 files changed

+114
-59
lines changed

13 files changed

+114
-59
lines changed

UPGRADE-3.4.md

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

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)) {

src/Symfony/Component/Yaml/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
}

src/Symfony/Component/Yaml/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
}

0 commit comments

Comments
 (0)