Skip to content

Commit bf3673a

Browse files
committed
ext/intl: TimeZone address todo to throw exceptions on error.
close GH-17215
1 parent 4e9cde7 commit bf3673a

9 files changed

+61
-58
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ PHP NEWS
2828

2929
- Intl:
3030
. Bumped ICU requirement to ICU >= 57.1. (cmb)
31+
. IntlDateFormatter::setTimeZone()/datefmt_set_timezone() throws an exception
32+
with uninitialised classes or clone failure. (David Carlier)
3133

3234
- MySQLnd:
3335
. Added mysqlnd.collect_memory_statistics to ini quick reference.

UPGRADING

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ PHP 8.5 UPGRADE NOTES
100100
5. Changed Functions
101101
========================================
102102

103-
- Zlib:
104-
. The "use_include_path" argument for the
105-
gzfile, gzopen and readgzfile functions had been changed
106-
from int to boolean.
103+
- Intl:
104+
. IntlDateFormatter::setTimeZone()()/datefmt_set_timezone
105+
throws an IntlException on uninitialised classes/clone failures.
107106

108107
- PCNTL:
109108
. pcntl_exec() now has a formal return type of false.
@@ -125,6 +124,11 @@ PHP 8.5 UPGRADE NOTES
125124
. posix_fpathconf checks invalid file descriptors and sets
126125
last_error to EBADF and raises an E_WARNING message.
127126

127+
- Zlib:
128+
. The "use_include_path" argument for the
129+
gzfile, gzopen and readgzfile functions had been changed
130+
from int to boolean.
131+
128132
========================================
129133
6. New Functions
130134
========================================

ext/intl/tests/bug62017.phpt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ intl
55
--FILE--
66
<?php
77
ini_set('intl.error_level', E_WARNING);
8-
var_dump(
8+
try {
99
datefmt_create('', IntlDateFormatter::NONE, IntlDateFormatter::NONE, "\xFF",
10-
IntlDateFormatter::GREGORIAN, 'a'));
10+
IntlDateFormatter::GREGORIAN, 'a');
11+
} catch (IntlException $e) {
12+
echo PHP_EOL."Exception: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . PHP_EOL;
13+
}
14+
1115
try {
12-
var_dump(
1316
new IntlDateFormatter('', IntlDateFormatter::NONE, IntlDateFormatter::NONE, "Europe/Lisbon",
14-
IntlDateFormatter::GREGORIAN, "\x80"));
17+
IntlDateFormatter::GREGORIAN, "\x80");
1518
} catch (IntlException $e) {
1619
echo PHP_EOL."Exception: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine() . PHP_EOL;
1720
}
1821
?>
1922
--EXPECTF--
20-
Warning: datefmt_create(): datefmt_create: Time zone identifier given is not a valid UTF-8 string in %s on line %d
21-
NULL
2223

23-
Exception: IntlDateFormatter::__construct(): datefmt_create: error converting pattern to UTF-16 in %sbug62017.php on line %d
24+
Exception: datefmt_create: Time zone identifier given is not a valid UTF-8 string in %s on line %d
25+
26+
Exception: IntlDateFormatter::__construct(): datefmt_create: error converting pattern to UTF-16 in %s on line %d

ext/intl/tests/calendar_createInstance_error.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ class X extends IntlTimeZone {
1010
function __construct() {}
1111
}
1212

13-
var_dump(intlcal_create_instance(new X, NULL));
13+
try {
14+
intlcal_create_instance(new X, NULL);
15+
} catch (IntlException $e) {
16+
echo $e->getMessage();
17+
}
1418
?>
15-
--EXPECTF--
16-
Warning: intlcal_create_instance(): intlcal_create_instance: passed IntlTimeZone is not properly constructed in %s on line %d
17-
NULL
19+
--EXPECT--
20+
intlcal_create_instance: passed IntlTimeZone is not properly constructed

ext/intl/tests/dateformat___construct_bad_tz_cal.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ try {
2929
}
3030
?>
3131
--EXPECTF--
32-
Exception: IntlDateFormatter::__construct(): datefmt_create: No such time zone: 'bad timezone' in %s on line %d
32+
33+
Exception: datefmt_create: No such time zone: 'bad timezone' in %s on line %d
3334

3435
Exception: IntlDateFormatter::__construct(): datefmt_create: Invalid value for calendar type; it must be one of IntlDateFormatter::TRADITIONAL (locale's default calendar) or IntlDateFormatter::GREGORIAN. Alternatively, it can be an IntlCalendar object in %s on line %d
3536

ext/intl/tests/dateformat_setTimeZone_error.phpt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ ini_set("date.timezone", 'Atlantic/Azores');
1010

1111
$df = new IntlDateFormatter(NULL, 0, 0);
1212

13-
var_dump($df->setTimeZone(array()));
14-
var_dump($df->setTimeZone('non existing timezone'));
13+
try {
14+
$df->setTimeZone(array());
15+
} catch (IntlException $e) {
16+
echo $e->getMessage() . PHP_EOL;
17+
}
1518

19+
try {
20+
$df->setTimeZone('non existing timezone');
21+
} catch (IntlException $e) {
22+
echo $e->getMessage();
23+
}
1624
?>
1725
--EXPECTF--
1826
Warning: Array to string conversion in %s on line %d
19-
20-
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'Array' in %s on line %d
21-
bool(false)
22-
23-
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'non existing timezone' in %s on line %d
24-
bool(false)
27+
datefmt_set_timezone: No such time zone: 'Array'
28+
datefmt_set_timezone: No such time zone: 'non existing timezone'

ext/intl/tests/dateformat_set_timezone_id3.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ function ut_main()
3737

3838
$res_str .= "-----------";
3939
$res_str .= "\nTrying to set timezone_id= $timezone_id_entry";
40-
if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed");
40+
try {
41+
ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry );
42+
} catch (IntlException $e) {
43+
echo $e->getMessage() . PHP_EOL;
44+
}
4145
$timezone_id = ut_datefmt_get_timezone_id( $fmt );
4246
$res_str .= "\nAfter call to set_timezone_id : timezone_id= $timezone_id";
4347
$formatted = ut_datefmt_format( $fmt, 0);
@@ -58,9 +62,8 @@ include_once( 'ut_common.inc' );
5862
ut_run();
5963
?>
6064
--EXPECTF--
61-
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d
62-
63-
Warning: datefmt_set_timezone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d
65+
datefmt_set_timezone: No such time zone: 'CN'
66+
datefmt_set_timezone: No such time zone: 'CN'
6467

