refactor(compiler): 优化编译器类方法处理逻辑

- 移除 CompilerBase.php 中不再使用的 method 属性初始化
- 为构造、析构、克隆方法添加无返回值类型限制
- 添加类和方法重置功能,确保状态正确清理
- 统一重复变量测试消息文本
- 调整静态绑定测试中的属性访问权限
- 新增受保护属性访问测试用例
pull/1/head
韩天峰 5 months ago
parent 1341dec3f2
commit 485dce7b95
  1. 12
      phpunit/code/protected-property.php
  2. 4
      phpunit/src/ClassTest.php
  3. 2
      phpunit/src/DuplicateTest.php
  4. 1
      src/Php/CompilerBase.php
  5. 12
      src/Php/Preprocessor.php
  6. 2
      tests/aot/late-static-binding.phpt
  7. 18
      tests/aot/type_decl/004.phpt

@ -0,0 +1,12 @@
<?php
class Config {
protected static array $settings = [];
}
class DevConfig extends Config {
}
function main() {
// DevConfig 使用自己的 settings
\DevConfig::$settings = ['dev' => true, 'debug' => true];
}

@ -7,4 +7,8 @@ class ClassTest extends \BaseTest
$this->exec('Cannot re-assign $this', 're-assign-this.php'); $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');
}
} }

@ -24,7 +24,7 @@ class DuplicateTest extends TestCase
public function testStaticVar() public function testStaticVar()
{ {
$this->exec('Duplicate static variable', 'duplicate_01.php'); $this->exec('Duplicate variable', 'duplicate_01.php');
} }
public function testFunction() public function testFunction()

@ -613,7 +613,6 @@ class CompilerBase extends \PhpAot\Core\Translator
{ {
$this->class = ''; $this->class = '';
$this->interface = ''; $this->interface = '';
$this->method = '';
$this->classDef = null; $this->classDef = null;
} }

@ -326,6 +326,11 @@ class Preprocessor extends CompilerBase
$fnName = $this->parseIdentifier($v->name); $fnName = $this->parseIdentifier($v->name);
$class = ''; $class = '';
$returnType = $this->parseTypeDecl($v->returnType, self::DECL_TYPE_OF_RETURN, $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 = new FunctionDef($fnName, $returnType, $this->namespace);
$functionDef->returnClass = $class; $functionDef->returnClass = $class;
$functionDef->stub = $this->stubFile; $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 protected function prepareClass(Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): string
{ {
$this->resetClass();
$this->class = $this->parseIdentifier($class->name); $this->class = $this->parseIdentifier($class->name);
$fullClassName = $this->getFullClassName(); $fullClassName = $this->getFullClassName();
$fullClassNameLower = strtolower($fullClassName); $fullClassNameLower = strtolower($fullClassName);
@ -446,8 +452,8 @@ class Preprocessor extends CompilerBase
abort($v); abort($v);
} }
} }
$this->class = '';
$this->parentClass = ''; $this->resetClass();
return $code; 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 protected function prepareClassMethod(Node\Stmt\ClassMethod $v, Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): void
{ {
$this->resetMethod();
$name = $this->getMethodName($v); $name = $this->getMethodName($v);
$this->method = $name; $this->method = $name;
$flags = $this->parseModifiers($v->flags); $flags = $this->parseModifiers($v->flags);
@ -592,5 +599,6 @@ class Preprocessor extends CompilerBase
} }
$fullClassNameLower = strtolower($parentClass); $fullClassNameLower = strtolower($parentClass);
} }
$this->resetMethod();
} }
} }

@ -24,7 +24,7 @@ class Child extends ParentClass {
// Test with static properties // Test with static properties
class Config { class Config {
protected static array $settings = []; public static array $settings = [];
public static function get(string $key): mixed { public static function get(string $key): mixed {
return static::$settings[$key] ?? null; return static::$settings[$key] ?? null;

@ -0,0 +1,18 @@
--TEST--
Type Declarations - Strict and weak typing modes
--FILE--
<?php
function sum_iterable($numbers) {
$sum = 0;
foreach ($numbers as $num) {
$sum += $num;
}
return $sum;
}
function main() {
var_dump(sum_iterable([1, 2, 3, 4, 5]));
}
?>
--EXPECT--
int(15)
Loading…
Cancel
Save