Skip to content

Commit d9bf253

Browse files
committed
minor symfony#22948 [Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS (GuilhemN)
This PR was squashed before being merged into the 3.4 branch (closes symfony#22948). Discussion ---------- [Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | yes <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | Sorry for opening this so lately... I just realized that we could get rid of `Yaml::PARSE_KEYS_AS_STRINGS` just by recommending using quotes... ~This way we don't allow a behavior not respecting the spec and we won't need to deprecate `PARSE_KEYS_AS_STRINGS` later.~ ~Is it too late for this to be merged in 3.3?~ ping @xabbuh Commits ------- b63c55c [Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
2 parents 2454a4f + b63c55c commit d9bf253

File tree

13 files changed

+102
-47
lines changed

13 files changed

+102
-47
lines changed

UPGRADE-3.4.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,30 @@ Yaml
162162

163163
* Using the non-specific tag `!` is deprecated and will have a different
164164
behavior in 4.0. Use a plain integer or `!!float` instead.
165+
166+
* Using the `Yaml::PARSE_KEYS_AS_STRINGS` flag is deprecated as it will be
167+
removed in 4.0.
168+
169+
Before:
170+
171+
```php
172+
$yaml = <<<YAML
173+
null: null key
174+
true: boolean true
175+
2.0: float key
176+
YAML;
177+
178+
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
179+
```
180+
181+
After:
182+
183+
```php
184+
$yaml = <<<YAML
185+
"null": null key
186+
"true": boolean true
187+
"2.0": float key
188+
YAML;
189+
190+
Yaml::parse($yaml);
191+
```

UPGRADE-4.0.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,32 @@ Yaml
721721
Yaml::parse($yaml);
722722
```
723723

724+
* Removed the `Yaml::PARSE_KEYS_AS_STRINGS` flag.
725+
726+
Before:
727+
728+
```php
729+
$yaml = <<<YAML
730+
null: null key
731+
true: boolean true
732+
2.0: float key
733+
YAML;
734+
735+
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
736+
```
737+
738+
After:
739+
740+
```php
741+
$yaml = <<<YAML
742+
"null": null key
743+
"true": boolean true
744+
"2.0": float key
745+
YAML;
746+
747+
Yaml::parse($yaml);
748+
```
749+
724750
* Omitting the key of a mapping is not supported anymore and throws a `ParseException`.
725751

726752
* 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)