diff --git a/CHANGELOG.md b/CHANGELOG.md index aeb41695..50852cb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 --- diff --git a/Parser.php b/Parser.php index 4d88c108..146a6455 100644 --- a/Parser.php +++ b/Parser.php @@ -198,9 +198,14 @@ private function doParse(string $value, int $flags) array_pop($this->refsBeingParsed); } } elseif ( - self::preg_match('#^(?P(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{!].*?)) *\:(( |\t)++(?P.+))?$#u', rtrim($this->currentLine), $values) + // @todo in 7.0 remove legacy "(?:!?!php/const:)?" + self::preg_match('#^(?P(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P.+))?$#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); } diff --git a/Tests/ParserTest.php b/Tests/ParserTest.php index 00b7056c..7307aac8 100644 --- a/Tests/ParserTest.php +++ b/Tests/ParserTest.php @@ -12,6 +12,7 @@ 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; @@ -19,8 +20,9 @@ class ParserTest extends TestCase { - /** @var Parser */ - protected $parser; + use ExpectDeprecationTrait; + + private ?Parser $parser; protected function setUp(): void { @@ -1557,6 +1559,7 @@ public function testParseDateAsMappingValue() /** * @param $lineNumber * @param $yaml + * * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider */ public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml) @@ -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 = <<