Skip to content

Commit 6fd3383

Browse files
author
Ion Bazan
committed
fixed #30
1 parent d722041 commit 6fd3383

File tree

4 files changed

+128
-51
lines changed

4 files changed

+128
-51
lines changed

tests/issue_005.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Check for issue #5 (DateTime reflection)
3+
--SKIPIF--
4+
<?php
5+
$required_version = "5.3";
6+
$required_func = array("timecop_freeze");
7+
$required_class = array("TimecopDateTime");
8+
include(__DIR__."/tests-skipcheck.inc.php");
9+
--INI--
10+
date.timezone=GMT
11+
timecop.func_override=1
12+
--FILE--
13+
<?php
14+
class Test
15+
{
16+
public function setCreatedAt(DateTime $createdAt)
17+
{
18+
$this->createdAt = $createdAt;
19+
}
20+
}
21+
22+
$test = new Test();
23+
24+
$reflection = new ReflectionClass($test);
25+
$params = $reflection->getMethod('setCreatedAt')->getParameters();
26+
27+
foreach ($params as $param) {
28+
var_dump($param->getClass());
29+
}
30+
--EXPECT--
31+
object(ReflectionClass)#3 (1) {
32+
["name"]=>
33+
string(8) "DateTime"
34+
}

tests/issue_030.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Check for issue #30 (DateTimeImmutable::createFromFormat with ! returns DateTime)
3+
--SKIPIF--
4+
<?php
5+
$required_version = "5.5";
6+
$required_func = array("timecop_freeze");
7+
$required_class = array("TimecopDateTimeImmutable");
8+
include(__DIR__."/tests-skipcheck.inc.php");
9+
--INI--
10+
date.timezone=GMT
11+
timecop.func_override=1
12+
--FILE--
13+
<?php
14+
var_dump(\DateTimeImmutable::createFromFormat('!Y-m-d', '2017-10-03'));
15+
var_dump(\DateTimeImmutable::createFromFormat('Y-m-d!', '2017-10-03'));
16+
--EXPECT--
17+
object(DateTimeImmutable)#1 (3) {
18+
["date"]=>
19+
string(26) "2017-10-03 00:00:00.000000"
20+
["timezone_type"]=>
21+
int(3)
22+
["timezone"]=>
23+
string(3) "GMT"
24+
}
25+
object(DateTimeImmutable)#1 (3) {
26+
["date"]=>
27+
string(26) "1970-01-01 00:00:00.000000"
28+
["timezone_type"]=>
29+
int(3)
30+
["timezone"]=>
31+
string(3) "GMT"
32+
}
33+

timecop.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,8 +1925,13 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
19251925
INIT_ZVAL(orig_time);
19261926
ZVAL_STRINGL(&orig_time, orig_time_str, orig_time_len, 0);
19271927
#endif
1928+
if (immutable) {
1929+
real_func = ORIG_FUNC_NAME("date_create_immutable_from_format");
1930+
} else {
1931+
real_func = ORIG_FUNC_NAME("date_create_from_format");
1932+
}
19281933

1929-
call_php_function_with_3_params(ORIG_FUNC_NAME("date_create_from_format"), &dt, &orig_format, &orig_time, orig_timezone);
1934+
call_php_function_with_3_params(real_func, &dt, &orig_format, &orig_time, orig_timezone);
19301935

19311936
#if PHP_MAJOR_VERSION >= 7
19321937
if (Z_TYPE(dt) == IS_FALSE) {
@@ -1955,7 +1960,13 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
19551960
INIT_ZVAL(now_timestamp);
19561961
#endif
19571962
ZVAL_LONG(&now_timestamp, now.sec);
1958-
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "settimestamp", NULL, &now_timestamp);
1963+
1964+
if (immutable) {
1965+
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTimeImmutable), "settimestamp", &dt, &now_timestamp);
1966+
} else {
1967+
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "settimestamp", &dt, &now_timestamp);
1968+
}
1969+
19591970
sprintf(buf, "Y-m-d H:i:s.%06ld ", now.usec);
19601971

19611972
#if PHP_MAJOR_VERSION >= 7
@@ -1965,7 +1976,12 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
19651976
ZVAL_STRINGL(&tmp, buf, strlen(buf), 0);
19661977
#endif
19671978

