refactor(php): 统一使用小写键值优化编译器方法重写检查

- 在 CompilerBase 中添加 isOverrideMethod 辅助方法简化重写判断逻辑
- 将 Preprocessor 中的所有符号名称转换为小写以确保一致性
- 修改类继承关系存储使用小写键值避免大小写敏感问题
- 优化方法重写检测循环使用小写键值提升查找性能
- 统一函数和方法名称的大小写处理逻辑
pull/1/head
韩天峰 5 months ago
parent 183b2ba96c
commit 3743a32eb8
  1. 8
      src/Php/CompilerBase.php
  2. 31
      src/Php/Preprocessor.php

@ -4193,6 +4193,12 @@ class CompilerBase extends \PhpAot\Core\Translator
return $def->flags & Modifiers::PUBLIC;
}
protected function isOverrideMethod(string $fullMethodName): bool
{
$fullMethodNameLower = strtolower($fullMethodName);
return isset($this->classMethodOverride[$fullMethodNameLower]) and $this->classMethodOverride[$fullMethodNameLower];
}
protected function findNativeMethod(CallLike $expr, string $object, string $method): string|false
{
$nativeFunc = '';
@ -4215,7 +4221,7 @@ class CompilerBase extends \PhpAot\Core\Translator
}
// 存在子类同名方法,需要转为动态调用
if (isset($this->classMethodOverride[$fullMethodName]) and $this->classMethodOverride[$fullMethodName]) {
if ($this->isOverrideMethod($fullMethodName)) {
return false;
}
if ($nativeFunc) {

@ -122,7 +122,7 @@ class Preprocessor extends CompilerBase
if ($call->name instanceof Node\Name) {
$name = $call->name->toString();
$this->symbolCallInFile[] = [
'name' => $name,
'name' => strtolower($name),
'file' => $this->file,
'line' => $call->getLine(),
];
@ -164,7 +164,7 @@ class Preprocessor extends CompilerBase
if ($this->stubFile) {
$this->nativeFunctions[$name] = $this->parseFunctionDecl($v);
} else {
$this->symbolDeclInFile[$name] = $this->file;
$this->symbolDeclInFile[strtolower($name)] = $this->file;
}
}
@ -181,16 +181,18 @@ class Preprocessor extends CompilerBase
{
$this->class = $this->parseIdentifier($class->name);
$fullClassName = $this->getNamespacedClassName($this->class);
$fullClassNameLower = strtolower($fullClassName);
if (!empty($class->extends)) {
$this->parentClass = $this->getParentClass($class->extends);
$parentClassLower = strtolower($this->parentClass);
$this->symbolCallInFile[] = [
'name' => $this->parentClass,
'name' => $parentClassLower,
'file' => $this->file,
'line' => $class->getLine(),
];
$this->classExtends[$fullClassName] = $this->parentClass;
$this->classExtends[$fullClassNameLower] = $parentClassLower;
}
$this->symbolDeclInFile[$fullClassName] = $this->file;
$this->symbolDeclInFile[$fullClassNameLower] = $this->file;
$code = '';
foreach ($class->stmts as $v) {
@ -223,16 +225,17 @@ class Preprocessor extends CompilerBase
$this->prepareFunction($v) . PHP_EOL;
if ($this->isIdExpr($v->name) or $this->isNameExpr($v->name)) {
$fullClassName = $this->getNamespacedClassName($this->class);
$fullName = $fullClassName . '::' . $v->name;
$this->classMethodOverride[$fullName] = false;
$fullMethodName = $fullClassName . '::' . $v->name;
$this->classMethodOverride[strtolower($fullMethodName)] = false;
// 查找父类是否有同名方法,递归查找
while (isset($this->classExtends[$fullClassName])) {
$parentClass = $this->classExtends[$fullClassName];
$parentMethod = $parentClass . '::' . $v->name;
if (isset($this->classMethodOverride[$parentMethod])) {
$this->classMethodOverride[$parentMethod] = true;
}
$fullClassName = $parentClass;
$fullClassNameLower = strtolower($fullClassName);
while (isset($this->classExtends[$fullClassNameLower])) {
$parentClass = $this->classExtends[$fullClassNameLower];
$parentMethodLower = strtolower($parentClass . '::' . $v->name);
if (isset($this->classMethodOverride[$parentMethodLower])) {
$this->classMethodOverride[$parentMethodLower] = true;
}
$fullClassNameLower = strtolower($parentClass);
}
}
}

Loading…
Cancel
Save