diff --git a/src/php/Client/Welcome_Client.php b/src/php/Client/Welcome_Client.php index d71145901..661a3cdf2 100644 --- a/src/php/Client/Welcome_Client.php +++ b/src/php/Client/Welcome_Client.php @@ -3,7 +3,6 @@ namespace Code_Snippets\Client; use DateTimeImmutable; -use Exception; use WP_Filesystem_Direct; use const Code_Snippets\PLUGIN_FILE; @@ -123,11 +122,9 @@ private static function safe_get_array( array $items, string $key ): array { * @return DateTimeImmutable|null */ private static function safe_parse_datetime( string $datetime ): ?DateTimeImmutable { - try { - return new DateTimeImmutable( $datetime ); - } catch ( Exception $e ) { - return null; - } + $parsed = date_create_immutable( $datetime ); + + return $parsed ? $parsed : null; } /** @@ -265,9 +262,6 @@ public static function read_changelog(): array { $filesystem = new WP_Filesystem_Direct( null ); $changelog_filename = 'CHANGELOG.md'; - $changelog = []; - - $section_titles = [ 'Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security', 'Other' ]; $changelog_dir = plugin_dir_path( PLUGIN_FILE ); while ( plugin_dir_path( $changelog_dir ) !== $changelog_dir && ! $filesystem->exists( $changelog_dir . $changelog_filename ) ) { @@ -279,6 +273,21 @@ public static function read_changelog(): array { } $changelog_contents = $filesystem->get_contents( $changelog_dir . $changelog_filename ); + return self::parse_changelog_contents( $changelog_contents ); + } + + + /** + * Parse the contents of a changelog file into an array of releases. + * + * @param string $changelog_contents + * + * @return array>}> + */ + public static function parse_changelog_contents( string $changelog_contents ): array { + $changelog = []; + $section_titles = [ 'Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security', 'Other' ]; + $changelog_releases = explode( "\n## ", $changelog_contents ); foreach ( array_slice( $changelog_releases, 1, self::MAX_CHANGELOG_ENTRIES ) as $changelog_release ) { @@ -317,12 +326,8 @@ public static function read_changelog(): array { $version = trim( trim( $header_parts[0] ), '[]' ); $date = trim( trim( $header_parts[1] ?? '' ), '()' ); - try { - $datetime = new DateTimeImmutable( $date ); - $parsed_date = $datetime->format( get_option( 'date_format' ) ); - } catch ( Exception $e ) { - $parsed_date = $date; - } + $datetime = self::safe_parse_datetime( $date ); + $parsed_date = $datetime ? $datetime->format( get_option( 'date_format' ) ) : $date; $changelog[] = [ 'version' => $version, diff --git a/tests/unit/Client/Welcome_Client_Test.php b/tests/unit/Client/Welcome_Client_Test.php new file mode 100644 index 000000000..a8d555024 --- /dev/null +++ b/tests/unit/Client/Welcome_Client_Test.php @@ -0,0 +1,31 @@ +assertSame( [ '9.1.0', '9.0.0' ], array_column( $changelog, 'version' ) ); + $this->assertSame( [ 'UPCOMING', '2026-11-05' ], array_column( $changelog, 'date' ) ); + } +}