1968-
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "format", &fixed_time, &tmp);
1979+
if (immutable) {
1980+
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTimeImmutable), "format", &fixed_time, &tmp);
1981+
} else {
1982+
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "format", &fixed_time, &tmp);
1983+
}
1984+
19691985
#if PHP_MAJOR_VERSION >= 7
19701986
zval_ptr_dtor(&tmp);
19711987
#else
@@ -2021,12 +2037,6 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
20212037
#else
20222038
call_php_function_with_3_params("sprintf", &new_time, &tmp, fixed_time, &orig_time);
20232039
#endif
2024-
2025-
if (immutable) {
2026-
real_func = ORIG_FUNC_NAME("date_create_immutable_from_format");
2027-
} else {
2028-
real_func = ORIG_FUNC_NAME("date_create_from_format");
2029-
}
20302040
#if PHP_MAJOR_VERSION >= 7
20312041
call_php_function_with_3_params(real_func, return_value, &new_format, &new_time, orig_timezone);
20322042
#else

timecop.h

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ SOFTWARE.
3636
#include "ext/standard/info.h"
3737

3838
#ifdef PHP_WIN32
39-
# define PHP_TIMECOP_API __declspec(dllexport)
39+
# define PHP_TIMECOP_API __declspec(dllexport)
4040
#elif defined(__GNUC__) && __GNUC__ >= 4
41-
# define PHP_TIMECOP_API __attribute__ ((visibility("default")))
41+
# define PHP_TIMECOP_API __attribute__ ((visibility("default")))
4242
#else
43-
# define PHP_TIMECOP_API
43+
# define PHP_TIMECOP_API
4444
#endif
4545

4646
#ifdef ZTS
@@ -109,30 +109,30 @@ PHP_METHOD(Timecop, freeze);
109109
PHP_METHOD(Timecop, travel);
110110

111111
typedef enum timecop_mode_t {
112-
TIMECOP_MODE_REALTIME,
113-
TIMECOP_MODE_FREEZE,
114-
TIMECOP_MODE_TRAVEL
112+
TIMECOP_MODE_REALTIME,
113+
TIMECOP_MODE_FREEZE,
114+
TIMECOP_MODE_TRAVEL
115115
} timecop_mode_t;
116116

117117
ZEND_BEGIN_MODULE_GLOBALS(timecop)
118-
long func_override;
119-
long sync_request_time;
118+
long func_override;
119+
long sync_request_time;
120120
#if PHP_MAJOR_VERSION >= 7
121-
zval orig_request_time;
121+
zval orig_request_time;
122122
#else
123-
zval *orig_request_time;
123+
zval *orig_request_time;
124124
#endif
125-
timecop_mode_t timecop_mode;
126-
tc_timeval freezed_time;
127-
tc_timeval travel_origin;
128-
tc_timeval travel_offset;
129-
zend_long scaling_factor;
130-
zend_class_entry *ce_DateTimeZone;
131-
zend_class_entry *ce_DateTimeInterface;
132-
zend_class_entry *ce_DateTime;
133-
zend_class_entry *ce_TimecopDateTime;
134-
zend_class_entry *ce_DateTimeImmutable;
135-
zend_class_entry *ce_TimecopDateTimeImmutable;
125+
timecop_mode_t timecop_mode;
126+
tc_timeval freezed_time;
127+
tc_timeval travel_origin;
128+
tc_timeval travel_offset;
129+
zend_long scaling_factor;
130+
zend_class_entry *ce_DateTimeZone;
131+
zend_class_entry *ce_DateTimeInterface;
132+
zend_class_entry *ce_DateTime;
133+
zend_class_entry *ce_TimecopDateTime;
134+
zend_class_entry *ce_DateTimeImmutable;
135+
zend_class_entry *ce_TimecopDateTimeImmutable;
136136
ZEND_END_MODULE_GLOBALS(timecop)
137137

138138
#if ZEND_DEBUG
@@ -147,23 +147,23 @@ ZEND_END_MODULE_GLOBALS(timecop)
147147
#define OVRD_CLASS_PREFIX "timecop"
148148

149149
#define ORIG_FUNC_NAME(fname) \
150-
(TIMECOP_G(func_override) ? (SAVE_FUNC_PREFIX fname) : fname)
150+
(TIMECOP_G(func_override) ? (SAVE_FUNC_PREFIX fname) : fname)
151151

152152
#define TIMECOP_OFE(fname) {fname, OVRD_FUNC_PREFIX fname, SAVE_FUNC_PREFIX fname}
153153
#define TIMECOP_OCE(cname, mname) \
154-
{cname, mname, OVRD_CLASS_PREFIX cname, SAVE_FUNC_PREFIX mname}
154+
{cname, mname, OVRD_CLASS_PREFIX cname, SAVE_FUNC_PREFIX mname}
155155

