refactor(php): 修改访问权限检查方法的参数类型

- 将 checkAccessible 方法的第二个参数从 MethodDef|ConstantDef 对象改为 int 类型的 flags
- 更新 ConstantDef 类中 flags 属性和构造函数参数类型为 int
- 提取访问修饰符解析逻辑到独立的 parseModifiers 方法
- 在类常量、属性和方法解析中统一使用 parseModifiers 方法处理 flags
pull/1/head
韩天峰 5 months ago
parent 7eed77138a
commit 67275c45f7
  1. 8
      src/Php/CompilerBase.php
  2. 4
      src/Php/Entity/ConstantDef.php
  3. 18
      src/Php/Translator.php

@ -1669,7 +1669,7 @@ class CompilerBase extends \PhpAot\Core\Translator
} }
} }
if (!$this->checkAccessible($classDef, $methodDef)) { if (!$this->checkAccessible($classDef, $methodDef->flags)) {
$this->fatalError($expr, 'Method `' . $classDef->getNamespacedName() . '::' . $method . '()` is not accessible'); $this->fatalError($expr, 'Method `' . $classDef->getNamespacedName() . '::' . $method . '()` is not accessible');
} }
// 函数调用占位符,不是真实的函数调用 // 函数调用占位符,不是真实的函数调用
@ -1703,7 +1703,7 @@ class CompilerBase extends \PhpAot\Core\Translator
break; break;
} }
} }
if (!$this->checkAccessible($classDef, $constDef)) { if (!$this->checkAccessible($classDef, $constDef->flags)) {
$this->fatalError($expr, 'Constant `' . $classDef->getNamespacedName() . '::' . $const . '` is not accessible'); $this->fatalError($expr, 'Constant `' . $classDef->getNamespacedName() . '::' . $const . '` is not accessible');
} }
if ($constDef->type === self::TYPE_ARRAY) { if ($constDef->type === self::TYPE_ARRAY) {
@ -4199,7 +4199,7 @@ class CompilerBase extends \PhpAot\Core\Translator
} }
} }
protected function checkAccessible(ClassDef $classDef, MethodDef|ConstantDef $def): bool protected function checkAccessible(ClassDef $classDef, int $flags): bool
{ {
// 在当前类中,允许调用所有方法 // 在当前类中,允许调用所有方法
if ($classDef->namespace === $this->namespace and $classDef->name == $this->class) { if ($classDef->namespace === $this->namespace and $classDef->name == $this->class) {
@ -4207,7 +4207,7 @@ class CompilerBase extends \PhpAot\Core\Translator
} }
// 类外部调用,只允许调用 public 方法 // 类外部调用,只允许调用 public 方法
return $def->flags & Modifiers::PUBLIC; return $flags & Modifiers::PUBLIC;
} }
protected function isOverrideMethod(string $fullMethodName): bool protected function isOverrideMethod(string $fullMethodName): bool

@ -12,11 +12,11 @@ class ConstantDef
{ {
public string $name; public string $name;
public string $type; public string $type;
public string $flags; public int $flags;
public string $value; public string $value;
public string $arrayExpr; public string $arrayExpr;
public function __construct(string $name, string $flags, string $type, string $value, string $arrayExpr = '') public function __construct(string $name, int $flags, string $type, string $value, string $arrayExpr = '')
{ {
$this->name = $name; $this->name = $name;
$this->type = $type; $this->type = $type;

@ -1106,7 +1106,7 @@ class Translator extends Preprocessor
protected function parseClassConstDef(Node\Stmt\ClassConst $v): void protected function parseClassConstDef(Node\Stmt\ClassConst $v): void
{ {
$this->resetFunction(); $this->resetFunction();
$flags = $v->flags; $flags = $this->parseModifiers($v->flags);
if ($v->type) { if ($v->type) {
$type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_CONST); $type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_CONST);
} else { } else {
@ -1136,7 +1136,7 @@ class Translator extends Preprocessor
protected function parsePropertyDef(Node\Stmt\Property $v): void protected function parsePropertyDef(Node\Stmt\Property $v): void
{ {
$flags = $v->flags; $flags = $this->parseModifiers($v->flags);
$type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_PROPERTY); $type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_PROPERTY);
foreach ($v->props as $prop) { foreach ($v->props as $prop) {
@ -1151,15 +1151,19 @@ class Translator extends Preprocessor
} }
} }
protected function parseClassMethod(Node\Stmt\ClassMethod $v, array &$methodCodes): void protected function parseModifiers(int $flags): int
{ {
$name = $this->getMethodName($v);
$this->method = $name;
$flags = $v->flags;
if (!($flags & Modifiers::PRIVATE) and !($flags & Modifiers::PROTECTED)) { if (!($flags & Modifiers::PRIVATE) and !($flags & Modifiers::PROTECTED)) {
$flags |= Modifiers::PUBLIC; $flags |= Modifiers::PUBLIC;
} }
return $flags;
}
protected function parseClassMethod(Node\Stmt\ClassMethod $v, array &$methodCodes): void
{
$name = $this->getMethodName($v);
$this->method = $name;
$flags = $this->parseModifiers($v->flags);
if (!($flags & Modifiers::ABSTRACT)) { if (!($flags & Modifiers::ABSTRACT)) {
$this->methodDef = new MethodDef($flags, $name); $this->methodDef = new MethodDef($flags, $name);

Loading…
Cancel
Save