From 485dce7b9551eabf18ef3f36261eee06a99d8bf4 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 9 Apr 2026 17:22:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(compiler):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=99=A8=E7=B1=BB=E6=96=B9=E6=B3=95=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 CompilerBase.php 中不再使用的 method 属性初始化 - 为构造、析构、克隆方法添加无返回值类型限制 - 添加类和方法重置功能,确保状态正确清理 - 统一重复变量测试消息文本 - 调整静态绑定测试中的属性访问权限 - 新增受保护属性访问测试用例 --- phpunit/code/protected-property.php | 12 ++++++++++++ phpunit/src/ClassTest.php | 4 ++++ phpunit/src/DuplicateTest.php | 2 +- src/Php/CompilerBase.php | 1 - src/Php/Preprocessor.php | 12 ++++++++++-- tests/aot/late-static-binding.phpt | 2 +- tests/aot/type_decl/004.phpt | 18 ++++++++++++++++++ 7 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 phpunit/code/protected-property.php create mode 100644 tests/aot/type_decl/004.phpt diff --git a/phpunit/code/protected-property.php b/phpunit/code/protected-property.php new file mode 100644 index 00000000..3b148fa8 --- /dev/null +++ b/phpunit/code/protected-property.php @@ -0,0 +1,12 @@ + true, 'debug' => true]; +} diff --git a/phpunit/src/ClassTest.php b/phpunit/src/ClassTest.php index de641eee..5144aeb5 100644 --- a/phpunit/src/ClassTest.php +++ b/phpunit/src/ClassTest.php @@ -7,4 +7,8 @@ class ClassTest extends \BaseTest $this->exec('Cannot re-assign $this', 're-assign-this.php'); } + public function testAccessProtectedProperty() + { + $this->exec('Cannot access protected property `settings` of class `DevConfig`', 'protected-property.php'); + } } diff --git a/phpunit/src/DuplicateTest.php b/phpunit/src/DuplicateTest.php index 5653c11c..215e1e97 100644 --- a/phpunit/src/DuplicateTest.php +++ b/phpunit/src/DuplicateTest.php @@ -24,7 +24,7 @@ class DuplicateTest extends TestCase public function testStaticVar() { - $this->exec('Duplicate static variable', 'duplicate_01.php'); + $this->exec('Duplicate variable', 'duplicate_01.php'); } public function testFunction() diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 69fd1992..ebcb034f 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -613,7 +613,6 @@ class CompilerBase extends \PhpAot\Core\Translator { $this->class = ''; $this->interface = ''; - $this->method = ''; $this->classDef = null; } diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 7810dd09..5bd33994 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -326,6 +326,11 @@ class Preprocessor extends CompilerBase $fnName = $this->parseIdentifier($v->name); $class = ''; $returnType = $this->parseTypeDecl($v->returnType, self::DECL_TYPE_OF_RETURN, $class); + // 构造、析构、克隆方法不能有返回值 + if ($this->method and in_array($this->method, ['__construct', '__destruct', '__clone'])) { + $returnType = self::TYPE_VOID; + } + $functionDef = new FunctionDef($fnName, $returnType, $this->namespace); $functionDef->returnClass = $class; $functionDef->stub = $this->stubFile; @@ -380,6 +385,7 @@ class Preprocessor extends CompilerBase protected function prepareClass(Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): string { + $this->resetClass(); $this->class = $this->parseIdentifier($class->name); $fullClassName = $this->getFullClassName(); $fullClassNameLower = strtolower($fullClassName); @@ -446,8 +452,8 @@ class Preprocessor extends CompilerBase abort($v); } } - $this->class = ''; - $this->parentClass = ''; + + $this->resetClass(); return $code; } @@ -551,6 +557,7 @@ class Preprocessor extends CompilerBase protected function prepareClassMethod(Node\Stmt\ClassMethod $v, Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): void { + $this->resetMethod(); $name = $this->getMethodName($v); $this->method = $name; $flags = $this->parseModifiers($v->flags); @@ -592,5 +599,6 @@ class Preprocessor extends CompilerBase } $fullClassNameLower = strtolower($parentClass); } + $this->resetMethod(); } } diff --git a/tests/aot/late-static-binding.phpt b/tests/aot/late-static-binding.phpt index 63f194a0..f48b57bb 100644 --- a/tests/aot/late-static-binding.phpt +++ b/tests/aot/late-static-binding.phpt @@ -24,7 +24,7 @@ class Child extends ParentClass { // Test with static properties class Config { - protected static array $settings = []; + public static array $settings = []; public static function get(string $key): mixed { return static::$settings[$key] ?? null; diff --git a/tests/aot/type_decl/004.phpt b/tests/aot/type_decl/004.phpt new file mode 100644 index 00000000..6d986f9a --- /dev/null +++ b/tests/aot/type_decl/004.phpt @@ -0,0 +1,18 @@ +--TEST-- +Type Declarations - Strict and weak typing modes +--FILE-- + +--EXPECT-- +int(15)