Skip to content

Commit a2fc26e

Browse files
committed
Change warnings to Errors in Date extension
1 parent 1d69bf1 commit a2fc26e

25 files changed

+191
-155
lines changed

Zend/tests/bug54043.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Bug #54043: Remove inconsitency of internal exceptions and user defined exceptions
2+
Bug #54043: Remove inconsistency of internal exceptions and user defined exceptions
33
--FILE--
44
<?php
55

@@ -8,13 +8,13 @@ $timeZone = new DateTimeZone('UTC');
88

99
try {
1010
$dateTime = new DateTime($time, $timeZone);
11-
} catch (Exception $e) {
12-
var_dump($e->getMessage());
11+
} catch (\Error $e) {
12+
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
1313
}
1414

1515
var_dump(error_get_last());
1616

1717
?>
1818
--EXPECT--
19-
string(105) "DateTime::__construct(): Failed to parse time string (9999-11-33) at position 9 (3): Unexpected character"
19+
Error: Failed to parse time string (9999-11-33) at position 9 (3): Unexpected character
2020
NULL

ext/date/php_date.c

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,8 @@ static PHP_INI_MH(OnUpdate_date_timezone)
650650
if (stage == PHP_INI_STAGE_RUNTIME) {
651651
if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
652652
if (DATEG(default_timezone) && *DATEG(default_timezone)) {
653-
php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
653+
zend_value_error("Invalid date.timezone value '%s'", DATEG(default_timezone));
654+
return FAILURE;
654655
}
655656
} else {
656657
DATEG(timezone_valid) = 1;
@@ -683,7 +684,7 @@ static char* guess_timezone(const timelib_tzdb *tzdb)
683684
}
684685

685686
if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
686-
php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
687+
zend_value_error("Invalid date.timezone value '%s'", DATEG(default_timezone));
687688
return "UTC";
688689
}
689690

@@ -702,7 +703,7 @@ PHPAPI timelib_tzinfo *get_timezone_info(void)
702703
tz = guess_timezone(DATE_TIMEZONEDB);
703704
tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
704705
if (! tzi) {
705-
php_error_docref(NULL, E_ERROR, "Timezone database is corrupt - this should *never* happen!");
706+
zend_throw_error(NULL, "Timezone database is corrupt - this should *never* happen!");
706707
}
707708
return tzi;
708709
}
@@ -1099,8 +1100,8 @@ PHP_FUNCTION(idate)
10991100
ZEND_PARSE_PARAMETERS_END();
11001101

11011102
if (ZSTR_LEN(format) != 1) {
1102-
php_error_docref(NULL, E_WARNING, "idate format is one char");
1103-
RETURN_FALSE;
1103+
zend_value_error("idate format is one char");
1104+
return;
11041105
}
11051106

11061107
if (ZEND_NUM_ARGS() == 1) {
@@ -1109,8 +1110,8 @@ PHP_FUNCTION(idate)
11091110

11101111
ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
11111112
if (ret == -1) {
1112-
php_error_docref(NULL, E_WARNING, "Unrecognized date format token.");
1113-
RETURN_FALSE;
1113+
zend_value_error("Unrecognized date format token");
1114+
return;
11141115
}
11151116
RETURN_LONG(ret);
11161117
}
@@ -1897,7 +1898,7 @@ static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
18971898
o2 = Z_PHPDATE_P(d2);
18981899

