Skip to content

Commit 2d43e0d

Browse files
committed
properly catch legacy tag syntax usages
1 parent 9973789 commit 2d43e0d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Inline.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,22 @@ private static function parseTag(string $value, int &$i, int $flags): ?string
670670
$nextOffset += strspn($value, ' ', $nextOffset);
671671

672672
// Is followed by a scalar and is a built-in tag
673-
if ($tag && (!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
673+
if ('' !== $tag && (!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
674674
// Manage in {@link self::evaluateScalar()}
675675
return null;
676676
}
677677

678678
$i = $nextOffset;
679679

680680
// Built-in tags
681-
if ($tag && '!' === $tag[0]) {
681+
if ('' !== $tag && '!' === $tag[0]) {
682682
throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
683683
}
684684

685+
if ('' !== $tag && !isset($value[$i])) {
686+
throw new ParseException(sprintf('Missing value for tag "%s".', $tag), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
687+
}
688+
685689
if ('' === $tag || Yaml::PARSE_CUSTOM_TAGS & $flags) {
686690
return $tag;
687691
}

Tests/ParserTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Yaml\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Yaml\Exception\ParseException;
1516
use Symfony\Component\Yaml\Parser;
1617
use Symfony\Component\Yaml\Tag\TaggedValue;
1718
use Symfony\Component\Yaml\Yaml;
@@ -1841,6 +1842,14 @@ public function testPhpConstantTagMappingKey()
18411842
$this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
18421843
}
18431844

1845+
public function testDeprecatedPhpConstantSyntax()
1846+
{
1847+
$this->expectException(ParseException::class);
1848+
$this->expectExceptionMessage('Missing value for tag "php/const:App\Kernel::SEMART_VERSION" at line 1 (near "!php/const:App\Kernel::SEMART_VERSION").');
1849+
1850+
$this->parser->parse('!php/const:App\Kernel::SEMART_VERSION', Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_CONSTANT);
1851+
}
1852+
18441853
public function testMergeKeysWhenMappingsAreParsedAsObjects()
18451854
{
18461855
$yaml = <<<YAML

0 commit comments

Comments
 (0)