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

Skip to content

Commit 592eb38

Browse files
committed
[Yaml] Deprecate tags using colon
1 parent 0cfb574 commit 592eb38

File tree

8 files changed

+151
-21
lines changed

8 files changed

+151
-21
lines changed

UPGRADE-3.4.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,33 @@ 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 `!php/object:` tag is deprecated and won't be supported in 4.0. Use
26+
the `!php/object` tag (without the colon) instead.
27+
28+
* using the `!php/const:` tag is deprecated and won't be supported in 4.0. Use
29+
the `!php/const` tag (without the colon) instead.
30+
31+
Before:
32+
33+
```yml
34+
!php/const:PHP_INT_MAX
35+
```
36+
37+
After:
38+
39+
```yml
40+
!php/const PHP_INT_MAX
41+
```
42+
>>>>>>> [Yaml] Deprecate tags using colon

UPGRADE-4.0.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,21 @@ Yaml
685685

686686
* The constructor arguments `$offset`, `$totalNumberOfLines` and
687687
`$skippedLineNumbers` of the `Parser` class were removed.
688+
689+
* The `!php/object:` tag was removed in favor of the `!php/object` tag (without
690+
the colon).
691+
692+
* The `!php/const:` tag was removed in favor of the `!php/const` tag (without
693+
the colon).
694+
695+
Before:
696+
697+
```yml
698+
!php/const:PHP_INT_MAX
699+
```
700+
701+
After:
702+
703+
```yml
704+
!php/const PHP_INT_MAX
705+
```

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
3.4.0
5+
-----
6+
7+
* using the `!php/object:` tag is deprecated and won't be supported in 4.0. Use
8+
the `!php/object` tag (without the colon) instead.
9+
10+
* using the `!php/const:` tag is deprecated and won't be supported in 4.0. Use
11+
the `!php/const` tag (without the colon) instead.
12+
413
3.3.0
514
-----
615

src/Symfony/Component/Yaml/Inline.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static function dump($value, $flags = 0)
170170
}
171171

172172
if (Yaml::DUMP_OBJECT & $flags) {
173-
return '!php/object:'.serialize($value);
173+
return '!php/object '.self::dump(serialize($value));
174174
}
175175

176176
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
@@ -614,6 +614,8 @@ private static function evaluateScalar($scalar, $flags, $references = array())
614614
return (int) self::parseScalar(substr($scalar, 2), $flags);
615615
case 0 === strpos($scalar, '!php/object:'):
616616
if (self::$objectSupport) {
617+
@trigger_error('The !php/object: tag to indicate dumped PHP objects is deprecated since version 3.4 and will be removed in 4.0. Use the !php/object (without the colon) tag instead.', E_USER_DEPRECATED);
618+
617619
return unserialize(substr($scalar, 12));
618620
}
619621

@@ -624,7 +626,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
624626
return;
625627
case 0 === strpos($scalar, '!!php/object:'):
626628
if (self::$objectSupport) {
627-
@trigger_error('The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead.', E_USER_DEPRECATED);
629+
@trigger_error('The !!php/object: tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object (without the colon) tag instead.', E_USER_DEPRECATED);
628630

629631
return unserialize(substr($scalar, 13));
630632
}
@@ -633,9 +635,21 @@ private static function evaluateScalar($scalar, $flags, $references = array())
633635
throw new ParseException('Object support when parsing a YAML file has been disabled.');
634636
}
635637

638+
return;
639+
case 0 === strpos($scalar, '!php/object'):
640+
if (self::$objectSupport) {
641+
return unserialize(self::parseScalar(substr($scalar, 12)));
642+
}
643+
644+
if (self::$exceptionOnInvalidType) {
645+
throw new ParseException('Object support when parsing a YAML file has been disabled.');
646+
}
647+
636648
return;
637649
case 0 === strpos($scalar, '!php/const:'):
638650
if (self::$constantSupport) {
651+
@trigger_error('The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.', E_USER_DEPRECATED);
652+
639653
if (defined($const = substr($scalar, 11))) {
640654
return constant($const);
641655
}
@@ -646,6 +660,20 @@ private static function evaluateScalar($scalar, $flags, $references = array())
646660
throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Have you forgotten to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar));
647661
}
648662

663+
return;
664+
665+
case 0 === strpos($scalar, '!php/const'):
666+
if (self::$constantSupport) {
667+
if (defined($const = self::parseScalar(substr($scalar, 11)))) {
668+
return constant($const);
669+
}
670+
671+
throw new ParseException(sprintf('The constant "%s" is not defined.', $const));
672+
}
673+
if (self::$exceptionOnInvalidType) {
674+
throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Have you forgotten to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar));
675+
}
676+
649677
return;
650678
case 0 === strpos($scalar, '!!float '):
651679
return (float) substr($scalar, 8);

src/Symfony/Component/Yaml/Parser.php

Lines changed: 1 addition & 1 deletion
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) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testObjectSupportEnabled()
210210
{
211211
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
212212

213-
$this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
213+
$this->assertEquals('{ foo: !php/object \'O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}\', bar: 1 }', $dump, '->dump() is able to dump objects');
214214
}
215215

