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

Skip to content

[Yaml] Restore deprecated php/const: syntax in YAML key #48207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add support for `!php/enum` and `!php/enum *->value`
* Deprecate the `!php/const:` tag in key which will be replaced by the `!php/const` tag (without the colon) since 3.4

6.1
---
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,14 @@ private function doParse(string $value, int $flags)
array_pop($this->refsBeingParsed);
}
} elseif (
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
// @todo in 7.0 remove legacy "(?:!?!php/const:)?"
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
&& (!str_contains($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
) {
if (str_starts_with($values['key'], '!php/const:')) {
trigger_deprecation('symfony/yaml', '6.2', 'YAML syntax for key "%s" is deprecated and replaced by "!php/const %s".', $values['key'], substr($values['key'], 11));
}

if ($context && 'sequence' == $context) {
throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
}
Expand Down
18 changes: 16 additions & 2 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
namespace Symfony\Component\Yaml\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml;

class ParserTest extends TestCase
{
/** @var Parser */
protected $parser;
use ExpectDeprecationTrait;

private ?Parser $parser;

protected function setUp(): void
{
Expand Down Expand Up @@ -1557,6 +1559,7 @@ public function testParseDateAsMappingValue()
/**
* @param $lineNumber
* @param $yaml
*
* @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
*/
public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
Expand Down Expand Up @@ -2484,6 +2487,17 @@ public function testDeprecatedPhpConstantSyntax()
$this->parser->parse('!php/const:App\Kernel::SEMART_VERSION', Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_CONSTANT);
}

/**
* @group legacy
*/
public function testDeprecatedPhpConstantSyntaxAsScalarKey()
{
$this->expectDeprecation('Since symfony/yaml 6.2: YAML syntax for key "!php/const:Symfony\Component\Yaml\Tests\B::BAR" is deprecated and replaced by "!php/const Symfony\Component\Yaml\Tests\B::BAR".');
$actual = $this->parser->parse('!php/const:Symfony\Component\Yaml\Tests\B::BAR: value', Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_CONSTANT);

$this->assertSame(['bar' => 'value'], $actual);
}

public function testPhpConstantTagMappingAsScalarKey()
{
$yaml = <<<YAML
Expand Down