From bf4c5a785249d7f641038a0008da9849c5567ee5 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 29 May 2026 09:53:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(generator):=20=E8=A7=A3=E5=86=B3=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E5=80=BC=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=92=8C=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E8=A7=A3=E6=9E=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 formatConstValue 函数统一处理常量值格式化 - 修复类常量引用时的字符串转义问题 - 优化 getClassConstValue 方法的命名空间解析逻辑 - 添加 getNamespaceOfClass 工具方法获取类的命名空间 - 更新默认参数处理中的常量值获取方式 - 添加新的测试用例验证默认数组属性功能 --- src/Php/Generator/Utils.php | 11 +++++++++++ src/Php/Translator.php | 6 +++--- src/gen_stub.php | 26 +++++++++++++++++--------- tests/aot/object_property/005.phpt | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 tests/aot/object_property/005.phpt diff --git a/src/Php/Generator/Utils.php b/src/Php/Generator/Utils.php index b86511eb..89391995 100644 --- a/src/Php/Generator/Utils.php +++ b/src/Php/Generator/Utils.php @@ -183,4 +183,15 @@ trait Utils return $str; } + + protected function getNamespaceOfClass(string $class): string + { + $lastPos = strrpos($class, '\\'); + if ($lastPos !== false) { + $namespace = substr($class, 0, $lastPos); + } else { + $namespace = ''; + } + return $namespace; + } } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index e608ba82..e4442e97 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1195,11 +1195,11 @@ CODE; * 仅用于 gen_stub 脚本 * @throws \Exception */ - public function getClassConstValue(NodeAbstract $expr, string $_class, string $name, string $currentClass): mixed + public function getClassConstValue(NodeAbstract $expr, string $_class, string $name, string $currentClass = ''): mixed { $namespace = $this->namespace; - if (!$namespace) { - $namespace = (string)strstr($currentClass, '\\', true); + if (!$namespace and $currentClass and !str_contains($_class, '\\')) { + $namespace = $this->getNamespaceOfClass($currentClass); } $class = $this->getNamespacedClassName($_class, $namespace); $nativeConst = $this->findNativeClassConst($expr, $class, $name); diff --git a/src/gen_stub.php b/src/gen_stub.php index a4b68a5a..0728384a 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -2307,14 +2307,14 @@ class EvaluatedValue if ($class === 'self') { $constName = ClassInfo::$currentClass . "::" . $expr->name->__toString(); if (isset($allConstInfos[$constName])) { - return $allConstInfos[$constName]->getValue($allConstInfos)->value; + return formatConstValue($allConstInfos[$constName]->getValue($allConstInfos)->value); } else { - return getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass, $expr->name->toString(), ClassInfo::$currentClass); + return formatConstValue(getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass, $expr->name->toString())); } } elseif ($expr->name->__toString() === 'class') { return $class; } else { - return getTranslator()->getClassConstValue($expr, $class, $expr->name->__toString(), ClassInfo::$currentClass); + return formatConstValue(getTranslator()->getClassConstValue($expr, $class, $expr->name->__toString(), ClassInfo::$currentClass)); } } else { $constName = $expr->name->__toString(); @@ -2352,11 +2352,7 @@ class EvaluatedValue if (isset($definedConstants[$constName])) { $constValue = $definedConstants[$constName]; if (is_scalar($constValue)) { - if (is_string($constValue)) { - return '"' . $constValue . '"'; - } else { - return $constValue; - } + return formatConstValue($constValue); } } @@ -4869,7 +4865,7 @@ function parseFunctionLike( } if ($param->default instanceof Expr\ClassConstFetch && $param->default->class->toLowerString() === "self") { - $defaultValue = getTranslator()->getClassConstValue($func, $name->className->name, $param->default->name->name, ClassInfo::$currentClass); + $defaultValue = getTranslator()->getClassConstValue($func, $name->className->name, $param->default->name->name); } else { $defaultValue = $param->default ? $prettyPrinter->prettyPrintExpr($param->default) : null; } @@ -6186,6 +6182,18 @@ function initPhpParser() { $isInitialized = true; } +function formatConstValue(mixed $constValue) +{ + if (is_string($constValue)) { + if (str_starts_with($constValue, '"') and str_ends_with($constValue, '"')) { + return $constValue; + } + return '"' . $constValue . '"'; + } else { + return $constValue; + } +} + function getTranslator(): Translator { global $translator; diff --git a/tests/aot/object_property/005.phpt b/tests/aot/object_property/005.phpt new file mode 100644 index 00000000..cf46ae6a --- /dev/null +++ b/tests/aot/object_property/005.phpt @@ -0,0 +1,21 @@ +--TEST-- +default array property +--FILE-- +x); + } +} + +function main() { + require __DIR__ . '/../../../src/Assert.php'; + $obj = new Test; + $obj->bar(); +} +?> +--EXPECT-- +string(4) "test" \ No newline at end of file