Skip to content

Commit 5a2904d

Browse files
yoeunesnicolas-grekas
authored andcommitted
[Validator] Fix call to undefined getParser() in YamlValidator
1 parent 7240869 commit 5a2904d

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

Constraints/YamlValidator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ public function validate(mixed $value, Constraint $constraint): void
3939

4040
$value = (string) $value;
4141

42+
$parser = new Parser();
43+
4244
/** @see \Symfony\Component\Yaml\Command\LintCommand::validate() */
43-
$prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) {
45+
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler, $parser) {
4446
if (\E_USER_DEPRECATED === $level) {
45-
throw new ParseException($message, $this->getParser()->getRealCurrentLineNb() + 1);
47+
throw new ParseException($message, $parser->getRealCurrentLineNb() + 1);
4648
}
4749

4850
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
4951
});
5052

5153
try {
52-
(new Parser())->parse($value, $constraint->flags);
54+
$parser->parse($value, $constraint->flags);
5355
} catch (ParseException $e) {
5456
$this->context->buildViolation($constraint->message)
5557
->setParameter('{{ error }}', $e->getMessage())

Tests/Constraints/YamlValidatorTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ public function testInvalidFlags()
7171
->assertRaised();
7272
}
7373

74+
/**
75+
* @dataProvider getDeprecationOnLinesData
76+
*/
77+
public function testDeprecationTriggersParseException(int $yamlLine, string $yamlValue)
78+
{
79+
$lines = explode("\n", $yamlValue);
80+
$errorLine = end($lines);
81+
$expectedError = 'This is a simulated deprecation at line '.$yamlLine.' (near "'.$errorLine.'")';
82+
83+
$constraint = new Yaml(
84+
message: 'myMessageTest',
85+
flags: YamlParser::PARSE_OBJECT,
86+
);
87+
$this->validator->validate($yamlValue, $constraint);
88+
$this->buildViolation('myMessageTest')
89+
->setParameter('{{ error }}', $expectedError)
90+
->setParameter('{{ line }}', $yamlLine)
91+
->setCode(Yaml::INVALID_YAML_ERROR)
92+
->assertRaised();
93+
}
94+
7495
public static function getValidValues()
7596
{
7697
return [
@@ -94,4 +115,34 @@ public static function getInvalidValues(): array
94115
["key:\nvalue", 'Unable to parse at line 2 (near "value").', 2],
95116
];
96117
}
118+
119+
/**
120+
* @return array<string, array{0: int, 1: string}>
121+
*/
122+
public static function getDeprecationOnLinesData(): array
123+
{
124+
$serialized = serialize(new DeprecatedObjectFixture());
125+
126+
return [
127+
'deprecation at line 1' => [1, "object: !php/object '".$serialized."'"],
128+
'deprecation at line 2' => [2, "valid: yaml\nobject: !php/object '".$serialized."'"],
129+
'deprecation at line 5' => [5, "line1: value\nline2: value\nline3: value\nline4: value\nobject: !php/object '".$serialized."'"],
130+
];
131+
}
132+
}
133+
134+
/**
135+
* Fixture class for triggering deprecation during unserialize.
136+
*/
137+
class DeprecatedObjectFixture
138+
{
139+
public function __serialize(): array
140+
{
141+
return [];
142+
}
143+
144+
public function __unserialize(array $data): void
145+
{
146+
@trigger_error('This is a simulated deprecation', \E_USER_DEPRECATED);
147+
}
97148
}

0 commit comments

Comments
 (0)