216216
/**
@@ -220,7 +220,7 @@ public function testObjectSupportEnabledPassingTrue()
220220
{
221221
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
222222

223-
$this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
223+
$this->assertEquals('{ foo: !php/object \'O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}\', bar: 1 }', $dump, '->dump() is able to dump objects');
224224
}
225225

226226
public function testObjectSupportDisabledButNoExceptions()

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function testParsePhpConstants($yaml, $value)
4949
public function getTestsForParsePhpConstants()
5050
{
5151
return array(
52-
array('!php/const:Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
53-
array('!php/const:PHP_INT_MAX', PHP_INT_MAX),
54-
array('[!php/const:PHP_INT_MAX]', array(PHP_INT_MAX)),
55-
array('{ foo: !php/const:PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
52+
array('!php/const Symfony\Component\Yaml\Yaml::PARSE_CONSTANT', Yaml::PARSE_CONSTANT),
53+
array('!php/const PHP_INT_MAX', PHP_INT_MAX),
54+
array('[!php/const PHP_INT_MAX]', array(PHP_INT_MAX)),
55+
array('{ foo: !php/const PHP_INT_MAX }', array('foo' => PHP_INT_MAX)),
5656
);
5757
}
5858

@@ -62,16 +62,25 @@ public function getTestsForParsePhpConstants()
6262
*/
6363
public function testParsePhpConstantThrowsExceptionWhenUndefined()
6464
{
65-
Inline::parse('!php/const:WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
65+
Inline::parse('!php/const WRONG_CONSTANT', Yaml::PARSE_CONSTANT);
6666
}
6767

6868
/**
6969
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
70-
* @expectedExceptionMessageRegExp #The string "!php/const:PHP_INT_MAX" could not be parsed as a constant.*#
70+
* @expectedExceptionMessageRegExp #The string "!php/const PHP_INT_MAX" could not be parsed as a constant.*#
7171
*/
7272
public function testParsePhpConstantThrowsExceptionOnInvalidType()
7373
{
74-
Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
74+
Inline::parse('!php/const PHP_INT_MAX', Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
75+
}
76+
77+
/**
78+
* @group legacy
79+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
80+
*/
81+
public function testDeprecatedConstantTag()
82+
{
83+
Inline::parse('!php/const:PHP_INT_MAX', Yaml::PARSE_CONSTANT);
7584
}
7685

7786
/**

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public function testBlockLiteralWithLeadingNewlines()
469469
public function testObjectSupportEnabled()
470470
{
471471
$input = <<<'EOF'
472-
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
472+
foo: !php/object O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
473473
bar: 1
474474
EOF;
475475
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
@@ -489,14 +489,29 @@ public function testObjectSupportEnabledPassingTrue()
489489

490490
/**
491491
* @group legacy
492+
* @dataProvider deprecatedObjectValueProvider
492493
*/
493-
public function testObjectSupportEnabledWithDeprecatedTag()
494+
public function testObjectSupportEnabledWithDeprecatedTag($yaml)
494495
{
495-
$input = <<<'EOF'
496+
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($yaml, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
497+
}
498+
499+
public function deprecatedObjectValueProvider()
500+
{
501+
return array(
502+
array(
503+
<<<YAML
496504
foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
497505
bar: 1
498-
EOF;
499-
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
506+
YAML
507+
),
508+
array(
509+
<<<YAML
510+
foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
511+
bar: 1
512+
YAML
513+
),
514+
);
500515
}
501516

502517
/**
@@ -1823,6 +1838,35 @@ public function testParserCleansUpReferencesBetweenRuns()
18231838
public function testPhpConstantTagMappingKey()
18241839
{
18251840
$yaml = <<<YAML
1841+
transitions:
1842+
!php/const 'Symfony\Component\Yaml\Tests\B::FOO':
1843+
from:
1844+
- !php/const 'Symfony\Component\Yaml\Tests\B::BAR'
1845+
to: !php/const 'Symfony\Component\Yaml\Tests\B::BAZ'
1846+
YAML;
1847+
$expected = array(
1848+
'transitions' => array(
1849+
'foo' => array(
1850+
'from' => array(
1851+
'bar',
1852+
),
1853+
'to' => 'baz',
1854+
),
1855+
),
1856+
);
1857+
1858+
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
1859+
}
1860+
1861+
/**
1862+
* @group legacy
1863+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
1864+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
1865+
* @expectedDeprecation The !php/const: tag to indicate dumped PHP constants is deprecated since version 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.
1866+
*/
1867+
public function testDeprecatedPhpConstantTagMappingKey()
1868+
{
1869+
$yaml = <<<YAML
18261870
transitions:
18271871
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
18281872
from:
@@ -1847,10 +1891,10 @@ public function testPhpConstantTagMappingKeyWithKeysCastToStrings()
18471891
{
18481892
$yaml = <<<YAML
18491893
transitions:
1850-
!php/const:Symfony\Component\Yaml\Tests\B::FOO:
1894+
!php/const 'Symfony\Component\Yaml\Tests\B::FOO':
18511895
from:
1852-
- !php/const:Symfony\Component\Yaml\Tests\B::BAR
1853-
to: !php/const:Symfony\Component\Yaml\Tests\B::BAZ
1896+
- !php/const 'Symfony\Component\Yaml\Tests\B::BAR'
1897+
to: !php/const 'Symfony\Component\Yaml\Tests\B::BAZ'
18541898
YAML;
18551899
$expected = array(
18561900
'transitions' => array(

0 commit comments

Comments
 (0)