Skip to content

Commit 43a9108

Browse files
authored
ext/intl: Refactor timezone parameter passing (#19409)
1 parent be28894 commit 43a9108

14 files changed

+205
-143
lines changed

ext/intl/calendar/calendar.stub.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ class IntlCalendar
9090
private function __construct() {}
9191

9292
/**
93-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
9493
* @tentative-return-type
9594
* @alias intlcal_create_instance
9695
*/
97-
public static function createInstance($timezone = null, ?string $locale = null): ?IntlCalendar {}
96+
public static function createInstance(IntlTimeZone|DateTimeZone|string|null $timezone = null, ?string $locale = null): ?IntlCalendar {}
9897

9998
/**
10099
* @tentative-return-type
@@ -355,11 +354,9 @@ public function setSkippedWallTimeOption(int $option): true {}
355354
public function setTime(float $timestamp): bool {}
356355

357356
/**
358-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
359357
* @tentative-return-type
360-
* @alias intlcal_set_time_zone
361358
*/
362-
public function setTimeZone($timezone): bool {}
359+
public function setTimeZone(IntlTimeZone|DateTimeZone|string|null $timezone): bool {}
363360

364361
/**
365362
* @tentative-return-type

ext/intl/calendar/calendar_arginfo.h

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/calendar/calendar_methods.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ U_CFUNC PHP_METHOD(IntlCalendar, __construct)
7272

7373
U_CFUNC PHP_FUNCTION(intlcal_create_instance)
7474
{
75-
zval *zv_timezone = NULL;
75+
zend_object *timezone_object = nullptr;
76+
zend_string *timezone_string = nullptr;
7677
char *locale_str = NULL;
7778
size_t locale_len = 0;
78-
TimeZone *timeZone;
7979
UErrorCode status = U_ZERO_ERROR;
8080
intl_error_reset(NULL);
8181

8282
ZEND_PARSE_PARAMETERS_START(0, 2)
8383
Z_PARAM_OPTIONAL
84-
Z_PARAM_ZVAL(zv_timezone)
84+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
8585
Z_PARAM_STRING_OR_NULL(locale_str, locale_len)
8686
ZEND_PARSE_PARAMETERS_END();
8787

88-
timeZone = timezone_process_timezone_argument(zv_timezone, NULL);
89-
if (timeZone == NULL) {
88+
TimeZone *timeZone = timezone_process_timezone_argument(timezone_object, timezone_string, nullptr);
89+
if (timeZone == nullptr) {
9090
RETURN_NULL();
9191
}
9292

@@ -296,26 +296,28 @@ U_CFUNC PHP_FUNCTION(intlcal_add)
296296
RETURN_TRUE;
297297
}
298298

299+
/* {{{ Set formatter's timezone. */
299300
U_CFUNC PHP_FUNCTION(intlcal_set_time_zone)
300301
{
301-
zval *zv_timezone;
302-
TimeZone *timeZone;
302+
zend_object *timezone_object = nullptr;
303+
zend_string *timezone_string = nullptr;
304+
303305
CALENDAR_METHOD_INIT_VARS;
304306

305-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
306-
"Oz!", &object, Calendar_ce_ptr, &zv_timezone) == FAILURE) {
307-
RETURN_THROWS();
308-
}
307+
ZEND_PARSE_PARAMETERS_START(2, 2)
308+
Z_PARAM_OBJECT_OF_CLASS(object, Calendar_ce_ptr)
309+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
310+
ZEND_PARSE_PARAMETERS_END();
309311

310312
CALENDAR_METHOD_FETCH_OBJECT;
311313