156156
struct timecop_override_func_entry {
157-
char *orig_func;
158-
char *ovrd_func;
159-
char *save_func;
157+
char *orig_func;
158+
char *ovrd_func;
159+
char *save_func;
160160
};
161161

162162
struct timecop_override_class_entry {
163-
char *orig_class;
164-
char *orig_method;
165-
char *ovrd_class;
166-
char *save_method;
163+
char *orig_class;
164+
char *orig_method;
165+
char *ovrd_class;
166+
char *save_method;
167167
};
168168

169169
static void timecop_globals_ctor(zend_timecop_globals *globals TSRMLS_DC);
@@ -226,35 +226,35 @@ static inline void _call_php_function_with_params(const char *function_name, zva
226226

227227
#if PHP_MAJOR_VERSION >= 7
228228
#define register_internal_class_ex(class_entry, parent_ce) \
229-
zend_register_internal_class_ex(class_entry, parent_ce)
229+
zend_register_internal_class_ex(class_entry, parent_ce)
230230
#else
231231
#define register_internal_class_ex(class_entry, parent_ce) \
232-
zend_register_internal_class_ex(class_entry, parent_ce, NULL TSRMLS_CC)
232+
zend_register_internal_class_ex(class_entry, parent_ce, NULL TSRMLS_CC)
233233
#endif
234234

235235
#define call_php_method_with_0_params(obj, ce, method_name, retval) \
236-
_call_php_method_with_0_params(obj, ce, method_name, retval TSRMLS_CC)
236+
_call_php_method_with_0_params(obj, ce, method_name, retval TSRMLS_CC)
237237

238-
#define call_php_method_with_1_params(obj, ce, method_name, retval, arg1) \
239-
_call_php_method_with_1_params(obj, ce, method_name, retval, arg1 TSRMLS_CC)
238+
#define call_php_method_with_1_params(obj, ce, method_name, retval, arg1) \
239+
_call_php_method_with_1_params(obj, ce, method_name, retval, arg1 TSRMLS_CC)
240240

241241
#define call_php_method_with_2_params(obj, ce, method_name, retval, arg1, arg2) \
242-
_call_php_method_with_2_params(obj, ce, method_name, retval, arg1, arg2 TSRMLS_CC)
242+
_call_php_method_with_2_params(obj, ce, method_name, retval, arg1, arg2 TSRMLS_CC)
243243

244244
#define call_php_function_with_0_params(function_name, retval) \
245-
_call_php_function_with_0_params(function_name, retval TSRMLS_CC)
245+
_call_php_function_with_0_params(function_name, retval TSRMLS_CC)
246246

247247
#define call_php_function_with_1_params(function_name, retval, arg1) \
248-
_call_php_function_with_1_params(function_name, retval, arg1 TSRMLS_CC)
248+
_call_php_function_with_1_params(function_name, retval, arg1 TSRMLS_CC)
249249

250250
#define call_php_function_with_2_params(function_name, retval, arg1, arg2) \
251-
_call_php_function_with_2_params(function_name, retval, arg1, arg2 TSRMLS_CC)
251+
_call_php_function_with_2_params(function_name, retval, arg1, arg2 TSRMLS_CC)
252252

253253
#define call_php_function_with_3_params(function_name, retval, arg1, arg2, arg3) \
254-
_call_php_function_with_3_params(function_name, retval, arg1, arg2, arg3 TSRMLS_CC)
254+
_call_php_function_with_3_params(function_name, retval, arg1, arg2, arg3 TSRMLS_CC)
255255

256256
#define call_php_function_with_params(function_name, retval, param_count, params) \
257-
_call_php_function_with_params(function_name, retval, param_count, params TSRMLS_CC)
257+
_call_php_function_with_params(function_name, retval, param_count, params TSRMLS_CC)
258258

259259
/* In every utility function you add that needs to use variables
260260
in php_timecop_globals, call TSRMLS_FETCH(); after declaring other
@@ -279,7 +279,7 @@ static inline void _call_php_function_with_params(const char *function_name, zva
279279
# endif
280280
#endif
281281

282-
#endif /* PHP_TIMECOP_H */
282+
#endif /* PHP_TIMECOP_H */
283283

284284
/*
285285
* Local variables:

0 commit comments

Comments
 (0)