6568
After creation of the dateformatter : timezone_id= US/Pacific
6669
-----------

ext/intl/tests/dateformat_set_timezone_id_icu72-1.phpt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ function ut_main()
3737

3838
$res_str .= "-----------";
3939
$res_str .= "\nTrying to set timezone_id= $timezone_id_entry";
40-
if (ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry ) !== $result) die("ut_datefmt_set_timezone_id failed");
40+
try {
41+
ut_datefmt_set_timezone_id( $fmt , $timezone_id_entry );
42+
} catch (IntlException $e) {
43+
echo $e->getMessage() . PHP_EOL;
44+
}
4145
$timezone_id = ut_datefmt_get_timezone_id( $fmt );
4246
$res_str .= "\nAfter call to set_timezone_id : timezone_id= $timezone_id";
4347
$formatted = ut_datefmt_format( $fmt, 0);
@@ -58,9 +62,8 @@ include_once( 'ut_common.inc' );
5862
ut_run();
5963
?>
6064
--EXPECTF--
61-
Warning: IntlDateFormatter::setTimeZone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d
62-
63-
Warning: datefmt_set_timezone(): datefmt_set_timezone: No such time zone: 'CN' in %sut_common.inc on line %d
65+
datefmt_set_timezone: No such time zone: 'CN'
66+
datefmt_set_timezone: No such time zone: 'CN'
6467

6568
After creation of the dateformatter : timezone_id= US/Pacific
6669
-----------

ext/intl/timezone/timezone_class.cpp

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <unicode/timezone.h>
2323
#include <unicode/calendar.h>
2424
#include "../intl_convertcpp.h"
25+
#include "../intl_common.h"
2526

2627
#include "../common/common_date.h"
2728

@@ -147,24 +148,15 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
147148
instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr)) {
148149
TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone);
149150

150-
/* TODO Throw proper Error exceptions for uninitialized classes and failure to clone */
151151
if (to->utimezone == NULL) {
152-
spprintf(&message, 0, "%s: passed IntlTimeZone is not "
152+
zend_throw_error(IntlException_ce_ptr, "%s: passed IntlTimeZone is not "
153153
"properly constructed", func);
154-
if (message) {
155-
intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
156-
efree(message);
157-
}
158154
zval_ptr_dtor_str(&local_zv_tz);
159155
return NULL;
160156
}
161157
timeZone = to->utimezone->clone();
162158
if (UNEXPECTED(timeZone == NULL)) {
163-
spprintf(&message, 0, "%s: could not clone TimeZone", func);
164-
if (message) {
165-
intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
166-
efree(message);
167-
}
159+
zend_throw_error(IntlException_ce_ptr, "%s: could not clone TimeZone", func);
168160
zval_ptr_dtor_str(&local_zv_tz);
169161
return NULL;
170162
}
@@ -185,32 +177,20 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone,
185177
}
186178
if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone),
187179
&status) == FAILURE) {
188-
spprintf(&message, 0, "%s: Time zone identifier given is not a "
180+
zend_throw_error(IntlException_ce_ptr, "%s: Time zone identifier given is not a "
189181
"valid UTF-8 string", func);
190-
if (message) {
191-
intl_errors_set(outside_error, status, message, 1);
192-
efree(message);
193-
}
194182
zval_ptr_dtor_str(&local_zv_tz);
195183
return NULL;
196184
}
197185
timeZone = TimeZone::createTimeZone(id);
198186
if (UNEXPECTED(timeZone == NULL)) {
199-
spprintf(&message, 0, "%s: Could not create time zone", func);
200-
if (message) {
201-
intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1);
202-
efree(message);
203-
}
187+
zend_throw_error(IntlException_ce_ptr, "%s: Could not create time zone", func);
204188
zval_ptr_dtor_str(&local_zv_tz);
205189
return NULL;
206190
}
207191
if (*timeZone == TimeZone::getUnknown()) {
208-
spprintf(&message, 0, "%s: No such time zone: '%s'",
192+
zend_throw_error(IntlException_ce_ptr, "%s: No such time zone: '%s'",
209193
func, Z_STRVAL_P(zv_timezone));
210-
if (message) {
211-
intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1);
212-
efree(message);
213-
}
214194
zval_ptr_dtor_str(&local_zv_tz);
215195
delete timeZone;
216196
return NULL;

0 commit comments

Comments
 (0)