312-
if (zv_timezone == NULL) {
314+
if (timezone_object == nullptr && timezone_string == nullptr) {
313315
RETURN_TRUE; /* the method does nothing if passed null */
314316
}
315317

316-
timeZone = timezone_process_timezone_argument(zv_timezone,
317-
CALENDAR_ERROR_P(co));
318-
if (timeZone == NULL) {
318+
TimeZone *timeZone = timezone_process_timezone_argument(
319+
timezone_object, timezone_string, CALENDAR_ERROR_P(co));
320+
if (timeZone == nullptr) {
319321
RETURN_FALSE;
320322
}
321323

@@ -324,6 +326,34 @@ U_CFUNC PHP_FUNCTION(intlcal_set_time_zone)
324326
RETURN_TRUE;
325327
}
326328

329+
U_CFUNC PHP_METHOD(IntlCalendar, setTimeZone)
330+
{
331+
zend_object *timezone_object = nullptr;
332+
zend_string *timezone_string = nullptr;
333+
334+
CALENDAR_METHOD_INIT_VARS;
335+
336+
ZEND_PARSE_PARAMETERS_START(1, 1)
337+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
338+
ZEND_PARSE_PARAMETERS_END();
339+
340+
object = ZEND_THIS;
341+
CALENDAR_METHOD_FETCH_OBJECT;
342+
343+
if (timezone_object == nullptr && timezone_string == nullptr) {
344+
RETURN_TRUE; /* the method does nothing if passed null */
345+
}
346+
347+
TimeZone *timeZone = timezone_process_timezone_argument(
348+
timezone_object, timezone_string, CALENDAR_ERROR_P(co));
349+
if (timeZone == nullptr) {
350+
RETURN_FALSE;
351+
}
352+
353+
co->ucal->adoptTimeZone(timeZone);
354+
355+
RETURN_TRUE;
356+
}
327357

328358
static void _php_intlcal_before_after(
329359
UBool (Calendar::*func)(const Calendar&, UErrorCode&) const,

ext/intl/calendar/gregoriancalendar_methods.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static bool set_gregorian_calendar_time_zone(GregorianCalendar *gcal, UErrorCode
7777

7878
static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
7979
{
80-
zval *tz_object = NULL;
80+
zend_object *timezone_object = nullptr;
81+
zend_string *timezone_string = nullptr;
8182
zval args_a[6],
8283
*args = &args_a[0];
8384
char *locale = NULL;
@@ -112,15 +113,23 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool
112113

113114
// argument parsing
114115
if (variant <= 2) {
115-
if (zend_parse_parameters(MIN(ZEND_NUM_ARGS(), 2),
116-
"|z!s!", &tz_object, &locale, &locale_len) == FAILURE) {
117-
RETURN_THROWS();
118-
}
119-
}
120-
if (variant > 2 && zend_parse_parameters(ZEND_NUM_ARGS(),
121-
"lll|lll", &largs[0], &largs[1], &largs[2], &largs[3], &largs[4],
122-
&largs[5]) == FAILURE) {
123-
RETURN_THROWS();
116+
/* These dummy variables are needed because the 2 param constructor allows trailing nulls... */
117+
zval *dummy1, *dummy2, *dummy3, *dummy4;
118+
ZEND_PARSE_PARAMETERS_START(0, 6)
119+
Z_PARAM_OPTIONAL
120+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
121+
Z_PARAM_STRING_OR_NULL(locale, locale_len)
122+
Z_PARAM_ZVAL(dummy1)
123+
Z_PARAM_ZVAL(dummy2)
124+
Z_PARAM_ZVAL(dummy3)
125+
Z_PARAM_ZVAL(dummy4)
126+
ZEND_PARSE_PARAMETERS_END();
127+
}
128+
if (variant > 2
129+
&& zend_parse_parameters(ZEND_NUM_ARGS(), "lll|lll",
130+
&largs[0], &largs[1], &largs[2], &largs[3], &largs[4], &largs[5]) == FAILURE
131+
) {
132+
RETURN_THROWS();
124133
}
125134

126135
// instantion of ICU object
@@ -134,8 +143,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool
134143

135144
if (variant <= 2) {
136145
// From timezone and locale (0 to 2 arguments)
137-
TimeZone *tz = timezone_process_timezone_argument(tz_object, NULL);
138-
if (tz == NULL) {
146+
TimeZone *tz = timezone_process_timezone_argument(timezone_object, timezone_string, nullptr);
147+
if (tz == nullptr) {
139148
// TODO: Exception should always occur already?
140149
if (!EG(exception)) {
141150
zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);

ext/intl/dateformat/dateformat.stub.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,26 @@ class IntlDateFormatter
3232
public const int TRADITIONAL = UNKNOWN;
3333

3434
/**
35-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
3635
* @param IntlCalendar|int|null $calendar
3736
*/
3837
public function __construct(
3938
?string $locale,
4039
int $dateType = IntlDateFormatter::FULL,
4140
int $timeType = IntlDateFormatter::FULL,
42-
$timezone = null,
41+
IntlTimeZone|DateTimeZone|string|null $timezone = null,
4342
$calendar = null,
4443
?string $pattern = null
4544
) {}
4645

4746
/**
48-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
4947
* @tentative-return-type
5048
* @alias datefmt_create
5149
*/
5250
public static function create(
5351
?string $locale,
5452
int $dateType = IntlDateFormatter::FULL,
5553
int $timeType = IntlDateFormatter::FULL,
56-
$timezone = null,
54+
IntlTimeZone|DateTimeZone|string|null $timezone = null,
5755
IntlCalendar|int|null $calendar = null,
5856
?string $pattern = null
5957
): ?IntlDateFormatter {}
@@ -101,11 +99,9 @@ public function getCalendarObject(): IntlCalendar|false|null {}
10199
public function getTimeZone(): IntlTimeZone|false {}
102100

103101
/**
104-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
105102
* @tentative-return-type
106-
* @alias datefmt_set_timezone
107103
*/
108-
public function setTimeZone($timezone): bool {}
104+
public function setTimeZone(IntlTimeZone|DateTimeZone|string|null $timezone): bool {}
109105

110106
/**
111107
* @tentative-return-type

ext/intl/dateformat/dateformat_arginfo.h

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/dateformat/dateformat_attrcpp.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,46 @@ U_CFUNC PHP_FUNCTION(datefmt_get_timezone)
8181
/* {{{ Set formatter's timezone. */
8282
U_CFUNC PHP_FUNCTION(datefmt_set_timezone)
8383
{
84-
zval *timezone_zv;
85-
TimeZone *timezone;
84+
zend_object *timezone_object = nullptr;
85+
zend_string *timezone_string = nullptr;
8686

8787
DATE_FORMAT_METHOD_INIT_VARS;
8888

89-
if ( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
90-
"Oz", &object, IntlDateFormatter_ce_ptr, &timezone_zv) == FAILURE) {
91-
RETURN_THROWS();
89+
ZEND_PARSE_PARAMETERS_START(2, 2)
90+
Z_PARAM_OBJECT_OF_CLASS(object, IntlDateFormatter_ce_ptr)
91+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
92+
ZEND_PARSE_PARAMETERS_END();
93+
94+
DATE_FORMAT_METHOD_FETCH_OBJECT;
95+
96+
TimeZone *timezone = timezone_process_timezone_argument(
97+
timezone_object, timezone_string, INTL_DATA_ERROR_P(dfo));
98+
if (timezone == nullptr) {
99+
RETURN_FALSE;
92100
}
93101

102+
fetch_datefmt(dfo)->adoptTimeZone(timezone);
103+
104+
RETURN_TRUE;
105+
}
106+
107+
U_CFUNC PHP_METHOD(IntlDateFormatter, setTimeZone)
108+
{
109+
zend_object *timezone_object = nullptr;
110+
zend_string *timezone_string = nullptr;
111+
112+
DATE_FORMAT_METHOD_INIT_VARS;
113+
114+
ZEND_PARSE_PARAMETERS_START(1, 1)
115+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
116+
ZEND_PARSE_PARAMETERS_END();
117+
118+
object = ZEND_THIS;
94119
DATE_FORMAT_METHOD_FETCH_OBJECT;
95120

96-
timezone = timezone_process_timezone_argument(timezone_zv,
97-
INTL_DATA_ERROR_P(dfo));
98-
if (timezone == NULL) {
121+
TimeZone *timezone = timezone_process_timezone_argument(
122+
timezone_object, timezone_string, INTL_DATA_ERROR_P(dfo));
123+
if (timezone == nullptr) {
99124
RETURN_FALSE;
100125
}
101126

0 commit comments

Comments
 (0)