Skip to content

Commit fae4d48

Browse files
committed
Restore deprecated php/const: syntax in YAML key
1 parent ea9ed6c commit fae4d48

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for `!php/enum` and `!php/enum *->value`
8+
* Deprecate `!php/const:Class::CONSTANT` as key
89

910
6.1
1011
---

src/Symfony/Component/Yaml/Parser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,14 @@ private function doParse(string $value, int $flags)
198198
array_pop($this->refsBeingParsed);
199199
}
200200
} elseif (
201-
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
201+
// @todo in 7.0 remove legacy "(?:!?!php/const:)?"
202+
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
202203
&& (!str_contains($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
203204
) {
205+
if (str_starts_with($values['key'], '!php/const:')) {
206+
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));
207+
}
208+
204209
if ($context && 'sequence' == $context) {
205210
throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
206211
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
namespace Symfony\Component\Yaml\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Yaml\Exception\ParseException;
1617
use Symfony\Component\Yaml\Parser;
1718
use Symfony\Component\Yaml\Tag\TaggedValue;
1819
use Symfony\Component\Yaml\Yaml;
1920

2021
class ParserTest extends TestCase
2122
{
22-
/** @var Parser */
23-
protected $parser;
23+
use ExpectDeprecationTrait;
24+
25+
private ?Parser $parser;
2426

2527
protected function setUp(): void
2628
{
@@ -1557,6 +1559,7 @@ public function testParseDateAsMappingValue()
15571559
/**
15581560
* @param $lineNumber
15591561
* @param $yaml
1562+
*
15601563
* @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
15611564
*/
15621565
public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
@@ -2484,6 +2487,18 @@ public function testDeprecatedPhpConstantSyntax()
24842487
$this->parser->parse('!php/const:App\Kernel::SEMART_VERSION', Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_CONSTANT);
24852488
}
24862489

2490+
/**
2491+
* @group legacy
2492+
*/
2493+
public function testDeprecatedPhpConstantSyntaxAsScalarKey()
2494+
{
2495+
$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".');
2496+
$actual = $this->parser->parse('!php/const:Symfony\Component\Yaml\Tests\B::BAR: value', Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_CONSTANT);
2497+
$expected = ['bar' => 'value'];
2498+
2499+
$this->assertEquals($expected, $actual);
2500+
}
2501+
24872502
public function testPhpConstantTagMappingAsScalarKey()
24882503
{
24892504
$yaml = <<<YAML

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy