Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34c5fb2

Browse files
committed
[Yaml] Recommend using quotes instead of PARSE_KEYS_AS_STRINGS
1 parent b0ede2c commit 34c5fb2

File tree

18 files changed

+119
-161
lines changed

18 files changed

+119
-161
lines changed

UPGRADE-3.3.md

Lines 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-3.4.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,42 @@ Finder
1010
------
1111

1212
* The `Symfony\Component\Finder\Iterator\FilterIterator` class has been
13-
deprecated and will be removed in 4.0 as it used to fix a bug which existed
13+
deprecated and will be removed in 4.0 as it used to fix a bug which existed
1414
before version 5.5.23/5.6.7.
1515

1616
Validator
1717
---------
1818

1919
* Not setting the `strict` option of the `Choice` constraint to `true` is
2020
deprecated and will throw an exception in Symfony 4.0.
21+
22+
Yaml
23+
----
24+
25+
* Using the `Yaml::PARSE_KEYS_AS_STRINGS` flag is deprecated as it will be
26+
removed in 4.0.
27+
28+
Before:
29+
30+
```php
31+
$yaml = <<<YAML
32+
null: null key
33+
true: boolean true
34+
2.0: float key
35+
YAML;
36+
37+
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
38+
```
39+
40+
After:
41+
42+
```php
43+
44+
$yaml = <<<YAML
45+
"null": null key
46+
"true": boolean true
47+
"2.0": float key
48+
YAML;
49+
50+
Yaml::parse($yaml);
51+
```

UPGRADE-4.0.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,15 @@ Yaml
562562
throws a `ParseException`.
563563

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

568568
Before:
569569

570570
```php
571571
$yaml = <<<YAML
572572
null: null key
573573
true: boolean true
574-
1: integer key
575574
2.0: float key
576575
YAML;
577576

@@ -582,16 +581,42 @@ Yaml
582581

583582
```php
584583

584+
$yaml = <<<YAML
585+
"null": null key
586+
"true": boolean true
587+
"2.0": float key
588+
YAML;
589+
590+
Yaml::parse($yaml);
591+
```
592+
593+
* Removed the `Yaml::PARSE_KEYS_AS_STRINGS` flag.
594+
595+
Before:
596+
597+
```php
585598
$yaml = <<<YAML
586599
null: null key
587600
true: boolean true
588-
1: integer key
589601
2.0: float key
590602
YAML;
591603

592604
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
593605
```
594606

607+
After:
608+
609+
```php
610+
611+
$yaml = <<<YAML
612+
"null": null key
613+
"true": boolean true
614+
"2.0": float key
615+
YAML;
616+
617+
Yaml::parse($yaml);
618+
```
619+
595620
* Omitting the key of a mapping is not supported anymore and throws a `ParseException`.
596621

597622
* 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
@@ -606,7 +606,7 @@ protected function loadFile($file)
606606
}
607607

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

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

Lines 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

Lines 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

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 & 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

Lines 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

Lines 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. Quote your evaluable mapping keys instead.', E_USER_DEPRECATED);
498497
}
499498
}
500499

src/Symfony/Component/Yaml/Parser.php

Lines changed: 7 additions & 3 deletions
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,9 +240,9 @@ private function doParse($value, $flags)
236240
throw $e;
237241
}
238242

239-
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags) && !is_string($key)) {
240-
$keyType = is_numeric($key) ? 'numeric key' : 'incompatible key type';
241-
@trigger_error(sprintf('Implicit casting of %s to string on line %d 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.', $keyType, $this->getRealCurrentLineNb()), E_USER_DEPRECATED);
243+
if (!is_string($key) && !is_int($key)) {
244+
$keyType = is_float($key) ? 'float key' : 'incompatible key type';
245+
@trigger_error(sprintf('Implicit casting of %s to string on line %d is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.', $keyType, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
242246
}
243247

244248
// Convert float keys to strings, to avoid being converted to integers by PHP

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
}

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)