8000 :bug: throw ParseException on invalid date · symfony/symfony@6d71a7e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d71a7e

Browse files
committed
🐛 throw ParseException on invalid date
1 parent c582b76 commit 6d71a7e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,13 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
700700
case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
701701
return (float) str_replace('_', '', $scalar);
702702
case Parser::preg_match(self::getTimestampRegex(), $scalar):
703-
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
704-
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
703+
try {
704+
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
705+
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
706+
} catch (\Exception $e) {
707+
// Some dates accepted by the regex are not valid dates.
708+
throw new ParseException(\sprintf('The date "%s" could not be parsed as it is an invalid date.', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename, $e);
709+
}
705710

706711
if (Yaml::PARSE_DATETIME & $flags) {
707712
return $time;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,14 @@ public function testParseNestedTimestampListAsDateTimeObject(string $yaml, int $
579579
$this->assertEquals($expectedNested, Inline::parse($yamlNested, Yaml::PARSE_DATETIME));
580580
}
581581

582+
public function testParseInvalidDate()
583+
{
584+
$this->expectException(ParseException::class);
585+
$this->expectExceptionMessageMatches('/^The date "2024-50-50" could not be parsed as it is an invalid date.*/');
586+
587+
Inline::parse('2024-50-50', Yaml::PARSE_DATETIME);
588+
}
589+
582590
/**
583591
* @dataProvider getDateTimeDumpTests
584592
*/

0 commit comments

Comments
 (0)
0