From e45354bdf497af1ed7ad0121be92d4678c431d7a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 11 Oct 2024 14:51:43 +0200 Subject: [PATCH] silence PHP warning when an invalid date interval format string is used --- Tests/Trigger/PeriodicalTriggerTest.php | 15 ++++++++------- Trigger/PeriodicalTrigger.php | 6 +++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Tests/Trigger/PeriodicalTriggerTest.php b/Tests/Trigger/PeriodicalTriggerTest.php index a6deba0..22162e7 100644 --- a/Tests/Trigger/PeriodicalTriggerTest.php +++ b/Tests/Trigger/PeriodicalTriggerTest.php @@ -60,21 +60,22 @@ public static function provideForConstructor(): iterable /** * @dataProvider getInvalidIntervals */ - public function testInvalidInterval($interval) + public function testInvalidInterval($interval, $expectedExceptionMessage) { $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage($expectedExceptionMessage); new PeriodicalTrigger($interval, $now = new \DateTimeImmutable(), $now->modify('1 day')); } public static function getInvalidIntervals(): iterable { - yield ['wrong']; - yield ['3600.5']; - yield ['-3600']; - yield [-3600]; - yield ['0']; - yield [0]; + yield ['wrong', 'Unknown or bad format (wrong) at position 0 (w): The timezone could not be found in the database']; + yield ['3600.5', 'Unknown or bad format (3600.5) at position 5 (5): Unexpected character']; + yield ['-3600', 'Unknown or bad format (-3600) at position 3 (0): Unexpected character']; + yield [-3600, 'The "$interval" argument must be greater than zero.']; + yield ['0', 'The "$interval" argument must be greater than zero.']; + yield [0, 'The "$interval" argument must be greater than zero.']; } /** diff --git a/Trigger/PeriodicalTrigger.php b/Trigger/PeriodicalTrigger.php index fb5cfc9..177737a 100644 --- a/Trigger/PeriodicalTrigger.php +++ b/Trigger/PeriodicalTrigger.php @@ -52,7 +52,11 @@ public function __construct( $i = $interval; if (\is_string($interval)) { $this->description = sprintf('every %s', $interval); - $i = \DateInterval::createFromDateString($interval); + $i = @\DateInterval::createFromDateString($interval); + + if (false === $i) { + throw new InvalidArgumentException(sprintf('Invalid interval "%s": ', $interval).error_get_last()['message']); + } } else { $a = (array) $interval; $this->description = \PHP_VERSION_ID >= 80200 && $a['from_string'] ? $a['date_string'] : 'DateInterval';