refactor: introduce InvalidDateFormatException for DatesHelper

This commit is contained in:
Jérémy Munsch 2026-08-17 09:43:34 +02:00
parent 2b7a8b4b99
commit e02f5f5c2a
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
3 changed files with 21 additions and 6 deletions

View file

@ -6,6 +6,7 @@ namespace Shared\Helpers;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Shared\Helpers\Exceptions\InvalidDateFormatException;
final class DatesHelper
{
@ -68,7 +69,7 @@ final class DatesHelper
try {
$output = new \DateTimeImmutable($datetime);
} catch (\Exception) {
throw new \InvalidArgumentException('Unrecognized date/time format: ' . $datetime);
throw InvalidDateFormatException::forDateString($datetime);
}
}
@ -113,7 +114,7 @@ final class DatesHelper
try {
return new \DateTimeImmutable($datetime);
} catch (\Throwable) {
throw new \InvalidArgumentException("Unrecognized date/time format: {$datetime}");
throw InvalidDateFormatException::forDateString($datetime);
}
}
}

View file

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Shared\Helpers\Exceptions;
final class InvalidDateFormatException extends \InvalidArgumentException
{
public static function forDateString(string $datetime): self
{
return new self("Unrecognized date/time format: {$datetime}");
}
}

View file

@ -6,6 +6,7 @@ namespace Tests\Integration\Shared\Helpers;
use Carbon\CarbonImmutable;
use Shared\Helpers\DatesHelper;
use Shared\Helpers\Exceptions\InvalidDateFormatException;
covers(DatesHelper::class);
@ -44,13 +45,13 @@ describe('Date Helper Integration Tests', function (): void {
['2025/12/25', '2025-12-25'],
]);
it('throws InvalidArgumentException for unparseable date strings', function (): void {
it('throws InvalidDateFormatException for unparseable date strings', function (): void {
// GIVEN
$invalidDate = 'not-a-real-date-string';
// WHEN & THEN
expect(fn () => DatesHelper::parseDateTime($invalidDate))
->toThrow(\InvalidArgumentException::class, 'Unrecognized date/time format: not-a-real-date-string');
->toThrow(InvalidDateFormatException::class, 'Unrecognized date/time format: not-a-real-date-string');
});
it('falls back to DateTimeImmutable for valid formats not fully handled by createFromFormat', function (): void {
@ -65,13 +66,13 @@ describe('Date Helper Integration Tests', function (): void {
->and($result->format('Y-m-d'))->toBe('2025-12-25');
});
it('throws InvalidArgumentException in parseFallback when fallback date is completely invalid logically', function (): void {
it('throws InvalidDateFormatException in parseFallback when fallback date is completely invalid logically', function (): void {
// GIVEN
$invalidFallbackDate = '2025-12-25T25:10:23.1234567Z'; // Invalid hour 25, matches regex but fails both createFromFormat and new DateTimeImmutable
// WHEN & THEN
expect(fn () => DatesHelper::parseDateTime($invalidFallbackDate))
->toThrow(\InvalidArgumentException::class, "Unrecognized date/time format: {$invalidFallbackDate}");
->toThrow(InvalidDateFormatException::class, "Unrecognized date/time format: {$invalidFallbackDate}");
});
it('handles isIn date boundary checks correctly', function (): void {