From 1a7bfe1de5b59af0afd1f034d7f18b9d94a6a5fd Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 1 Apr 2026 10:06:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(preprocessor):=20=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=87=BD=E6=95=B0=E5=92=8C=E7=B1=BB=E5=A3=B0?= =?UTF-8?q?=E6=98=8E=E6=A3=80=E6=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在函数声明前添加 prepare 步骤以确保预处理器初始化 - 添加重复函数名称检测逻辑,防止相同函数名重复声明 - 添加重复类名检测逻辑,避免类重复定义错误 - 使用小写函数名作为符号表键值进行冲突检查 - 在检测到重复声明时抛出致命错误 --- phpunit/src/DuplicateTest.php | 1 + src/Php/Preprocessor.php | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/phpunit/src/DuplicateTest.php b/phpunit/src/DuplicateTest.php index 767ef6ee..5653c11c 100644 --- a/phpunit/src/DuplicateTest.php +++ b/phpunit/src/DuplicateTest.php @@ -13,6 +13,7 @@ class DuplicateTest extends TestCase $compiler = CompilerTest::create(ROOT_PATH); $testFile = __DIR__ . '/../code/' . $file; $compiler->addFiles([$testFile]); + $compiler->prepare($testFile); $compiler->convert($testFile); } catch (TestError $exception) { $this->assertStringContainsString($expected, $exception->getMessage()); diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index b0221e11..3689c70a 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -197,7 +197,11 @@ class Preprocessor extends CompilerBase if ($this->stubFile) { $this->addFunction($name, $this->parseFunctionDecl($v)); } else { - $this->symbolDeclInFile[strtolower($name)] = $this->file; + $functionNameLower = strtolower($name); + if (isset($this->symbolDeclInFile[$functionNameLower])) { + $this->fatalError($v, "Duplicate function `{$functionNameLower}`"); + } + $this->symbolDeclInFile[$functionNameLower] = $this->file; } } @@ -227,6 +231,11 @@ class Preprocessor extends CompilerBase $this->symbolCallInFile[$this->file][] = $parentClassLower; } } + + if (isset($this->symbolDeclInFile[$fullClassNameLower])) { + $this->fatalError($class, "Duplicate class `{$fullClassName}`"); + } + $this->symbolDeclInFile[$fullClassNameLower] = $this->file; $code = '';