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
master
韩天峰 3 weeks ago
parent 1a2a49addb
commit 6c0c26458f
  1. 5
      phpunit/code/datetime-direct-calls.php
  2. 7
      phpunit/src/DatetimeOptimizerTest.php
  3. 2
      src/Optimizer/FuncCallOptimizer.php
  4. 10
      tests/compiler/stdlib/strtotime_epoch.phpt

@ -8,3 +8,8 @@ function datetimeDirectCalls(int $timestamp): string
. date('U', $timestamp) . date('U', $timestamp)
. $now; . $now;
} }
function strtotimeNormalCall(string $datetime): int|false
{
return strtotime($datetime);
}

@ -4,7 +4,7 @@ use TypePhp\CompilerTest;
final class DatetimeOptimizerTest extends BaseTest final class DatetimeOptimizerTest extends BaseTest
{ {
public function testCoreDatetimeCallsUseDirectPhpxWrappers(): void public function testOnlySupportedDatetimeCallsUseDirectPhpxWrappers(): void
{ {
global $translator; global $translator;
@ -20,7 +20,8 @@ final class DatetimeOptimizerTest extends BaseTest
self::assertSame(1, substr_count($code, 'php::fn::time(')); self::assertSame(1, substr_count($code, 'php::fn::time('));
self::assertSame(2, substr_count($code, 'php::fn::date(')); self::assertSame(2, substr_count($code, 'php::fn::date('));
self::assertSame(1, substr_count($code, 'php::fn::gmdate(')); self::assertSame(1, substr_count($code, 'php::fn::gmdate('));
self::assertStringNotContainsString('get_persistent_func', $code); self::assertStringNotContainsString('php::fn::strtotime(', $code);
self::assertStringNotContainsString('php::call(', $code); self::assertSame(1, substr_count($code, 'get_persistent_func'));
self::assertSame(1, substr_count($code, 'php::call('));
} }
} }

@ -83,7 +83,7 @@ trait FuncCallOptimizer
'is_scalar', 'is_numeric', 'is_countable', 'is_iterable', 'is_scalar', 'is_numeric', 'is_countable', 'is_iterable',
'array_is_list', 'is_dir', 'is_file', 'file_exists', 'realpath', 'array_is_list', 'is_dir', 'is_file', 'file_exists', 'realpath',
'in_array', 'array_search', 'in_array', 'array_search',
'strtotime', 'md5', 'sha1', 'hash', 'print_r', 'md5', 'sha1', 'hash', 'print_r',
'base64_encode', 'base64_decode', 'base64_encode', 'base64_decode',
'urlencode', 'urldecode', 'rawurlencode', 'rawurldecode', 'urlencode', 'urldecode', 'rawurlencode', 'rawurldecode',
'json_encode', 'json_decode', 'serialize', 'unserialize', 'json_encode', 'json_decode', 'serialize', 'unserialize',

@ -1,11 +1,17 @@
--TEST-- --TEST--
strtotime: epoch 0 and invalid date strtotime: absolute, relative, epoch 0 and invalid dates
--FILE-- --FILE--
<?php <?php
date_default_timezone_set("UTC");
var_dump(strtotime("@0")); var_dump(strtotime("@0"));
var_dump(strtotime("1970-01-01 00:00:00 UTC")); var_dump(strtotime("1970-01-01 00:00:00 UTC"));
var_dump(strtotime("1970-01-02 00:00:00 UTC")); var_dump(strtotime("1970-01-02 00:00:00 UTC"));
var_dump(strtotime("2000-01-01 00:00:00 UTC")); var_dump(strtotime("2000-01-01 00:00:00 UTC"));
var_dump(strtotime("-1 day", 1609459200));
$before = time();
$relative = strtotime("-1 day");
$after = time();
var_dump($relative >= $before - 86401 && $relative <= $after - 86399);
var_dump(strtotime("invalid-date-string")); var_dump(strtotime("invalid-date-string"));
?> ?>
--EXPECT-- --EXPECT--
@ -13,4 +19,6 @@ int(0)
int(0) int(0)
int(86400) int(86400)
int(946684800) int(946684800)
int(1609372800)
bool(true)
bool(false) bool(false)

Loading…
Cancel
Save