From a4d28fbe6be01e039b2b9f633b26cd2b2889d53d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 1 Jun 2026 18:49:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(tests):=20=E6=9B=BF=E6=8D=A2=20objval?= =?UTF-8?q?=20=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E4=B8=BA=20toObject=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除了全局 objval 函数实现 - 将所有测试文件中的 objval($obj, Class::class) 替换为 $obj->toObject(Class::class) - 更新了 std-vector、std-map、std-unordered-map、std-array 和 type_decl 测试 - 添加了新的 var_convert 链式调用测试用例 - 统一了对象类型转换的调用方式 --- src/polyfills.php | 7 ------- tests/aot/basic/objval.phpt | 4 ++-- tests/aot/std-array/005.phpt | 2 +- tests/aot/std-map/003.phpt | 2 +- tests/aot/std-unordered-map/003.phpt | 2 +- tests/aot/std-vector/002.phpt | 2 +- tests/aot/type_decl/007.phpt | 2 +- tests/aot/var_convert/001.phpt | 19 +++++++++++++++++++ 8 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 tests/aot/var_convert/001.phpt diff --git a/src/polyfills.php b/src/polyfills.php index a7dac262..4a37bacd 100644 --- a/src/polyfills.php +++ b/src/polyfills.php @@ -83,13 +83,6 @@ class std } } -function objval(mixed $obj, string $class): object -{ - if ($obj instanceof $class) { - return $obj; - } - throw new Exception('Invalid object type'); -} function &refval(&$var) { diff --git a/tests/aot/basic/objval.phpt b/tests/aot/basic/objval.phpt index 9e4ddd8f..6571dc60 100644 --- a/tests/aot/basic/objval.phpt +++ b/tests/aot/basic/objval.phpt @@ -25,10 +25,10 @@ function main() $obj = new TestObjval(1, 2); $arr = array(); $arr['obj'] = $obj; - $obj2 = objval($arr['obj'], 'TestObjval'); + $obj2 = $arr['obj']->toObject('TestObjval'); var_dump($obj2->test()); - $obj3 = objval($obj, TestObjval::class); + $obj3 = $obj->toObject(TestObjval::class); var_dump($obj3->test()); } ?> diff --git a/tests/aot/std-array/005.phpt b/tests/aot/std-array/005.phpt index 5610b572..5fd277a6 100644 --- a/tests/aot/std-array/005.phpt +++ b/tests/aot/std-array/005.phpt @@ -28,7 +28,7 @@ function main() { $objects = std::array(complex_types::type_object, 2); $objects[0] = new StdArrayComplexValue(28); var_dump($objects[0] instanceof StdArrayComplexValue); - $object = objval($objects[0], StdArrayComplexValue::class); + $object = $objects[0]->toObject(StdArrayComplexValue::class); var_dump($object->getValue()); $variants = std::array(complex_types::type_any, 2); diff --git a/tests/aot/std-map/003.phpt b/tests/aot/std-map/003.phpt index d124a6d1..3eff8aa2 100644 --- a/tests/aot/std-map/003.phpt +++ b/tests/aot/std-map/003.phpt @@ -28,7 +28,7 @@ function main() { $objects = std::map(complex_types::type_string, complex_types::type_object); $objects["item"] = new StdMapComplexValue(14); var_dump($objects["item"] instanceof StdMapComplexValue); - $object = objval($objects["item"], StdMapComplexValue::class); + $object = $objects["item"]->toObject(StdMapComplexValue::class); var_dump($object->getValue()); $variants = std::map(native_types::type_int, complex_types::type_any); diff --git a/tests/aot/std-unordered-map/003.phpt b/tests/aot/std-unordered-map/003.phpt index 12625f7e..1c9abed7 100644 --- a/tests/aot/std-unordered-map/003.phpt +++ b/tests/aot/std-unordered-map/003.phpt @@ -28,7 +28,7 @@ function main() { $objects = std::unordered_map(native_types::type_int, complex_types::type_object); $objects[2] = new StdUnorderedMapComplexValue(21); var_dump($objects[2] instanceof StdUnorderedMapComplexValue); - $object = objval($objects[2], StdUnorderedMapComplexValue::class); + $object = $objects[2]->toObject(StdUnorderedMapComplexValue::class); var_dump($object->getValue()); $variants = std::unordered_map(native_types::type_int, complex_types::type_var); diff --git a/tests/aot/std-vector/002.phpt b/tests/aot/std-vector/002.phpt index 71c335b7..6ed93d6d 100644 --- a/tests/aot/std-vector/002.phpt +++ b/tests/aot/std-vector/002.phpt @@ -28,7 +28,7 @@ function main() { $objects = std::vector(complex_types::type_object); $objects[] = new StdVectorComplexValue(7); var_dump($objects[0] instanceof StdVectorComplexValue); - $object = objval($objects[0], StdVectorComplexValue::class); + $object = $objects[0]->toObject(StdVectorComplexValue::class); var_dump($object->getValue()); $variants = std::vector(complex_types::type_variant); diff --git a/tests/aot/type_decl/007.phpt b/tests/aot/type_decl/007.phpt index e6f5a9d8..fb96b2e7 100644 --- a/tests/aot/type_decl/007.phpt +++ b/tests/aot/type_decl/007.phpt @@ -18,7 +18,7 @@ namespace TestApp\Command { public function callCommand(string $class): Command { $array = ['object' => new Command($class)]; - return objval($array['object'], Command::class); + return $array['object']->toObject(Command::class); } } } diff --git a/tests/aot/var_convert/001.phpt b/tests/aot/var_convert/001.phpt new file mode 100644 index 00000000..5f5893f4 --- /dev/null +++ b/tests/aot/var_convert/001.phpt @@ -0,0 +1,19 @@ +--TEST-- +var convert chained on array element +--FILE-- +toString()); + var_dump($arr["name"]->toString()); +} +?> +--EXPECT-- +string(2) "20" +string(5) "hello"