refactor(php): 重构类名比较和属性访问检查逻辑

- 移除对 PropertyAccessResolver 的依赖,直接实现类名比较功能
- 使用 strcasecmp 函数进行大小写不敏感的类名比较
- 重写 isSameOrSubclassOf 方法,通过 getParentClass 遍历继承链
- 简化 canAccessProtectedProperty 逻辑,优化作用域检查条件
- 更新单元测试中的属性访问器解析方法调用方式
pull/4/head
韩天峰 2 months ago
parent c05c63b8aa
commit 0af331db16
  1. 4
      phpunit/src/CompilerPhaseTest.php
  2. 18
      src/Php/CompilerBase.php

@ -24,7 +24,9 @@ class CompilerPhaseProbe extends CompilerTest
public function probePropertyAccessResolver(): bool
{
return $this->canAccessProtectedProperty('ProbeClass', 'ProbeClass');
$method = new ReflectionMethod(\PhpAot\Php\CompilerBase::class, 'createPropertyAccessResolver');
$resolver = $method->invoke($this);
return $resolver->canAccessProtectedProperty('ProbeClass', 'ProbeClass');
}
}

@ -5937,17 +5937,29 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
protected function isSameClassName(string $classA, string $classB): bool
{
return $this->createPropertyAccessResolver()->isSameClassName($classA, $classB);
return strcasecmp(ltrim($classA, '\\'), ltrim($classB, '\\')) === 0;
}
protected function isSameOrSubclassOf(string $class, string $parent): bool
{
return $this->createPropertyAccessResolver()->isSameOrSubclassOf($class, $parent);
$class = strtolower(ltrim($class, '\\'));
$parent = strtolower(ltrim($parent, '\\'));
while ($class !== '') {
if ($class === $parent) {
return true;
}
$class = $this->getParentClass($class);
}
return false;
}
protected function canAccessProtectedProperty(string $scope, string $declaringClass): bool
{
return $this->createPropertyAccessResolver()->canAccessProtectedProperty($scope, $declaringClass);
if ($scope === '') {
return false;
}
return $this->isSameOrSubclassOf($scope, $declaringClass)
|| $this->isSameOrSubclassOf($declaringClass, $scope);
}
private function resolveNativeInstanceProperty(NodeAbstract $expr, string $property, string $class): ?PropertyAccessResult

Loading…
Cancel
Save