diff --git a/src/Reader/Feed/Rss.php b/src/Reader/Feed/Rss.php index bac66b03..f7208bc1 100644 --- a/src/Reader/Feed/Rss.php +++ b/src/Reader/Feed/Rss.php @@ -206,23 +206,22 @@ public function getDateModified() DateTime::RSS, DateTime::RFC822, DateTime::RFC2822, - null, ]; foreach ($dateStandards as $standard) { - try { - $date = DateTime::createFromFormat($standard, $dateModified); + $date = DateTime::createFromFormat( + $standard, + $dateModified + ); + if ($date instanceof DateTime) { break; - } catch (\Exception $e) { - if ($standard === null) { - throw new Exception\RuntimeException( - 'Could not load date due to unrecognised format' - . ' (should follow RFC 822 or 2822): ' . $e->getMessage(), - 0, - $e - ); - } } } + if (! $date) { + throw new Exception\RuntimeException( + 'Could not load date due to unrecognised' + . ' format (should follow RFC 822 or 2822).' + ); + } } } } diff --git a/test/Reader/Feed/RssTest.php b/test/Reader/Feed/RssTest.php index 52407f0b..b0f4719b 100644 --- a/test/Reader/Feed/RssTest.php +++ b/test/Reader/Feed/RssTest.php @@ -19,6 +19,7 @@ */ class RssTest extends TestCase { + /** @var string */ protected $feedSamplePath; protected $expectedCats = []; @@ -2178,6 +2179,22 @@ public function dateModifiedProvider(): array ]; } + public function testGetDateModifiedShouldThrowExceptionForInvalidDate(): void + { + $feed = Reader\Reader::importString( + file_get_contents( + $this->feedSamplePath . '/datemodified/plain/invalid.xml' + ) + ); + + $this->expectException(Reader\Exception\RuntimeException::class); + $this->expectExceptionMessage( + 'Could not load date due to unrecognised format (should follow RFC 822 or 2822).' + ); + + $feed->getDateModified(); + } + /** * Get Hubs (Unencoded Text) * diff --git a/test/Reader/Feed/_files/Rss/datemodified/plain/invalid.xml b/test/Reader/Feed/_files/Rss/datemodified/plain/invalid.xml new file mode 100644 index 00000000..ccf3494e --- /dev/null +++ b/test/Reader/Feed/_files/Rss/datemodified/plain/invalid.xml @@ -0,0 +1,7 @@ + + + + + Sat, 07 Mar 08:03:50 +0000 + +