Skip to content

Commit 466a93d

Browse files
committed
DateTime: strict behavior (BC break)
1 parent b7d65bb commit 466a93d

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

src/Utils/DateTime.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function modify(string $modifier): static
120120
public function setDate(int $year, int $month, int $day): static
121121
{
122122
if (!checkdate($month, $day, $year)) {
123-
trigger_error(sprintf(self::class . ': The date %04d-%02d-%02d is not valid.', $year, $month, $day), E_USER_WARNING);
123+
throw new \Exception(sprintf('The date %04d-%02d-%02d is not valid.', $year, $month, $day));
124124
}
125125
return parent::setDate($year, $month, $day);
126126
}
@@ -134,7 +134,7 @@ public function setTime(int $hour, int $minute, int $second = 0, int $microsecon
134134
|| $second < 0 || $second >= 60
135135
|| $microsecond < 0 || $microsecond >= 1_000_000
136136
) {
137-
trigger_error(sprintf(self::class . ': The time %02d:%02d:%08.5F is not valid.', $hour, $minute, $second + $microsecond / 1_000_000), E_USER_WARNING);
137+
throw new \Exception(sprintf('The time %02d:%02d:%08.5F is not valid.', $hour, $minute, $second + $microsecond / 1_000_000));
138138
}
139139
return parent::setTime($hour, $minute, $second, $microsecond);
140140
}
@@ -211,7 +211,7 @@ private function handleErrors(string $value): void
211211
$errors = self::getLastErrors();
212212
$errors = array_merge($errors['errors'] ?? [], $errors['warnings'] ?? []);
213213
if ($errors) {
214-
trigger_error(self::class . ': ' . implode(', ', $errors) . " '$value'", E_USER_WARNING);
214+
throw new \Exception(implode(', ', $errors) . " '$value'");
215215
}
216216
}
217217
}

tests/Utils/DateTime.construct.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,21 @@ test('Exception handling for invalid input', function () {
101101
'%a%invalid date format%a%',
102102
);
103103

104-
Assert::error(
104+
Assert::exception(
105105
fn() => new DateTime('0000-00-00'),
106-
E_USER_WARNING,
107-
"Nette\\Utils\\DateTime: The parsed date was invalid '0000-00-00'",
106+
Throwable::class,
107+
"The parsed date was invalid '0000-00-00'",
108108
);
109109

110-
Assert::error(
110+
Assert::exception(
111111
fn() => new DateTime('2024-02-31 10:00:00'), // Invalid day for February
112-
E_USER_WARNING,
113-
"Nette\\Utils\\DateTime: The parsed date was invalid '2024-02-31 10:00:00'",
112+
Throwable::class,
113+
"The parsed date was invalid '2024-02-31 10:00:00'",
114114
);
115115

116-
Assert::error(
116+
Assert::exception(
117117
fn() => new DateTime('1978-01-23 23:00:60'),
118-
E_USER_WARNING,
119-
"Nette\\Utils\\DateTime: The parsed time was invalid '1978-01-23 23:00:60'",
118+
Throwable::class,
119+
"The parsed time was invalid '1978-01-23 23:00:60'",
120120
);
121121
});

tests/Utils/DateTime.modify.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,22 @@ test('Complex and varied format strings', function () {
167167

168168
test('Invalid modifier format exceptions', function () {
169169
if (PHP_VERSION_ID < 80300) {
170-
Assert::error(
170+
Assert::exception(
171171
fn() => (new DateTime)->modify('+'),
172-
E_WARNING,
172+
Throwable::class,
173173
'DateTime::modify(): Failed to parse time string (+) at position 0 (+): Unexpected character',
174174
);
175175
} else {
176-
Assert::error(
176+
Assert::exception(
177177
fn() => (new DateTime)->modify('+'),
178178
DateMalformedStringException::class,
179179
'DateTime::modify(): Failed to parse time string (+) at position 0 (+): Unexpected character',
180180
);
181181
}
182182

183-
Assert::error(
183+
Assert::exception(
184184
fn() => (new DateTime)->modify('2024-02-31 10:00:00'), // Invalid day for February
185-
E_USER_WARNING,
186-
"Nette\\Utils\\DateTime: The parsed date was invalid '2024-02-31 10:00:00'",
185+
Throwable::class,
186+
"The parsed date was invalid '2024-02-31 10:00:00'",
187187
);
188188
});

tests/Utils/DateTime.relativeToSeconds.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ test('relativeToSeconds throws on invalid', function () {
2929
'%a?%Failed to parse time string %a%',
3030
);
3131

32-
Assert::error(
32+
Assert::exception(
3333
fn() => DateTime::relativeToSeconds('1 minu'),
34-
E_USER_WARNING,
34+
Throwable::class,
3535
);
3636
});

tests/Utils/DateTime.setDateTime.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use Tester\Assert;
1212
require __DIR__ . '/../bootstrap.php';
1313

1414

15-
Assert::error(
15+
Assert::exception(
1616
fn() => (new DateTime)->setDate(1978, 2, 31),
17-
E_USER_WARNING,
18-
'Nette\Utils\DateTime: The date 1978-02-31 is not valid.',
17+
Throwable::class,
18+
'The date 1978-02-31 is not valid.',
1919
);
2020

21-
Assert::error(
21+
Assert::exception(
2222
fn() => (new DateTime)->setTime(0, 60),
23-
E_USER_WARNING,
24-
'Nette\Utils\DateTime: The time 00:60:00.00000 is not valid.',
23+
Throwable::class,
24+
'The time 00:60:00.00000 is not valid.',
2525
);

0 commit comments

Comments
 (0)