Skip to content

Commit

Permalink
[Yaml] Restore deprecated php/const: syntax in YAML key
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN authored and nicolas-grekas committed Nov 14, 2022
1 parent 9461dec commit d3cf343
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions 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 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 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

0 comments on commit d3cf343

Please sign in to comment.