From 6c0c26458fa5c51dfa9d2f30d4244fa897243521 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 21 Sep 2026 12:23:02 +0800 Subject: [PATCH] refactor(optimizer): remove strtotime from direct wrapper optimization - Removed strtotime from the list of functions that use direct PHPX wrappers - Updated test case to verify only supported datetime calls use direct wrappers - Modified strtotime test to include absolute, relative, epoch 0 and invalid dates - Added timezone setting and additional test cases for relative date handling - Updated expected test output to reflect new strtotime behavior - Adjusted assertions to check for correct persistent function calls --- phpunit/code/datetime-direct-calls.php | 5 +++++ phpunit/src/DatetimeOptimizerTest.php | 7 ++++--- src/Optimizer/FuncCallOptimizer.php | 2 +- tests/compiler/stdlib/strtotime_epoch.phpt | 10 +++++++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/phpunit/code/datetime-direct-calls.php b/phpunit/code/datetime-direct-calls.php index 6785d49a..8e8c195a 100644 --- a/phpunit/code/datetime-direct-calls.php +++ b/phpunit/code/datetime-direct-calls.php @@ -8,3 +8,8 @@ function datetimeDirectCalls(int $timestamp): string . date('U', $timestamp) . $now; } + +function strtotimeNormalCall(string $datetime): int|false +{ + return strtotime($datetime); +} diff --git a/phpunit/src/DatetimeOptimizerTest.php b/phpunit/src/DatetimeOptimizerTest.php index 1c2a52c8..7a572c6d 100644 --- a/phpunit/src/DatetimeOptimizerTest.php +++ b/phpunit/src/DatetimeOptimizerTest.php @@ -4,7 +4,7 @@ use TypePhp\CompilerTest; final class DatetimeOptimizerTest extends BaseTest { - public function testCoreDatetimeCallsUseDirectPhpxWrappers(): void + public function testOnlySupportedDatetimeCallsUseDirectPhpxWrappers(): void { global $translator; @@ -20,7 +20,8 @@ final class DatetimeOptimizerTest extends BaseTest self::assertSame(1, substr_count($code, 'php::fn::time(')); self::assertSame(2, substr_count($code, 'php::fn::date(')); self::assertSame(1, substr_count($code, 'php::fn::gmdate(')); - self::assertStringNotContainsString('get_persistent_func', $code); - self::assertStringNotContainsString('php::call(', $code); + self::assertStringNotContainsString('php::fn::strtotime(', $code); + self::assertSame(1, substr_count($code, 'get_persistent_func')); + self::assertSame(1, substr_count($code, 'php::call(')); } } diff --git a/src/Optimizer/FuncCallOptimizer.php b/src/Optimizer/FuncCallOptimizer.php index bce35f93..26ebaf58 100644 --- a/src/Optimizer/FuncCallOptimizer.php +++ b/src/Optimizer/FuncCallOptimizer.php @@ -83,7 +83,7 @@ trait FuncCallOptimizer 'is_scalar', 'is_numeric', 'is_countable', 'is_iterable', 'array_is_list', 'is_dir', 'is_file', 'file_exists', 'realpath', 'in_array', 'array_search', - 'strtotime', 'md5', 'sha1', 'hash', 'print_r', + 'md5', 'sha1', 'hash', 'print_r', 'base64_encode', 'base64_decode', 'urlencode', 'urldecode', 'rawurlencode', 'rawurldecode', 'json_encode', 'json_decode', 'serialize', 'unserialize', diff --git a/tests/compiler/stdlib/strtotime_epoch.phpt b/tests/compiler/stdlib/strtotime_epoch.phpt index 9bd2509d..eed4ed0b 100644 --- a/tests/compiler/stdlib/strtotime_epoch.phpt +++ b/tests/compiler/stdlib/strtotime_epoch.phpt @@ -1,11 +1,17 @@ --TEST-- -strtotime: epoch 0 and invalid date +strtotime: absolute, relative, epoch 0 and invalid dates --FILE-- = $before - 86401 && $relative <= $after - 86399); var_dump(strtotime("invalid-date-string")); ?> --EXPECT-- @@ -13,4 +19,6 @@ int(0) int(0) int(86400) int(946684800) +int(1609372800) +bool(true) bool(false)