Skip to content

Commit

Permalink
[Yaml] Deprecate using the object and const tag without a value
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Jan 16, 2020
1 parent af4035d commit 73d8d4e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-5.1.md
Expand Up @@ -23,3 +23,8 @@ Routing
-------

* Deprecated `RouteCollectionBuilder` in favor of `RoutingConfigurator`.

Yaml
----

* Deprecated using the `!php/object` and `!php/const` tags without a value.
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added `yaml-lint` binary.
* Deprecated using the `!php/object` and `!php/const` tags without a value.

5.0.0
-----
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -435,6 +435,11 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
$isKeyQuoted = \in_array($mapping[$i], ['"', "'"], true);
$key = self::parseScalar($mapping, $flags, [':', ' '], $i, false, []);

if ('!php/const' === $key) {
$key .= self::parseScalar($mapping, $flags, [':', ' '], $i, false, []);
$key = self::evaluateScalar($key, $flags);
}

if ($offsetBeforeKeyParsing === $i) {
throw new ParseException('Missing mapping key.', self::$parsedLineNumber + 1, $mapping);
}
Expand Down Expand Up @@ -584,6 +589,12 @@ private static function evaluateScalar(string $scalar, int $flags, array $refere
return substr($scalar, 2);
case 0 === strpos($scalar, '!php/object'):
if (self::$objectSupport) {
if (!isset($scalar[12])) {
@trigger_error('Using the !php/object tag without a value is deprecated since Symfony 5.1.', E_USER_DEPRECATED);

return false;
}

return unserialize(self::parseScalar(substr($scalar, 12)));
}

Expand All @@ -594,6 +605,12 @@ private static function evaluateScalar(string $scalar, int $flags, array $refere
return null;
case 0 === strpos($scalar, '!php/const'):
if (self::$constantSupport) {
if (!isset($scalar[11])) {
@trigger_error('Using the !php/const tag without a value is deprecated since Symfony 5.1.', E_USER_DEPRECATED);

return '';
}

$i = 0;
if (\defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
return \constant($const);
Expand Down
51 changes: 51 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Expand Up @@ -736,4 +736,55 @@ public function getTestsForOctalNumbers()
'negative octal number' => [-28, '-034'],
];
}

/**
* @dataProvider phpObjectTagWithEmptyValueProvider
*
* @group legacy
*
* @expectedDeprecation Using the !php/object tag without a value is deprecated since Symfony 5.1.
*/
public function testPhpObjectWithEmptyValue($expected, $value)
{
$this->assertSame($expected, Inline::parse($value, Yaml::PARSE_OBJECT));
}

public function phpObjectTagWithEmptyValueProvider()
{
return [
[false, '!php/object'],
[false, '!php/object '],
[false, '!php/object '],
[[false], '[!php/object]'],
[[false], '[!php/object ]'],
[[false, 'foo'], '[!php/object , foo]'],
];
}

/**
* @dataProvider phpConstTagWithEmptyValueProvider
*
* @group legacy
*
* @expectedDeprecation Using the !php/const tag without a value is deprecated since Symfony 5.1.
*/
public function testPhpConstTagWithEmptyValue($expected, $value)
{
$this->assertSame($expected, Inline::parse($value, Yaml::PARSE_CONSTANT));
}

public function phpConstTagWithEmptyValueProvider()
{
return [
['', '!php/const'],
['', '!php/const '],
['', '!php/const '],
[[''], '[!php/const]'],
[[''], '[!php/const ]'],
[['', 'foo'], '[!php/const , foo]'],
[['' => 'foo'], '{!php/const: foo}'],
[['' => 'foo'], '{!php/const : foo}'],
[['' => 'foo', 'bar' => 'ccc'], '{!php/const : foo, bar: ccc}'],
];
}
}

0 comments on commit 73d8d4e

Please sign in to comment.