refactor(php): 重构参数类型检查和方法重写逻辑

- 调整了对象参数类型验证的条件判断结构
- 在反射类中添加了获取类方法修饰符的辅助方法
- 优化了父类方法重写的检查逻辑,支持内置类的私有方法检测
- 添加了标签跳转来处理私有方法重写的错误情况
pull/1/head v0.0.3
韩天峰 5 months ago
parent ea34eaaf89
commit 156fb0a8bf
  1. 12
      src/Php/CompilerBase.php
  2. 15
      src/Php/Reflection.php
  3. 8
      src/Php/Translator.php

@ -3270,11 +3270,13 @@ class CompilerBase extends \PhpAot\Core\Translator
}
if ($argInfo->type === self::TYPE_OBJECT) {
$object = $this->parseVariable($arg->value);
if ($this->isVarExpr($arg->value) and $this->isTypedObject($object)) {
$class = $this->getObjectType($object);
if ($argInfo->class and $class != $argInfo->class) {
$this->fatalError($arg, "Argument `{$argInfo->name}` must be an instance of `{$argInfo->class}`, `{$class}` given");
if ($this->isVarExpr($arg->value)) {
$object = $this->parseVariable($arg->value);
if ($this->isTypedObject($object)) {
$class = $this->getObjectType($object);
if ($argInfo->class and $class != $argInfo->class) {
$this->fatalError($arg, "Argument `{$argInfo->name}` must be an instance of `{$argInfo->class}`, `{$class}` given");
}
}
}
return $this->convertObjectExpr($expr);

@ -95,6 +95,21 @@ class Reflection
return $args[$index];
}
public static function getClassMethodModifiers(string $className, string $fn): ?int
{
$classRef = self::getClass($className);
if (!$classRef) {
return null;
}
try {
$method = $classRef->getMethod($fn);
return $method->getModifiers();
} catch (\ReflectionException $e) {
return null;
}
}
public static function getClassMethodParameter(string $className, string $fn, int $index): ?\ReflectionParameter
{
$classRef = self::getClass($className);

@ -1200,10 +1200,18 @@ class Translator extends Preprocessor
if (!$extends) {
break;
}
// 父类是内置类
if ($classDef->inheritedFromInternalClass) {
if (Reflection::getClassMethodModifiers($extends, $name) & \ReflectionMethod::IS_PRIVATE) {
goto _error;
}
break;
}
$classDef = $this->getClass($extends);
if ($classDef->hasMethod($this->method)) {
$methodDef = $classDef->getMethod($this->method);
if ($methodDef->flags & Modifiers::PRIVATE) {
_error:
$this->fatalError($v,
'Cannot override private method `' .
$classDef->getNamespacedName(false) . '::' . $this->method . '()`');

Loading…
Cancel
Save