18991900
if (!o1->time || !o2->time) {
1900-
php_error_docref(NULL, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
1901+
zend_throw_error(NULL, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
19011902
return 1;
19021903
}
19031904
if (!o1->time->sse_uptodate) {
@@ -2324,7 +2325,7 @@ PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str,
23242325

23252326
if (ctor && err && err->error_count) {
23262327
/* spit out the first library error message, at least */
2327-
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
2328+
zend_throw_error(NULL, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
23282329
err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
23292330
}
23302331
if (err && err->error_count) {
@@ -2855,7 +2856,7 @@ static int php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{
28552856
dateobj = Z_PHPDATE_P(object);
28562857

28572858
if (!(dateobj->time)) {
2858-
php_error_docref(NULL, E_WARNING, "The DateTime object has not been correctly initialized by its constructor");
2859+
zend_throw_error(NULL, "The DateTime object has not been correctly initialized by its constructor");
28592860
return 0;
28602861
}
28612862

@@ -2865,7 +2866,7 @@ static int php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{
28652866
update_errors_warnings(err);
28662867
if (err && err->error_count) {
28672868
/* spit out the first library error message, at least */
2868-
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
2869+
zend_throw_error(NULL, "Failed to parse time string (%s) at position %d (%c): %s", modify,
28692870
err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
28702871
timelib_time_dtor(tmp_time);
28712872
return 0;
@@ -3022,7 +3023,7 @@ static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{
30223023
DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);
30233024

30243025
if (intobj->diff->have_special_relative) {
3025-
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
3026+
zend_throw_error(NULL, "Only non-special relative time specifications are supported for subtraction");
30263027
return;
30273028
}
30283029

@@ -3474,14 +3475,14 @@ static int timezone_initialize(php_timezone_obj *tzobj, /*const*/ char *tz, size
34743475
char *orig_tz = tz;
34753476

34763477
if (strlen(tz) != tz_len) {
3477-
php_error_docref(NULL, E_WARNING, "Timezone must not contain null bytes");
3478+
zend_value_error("Timezone must not contain null bytes");
34783479
efree(dummy_t);
34793480
return FAILURE;
34803481
}
34813482

34823483
dummy_t->z = timelib_parse_zone(&tz, &dst, dummy_t, &not_found, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
34833484
if (not_found) {
3484-
php_error_docref(NULL, E_WARNING, "Unknown or bad timezone (%s)", orig_tz);
3485+
zend_value_error("Unknown or bad timezone (%s)", orig_tz);
34853486
efree(dummy_t);
34863487
return FAILURE;
34873488
} else {
@@ -3792,7 +3793,7 @@ static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *forma
37923793
timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
37933794

37943795
if (errors->error_count > 0) {
3795-
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
3796+
zend_value_error("Unknown or bad format (%s)", format);
37963797
retval = FAILURE;
37973798
} else {
37983799
if(p) {
@@ -3805,7 +3806,7 @@ static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *forma
38053806
*rt = timelib_diff(b, e);
38063807
retval = SUCCESS;
38073808
} else {
3808-
php_error_docref(NULL, E_WARNING, "Failed to parse interval (%s)", format);
3809+
zend_throw_error(NULL, "Failed to parse interval (%s)", format);
38093810
retval = FAILURE;
38103811
}
38113812
}
@@ -4097,7 +4098,7 @@ PHP_FUNCTION(date_interval_create_from_date_string)
40974098
time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
40984099

40994100
if (err->error_count > 0) {
4100-
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
4101+
zend_value_error("Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
41014102
err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
41024103
RETVAL_FALSE;
41034104
goto cleanup;
@@ -4215,7 +4216,7 @@ static int date_period_initialize(timelib_time **st, timelib_time **et, timelib_
42154216
timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
42164217

42174218
if (errors->error_count > 0) {
4218-
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
4219+
zend_value_error("Unknown or bad format (%s)", format);
42194220
retval = FAILURE;
42204221
} else {
42214222
*st = b;
@@ -4246,7 +4247,7 @@ PHP_METHOD(DatePeriod, __construct)
42464247
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOl|l", &start, date_ce_interface, &interval, date_ce_interval, &recurrences, &options) == FAILURE) {
42474248
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOO|l", &start, date_ce_interface, &interval, date_ce_interval, &end, date_ce_interface, &options) == FAILURE) {
42484249
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "s|l", &isostr, &isostr_len, &options) == FAILURE) {
4249-
php_error_docref(NULL, E_WARNING, "This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.");
4250+
zend_type_error("This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.");
42504251
zend_restore_error_handling(&error_handling);
42514252
return;
42524253
}
@@ -4259,13 +4260,16 @@ PHP_METHOD(DatePeriod, __construct)
42594260
if (isostr) {
42604261
date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), &recurrences, isostr, isostr_len);
42614262
if (dpobj->start == NULL) {
4262-
php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain a start date.", isostr);
4263+
zend_value_error("The ISO interval '%s' did not contain a start date.", isostr);
4264+
return;
42634265
}
42644266
if (dpobj->interval == NULL) {
4265-
php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an interval.", isostr);
4267+
zend_value_error("The ISO interval '%s' did not contain an interval.", isostr);
4268+
return;
42664269
}
42674270
if (dpobj->end == NULL && recurrences == 0) {
4268-
php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an end date or a recurrence count.", isostr);
4271+
zend_value_error("The ISO interval '%s' did not contain an end date or a recurrence count.", isostr);
4272+
return;
42694273
}
42704274

42714275
if (dpobj->start) {
@@ -4304,7 +4308,8 @@ PHP_METHOD(DatePeriod, __construct)
43044308
}
43054309

43064310
if (dpobj->end == NULL && recurrences < 1) {
4307-
php_error_docref(NULL, E_WARNING, "The recurrence count '%d' is invalid. Needs to be > 0", (int) recurrences);
4311+
zend_value_error("The recurrence count '%d' is invalid. Needs to be > 0", (int) recurrences);
4312+
return;
43084313
}
43094314

43104315
/* options */
@@ -4447,6 +4452,7 @@ PHP_FUNCTION(timezone_identifiers_list)
44474452

44484453
/* Extra validation */
44494454
if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
4455+
/* Upgrade to warning/exception? */
44504456
php_error_docref(NULL, E_NOTICE, "A two-letter ISO 3166-1 compatible country code is expected");
44514457
RETURN_FALSE;
44524458
}
@@ -4596,16 +4602,15 @@ static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_su
45964602
case 6:
45974603
break;
45984604
default:
4599-
php_error_docref(NULL, E_WARNING, "invalid format");
4600-
RETURN_FALSE;
4601-
break;
4605+
zend_value_error("Invalid format");
4606+
return;
46024607
}
46034608
if (retformat != SUNFUNCS_RET_TIMESTAMP &&
46044609
retformat != SUNFUNCS_RET_STRING &&
46054610
retformat != SUNFUNCS_RET_DOUBLE)
46064611
{
4607-
php_error_docref(NULL, E_WARNING, "Wrong return format given, pick one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE");
4608-
RETURN_FALSE;
4612+
zend_value_error("Wrong return format given, pick one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE");
4613+
return;
46094614
}
46104615
altitude = 90 - zenith;
46114616

ext/date/php_date.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function strtotime(string $time, int $now = UNKNOWN): int|false {}
77

88
function date(string $format, int $timestamp = UNKNOWN): string {}
99

10-
function idate(string $format, int $timestamp = UNKNOWN): int|false {}
10+
function idate(string $format, int $timestamp = UNKNOWN): int {}
1111

1212
function gmdate(string $format, int $timestamp = UNKNOWN): string {}
1313

ext/date/php_date_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_date, 0, 1, IS_STRING, 0)
1010
ZEND_ARG_TYPE_INFO(0, timestamp, IS_LONG, 0)
1111
ZEND_END_ARG_INFO()
1212

13-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_idate, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
13+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_idate, 0, 1, IS_LONG, 0)
1414
ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0)
1515
ZEND_ARG_TYPE_INFO(0, timestamp, IS_LONG, 0)
1616
ZEND_END_ARG_INFO()

ext/date/tests/005.phpt

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,43 @@ date_default_timezone_set('UTC');
66

77
$t = mktime(0,0,0, 6, 27, 2006);
88

9-
var_dump(idate(1,1));
10-
var_dump(idate(""));
11-
var_dump(idate(0));
9+
try {
10+
var_dump(idate(1,1));
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage() . \PHP_EOL;
13+
}
14+
15+
try {
16+
var_dump(idate(""));
17+
} catch (\ValueError $e) {
18+
echo $e->getMessage() . \PHP_EOL;
19+
}
20+
21+
try {
22+
var_dump(idate(0));
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage() . \PHP_EOL;
25+
}
1226

1327
var_dump(idate("B", $t));
14-
var_dump(idate("[", $t));
15-
var_dump(idate("'"));
1628

17-
echo "Done\n";
18-
?>
19-
--EXPECTF--
20-
Warning: idate(): Unrecognized date format token. in %s on line %d
21-
bool(false)
29+
try {
30+
var_dump(idate("[", $t));
31+
} catch (\ValueError $e) {
32+
echo $e->getMessage() . \PHP_EOL;
33+
}
2234

23-
Warning: idate(): idate format is one char in %s on line %d
24-
bool(false)
35+
try {
36+
var_dump(idate("'"));
37+
} catch (\ValueError $e) {
38+
echo $e->getMessage() . \PHP_EOL;
39+
}
2540

26-
Warning: idate(): Unrecognized date format token. in %s on line %d
27-
bool(false)
41+
?>
42+
--EXPECT--
43+
Unrecognized date format token
44+
idate format is one char
45+
Unrecognized date format token
2846
int(41)
29-
30-
Warning: idate(): Unrecognized date format token. in %s on line %d
31-
bool(false)
32-
33-
Warning: idate(): Unrecognized date format token. in %s on line %d
34-
bool(false)
35-
Done
47+
Unrecognized date format token
48+
Unrecognized date format token

ext/date/tests/DatePeriod_wrong_constructor.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ Havard Eide <nucleuz@gmail.com>
77
date.timezone=UTC
88
--FILE--
99
<?php
10-
new DatePeriod();
10+
try {
11+
new DatePeriod();
12+
} catch (\TypeError $exception) {
13+
echo $exception->getMessage(), "\n";
14+
}
1115
?>
12-
--EXPECTF--
13-
Fatal error: Uncaught Exception: DatePeriod::__construct(): This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments. in %s:%d
14-
Stack trace:
15-
#0 %s(%d): DatePeriod->__construct()
16-
#1 {main}
17-
thrown in %s on line %d
16+
--EXPECT--
17+
This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.

ext/date/tests/DatePeriod_wrong_recurrence_on_constructor.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ DatePeriod: Test wrong recurrence parameter on __construct
44
<?php
55
try {
66
new DatePeriod(new DateTime('yesterday'), new DateInterval('P1D'), 0);
7-
} catch (Exception $exception) {
7+
} catch (\ValueError $exception) {
88
echo $exception->getMessage(), "\n";
99
}
1010

1111
try {
1212
new DatePeriod(new DateTime('yesterday'), new DateInterval('P1D'),-1);
13-
} catch (Exception $exception) {
13+
} catch (\ValueError $exception) {
1414
echo $exception->getMessage(), "\n";
1515
}
1616
?>
17-
--EXPECTF--
18-
DatePeriod::__construct(): The recurrence count '0' is invalid. Needs to be > 0
19-
DatePeriod::__construct(): The recurrence count '-1' is invalid. Needs to be > 0
17+
--EXPECT--
18+
The recurrence count '0' is invalid. Needs to be > 0
19+
The recurrence count '-1' is invalid. Needs to be > 0

ext/date/tests/bug44562.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ try
88
{
99
$dp = new DatePeriod('2D');
1010
}
11-
catch ( Exception $e )
11+
catch (\ValueError $e )
1212
{
1313
echo $e->getMessage(), "\n";
1414
}
@@ -24,7 +24,7 @@ foreach ( $dp as $d )
2424

2525
?>
2626
--EXPECT--
27-
DatePeriod::__construct(): Unknown or bad format (2D)
27+
The ISO interval '2D' did not contain a start date.
2828
string(24) "2008-07-20T22:44:53+0200"
2929
string(24) "2008-07-21T22:44:53+0200"
3030
string(24) "2008-07-22T22:44:53+0200"

0 commit comments

Comments
 (0)