refactor(php): 重构原生属性访问逻辑以提高代码可维护性

- 引入 NativePropertyAccess 类来统一管理原生属性访问信息
- 将直接使用 getAttribute 的方式改为专用的 getter/setter 方法
- 修改属性变量检查逻辑从字符串验证改为 null 检查
- 优化属性访问结果的应用方式,使用对象封装替代多个属性设置
- 添加类型安全的原生属性访问获取方法
- 统一属性变量的设置和获取接口
pull/4/head
韩天峰 2 months ago
parent c34d11c136
commit 6f15c61720
  1. 36
      src/Php/CompilerBase.php
  2. 4
      src/Php/Parser/BinaryOpTrait.php

@ -44,6 +44,7 @@ use PhpAot\Php\Platform\PlatformBase;
use PhpAot\Php\Platform\PlatformFactory; use PhpAot\Php\Platform\PlatformFactory;
use PhpAot\Php\Platform\Windows; use PhpAot\Php\Platform\Windows;
use PhpAot\Php\Resolver\PropertyAccessContext; use PhpAot\Php\Resolver\PropertyAccessContext;
use PhpAot\Php\Resolver\NativePropertyAccess;
use PhpAot\Php\Resolver\PropertyAccessResult; use PhpAot\Php\Resolver\PropertyAccessResult;
use PhpAot\Php\Resolver\PropertyAccessResolver; use PhpAot\Php\Resolver\PropertyAccessResolver;
use PhpAot\Php\Resolver\PropertyAssignTypeInfo; use PhpAot\Php\Resolver\PropertyAssignTypeInfo;
@ -2410,8 +2411,8 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
// Native property var type // Native property var type
if ($this->isVarExpr($expr->var)) { if ($this->isVarExpr($expr->var)) {
$this->parsePropertyFetch($expr); $this->parsePropertyFetch($expr);
if ($expr->getAttribute('nativePropertyVar')) { $propVar = $this->getNativePropertyVar($expr);
$propVar = $expr->getAttribute('nativePropertyVar'); if ($propVar !== null) {
$info = $this->context->objectProps[$propVar]; $info = $this->context->objectProps[$propVar];
return $info['type']; return $info['type'];
} }
@ -4906,14 +4907,14 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
'kind' => $info['kind'], 'kind' => $info['kind'],
]; ];
} }
$expr->setAttribute('nativePropertyVar', $propVar); $this->setNativePropertyVar($expr, $propVar);
$this->setNativePropertyValueSource($expr, self::NATIVE_PROPERTY_VALUE_VAR); $this->setNativePropertyValueSource($expr, self::NATIVE_PROPERTY_VALUE_VAR);
return $propVar; return $propVar;
} }
} elseif ($this->canHoistStableObjectProp($objectVar, $propName)) { } elseif ($this->canHoistStableObjectProp($objectVar, $propName)) {
// SSA-stable object: lazily create reference at first access point // SSA-stable object: lazily create reference at first access point
$result = $this->hoistStableObjectProp($objectVar, $propName, $id, $def->type); $result = $this->hoistStableObjectProp($objectVar, $propName, $id, $def->type);
$expr->setAttribute('nativePropertyVar', $result); $this->setNativePropertyVar($expr, $result);
$this->setNativePropertyValueSource($expr, self::NATIVE_PROPERTY_VALUE_VAR); $this->setNativePropertyValueSource($expr, self::NATIVE_PROPERTY_VALUE_VAR);
return $result; return $result;
} }
@ -5845,25 +5846,40 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
private function applyNativePropertyAccessResult(NodeAbstract $expr, PropertyAccessResult $result): string private function applyNativePropertyAccessResult(NodeAbstract $expr, PropertyAccessResult $result): string
{ {
$offset = $this->getPropertyOffset($result->classDef->getNamespacedName(false), $result->property); $offset = $this->getPropertyOffset($result->classDef->getNamespacedName(false), $result->property);
$expr->setAttribute('nativePropertyDef', $result->propertyDef); $expr->setAttribute('nativePropertyAccess', new NativePropertyAccess($offset, $result));
$expr->setAttribute('nativeClassDef', $result->classDef);
$expr->setAttribute('nativeProperty', $offset);
return $offset; return $offset;
} }
protected function isNativePropertyAccess(NodeAbstract $expr): bool protected function isNativePropertyAccess(NodeAbstract $expr): bool
{ {
return $expr->hasAttribute('nativeProperty'); return $this->getNativePropertyAccess($expr) !== null;
} }
protected function getNativePropertyDef(NodeAbstract $expr): ?PropertyDef protected function getNativePropertyDef(NodeAbstract $expr): ?PropertyDef
{ {
return $expr->hasAttribute('nativePropertyDef') ? $expr->getAttribute('nativePropertyDef') : null; return $this->getNativePropertyAccess($expr)?->getPropertyDef();
} }
protected function getNativePropertyClassDef(NodeAbstract $expr): ?ClassDef protected function getNativePropertyClassDef(NodeAbstract $expr): ?ClassDef
{ {
return $expr->hasAttribute('nativeClassDef') ? $expr->getAttribute('nativeClassDef') : null; return $this->getNativePropertyAccess($expr)?->getClassDef();
}
private function getNativePropertyAccess(NodeAbstract $expr): ?NativePropertyAccess
{
$access = $expr->getAttribute('nativePropertyAccess');
return $access instanceof NativePropertyAccess ? $access : null;
}
protected function setNativePropertyVar(NodeAbstract $expr, string $var): void
{
$expr->setAttribute('nativePropertyVar', $var);
}
protected function getNativePropertyVar(NodeAbstract $expr): ?string
{
$var = $expr->getAttribute('nativePropertyVar');
return is_string($var) ? $var : null;
} }
protected function setNativePropertyValueSource(NodeAbstract $expr, string $source): void protected function setNativePropertyValueSource(NodeAbstract $expr, string $source): void

@ -204,8 +204,8 @@ trait BinaryOpTrait
} }
if ($expr instanceof Expr\PropertyFetch) { if ($expr instanceof Expr\PropertyFetch) {
$nativePropertyVar = $expr->getAttribute('nativePropertyVar'); $nativePropertyVar = $this->getNativePropertyVar($expr);
if (is_string($nativePropertyVar) && $nativePropertyVar === $value) { if ($nativePropertyVar !== null && $nativePropertyVar === $value) {
if (isset($this->context->objectProps[$nativePropertyVar])) { if (isset($this->context->objectProps[$nativePropertyVar])) {
return $this->context->objectProps[$nativePropertyVar]['type']; return $this->context->objectProps[$nativePropertyVar]['type'];
} }

Loading…
Cancel
Save