From 0cee9a41542e17e67b59a10e7ccd2293c9efb5ee Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Apr 2026 19:00:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=94=AF=E6=8C=81=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E5=88=AB=E5=90=8D=E8=A7=A3=E6=9E=90=E5=92=8C=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现 useAliases 映射来处理常量别名 - 添加命名空间类名解析逻辑到 getNamespacedClassName 方法 - 修复 parseUse 方法的参数类型声明为 Node\Stmt\Use_ - 更新变量命名提高代码可读性 - 添加 use-const.phpt 测试用例验证常量别名功能 --- src/Php/CompilerBase.php | 16 +++++++++++----- tests/aot/use-const.phpt | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 tests/aot/use-const.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 2b74ef00..3b39f89b 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3131,6 +3131,13 @@ class CompilerBase extends \PhpAot\Core\Translator $fullName = $this->getNamespacedClassName($ns[0]); $ce = $this->getClassEntryPtr($fullName); return 'php::constant(' . $ce . ', ' . $this->getLiteralString($ns[1]) . ')'; + } elseif (isset($this->useAliases[$name])) { + $name = $this->useAliases[$name]; + } else { + $fullName = $this->getNamespacedClassName($name); + if ($fullName) { + $name = $fullName; + } } return 'php::constant(nullptr, ' . $this->getLiteralString($name) . ')'; } @@ -4374,16 +4381,15 @@ class CompilerBase extends \PhpAot\Core\Translator } } - protected function parseUse(mixed $v2): string + protected function parseUse(Node\Stmt\Use_ $v2): string { $code = ''; foreach ($v2->uses as $use) { $id = $this->parseIdentifier($use->name); if ($use->type === Node\Stmt\Use_::TYPE_FUNCTION) { - $rpos = strrpos($id, '\\'); - $fn = substr($id, $rpos + 1); - $ns = substr($id, 0, $rpos); - // fn => namespace + $lastIndex = strrpos($id, '\\'); + $fn = substr($id, $lastIndex + 1); + $ns = substr($id, 0, $lastIndex); $this->useFunctions[$fn] = $ns; } else { if ($id === 'native_types') { diff --git a/tests/aot/use-const.phpt b/tests/aot/use-const.phpt new file mode 100644 index 00000000..33a03e51 --- /dev/null +++ b/tests/aot/use-const.phpt @@ -0,0 +1,28 @@ +--TEST-- +aliasing imported constants to resolve naming conflicts +--FILE-- + +--EXPECT-- +int(42) +int(43) +Done