fix(php): 修复编译器基类中的回溯打印和类型返回验证问题

- 将 printBacktraceOnError 默认值从 false 修改为 true
- 扩展子类作为父类返回时的类型检查,支持抽象类或接口
- 更新 SimpleType::fromNode 和 fromString 方法以支持 ArrayType 返回类型
- 修改测试用例中 ZipArchive 的路径处理逻辑
- 更新测试输出预期结果以匹配新的行为
pull/1/head
韩天峰 4 months ago
parent 76ed488b75
commit eaa1b4cd9a
  1. 8
      src/Php/CompilerBase.php
  2. 5
      src/gen_stub.php
  3. 7
      tests/aot/ref-call-arg.phpt

@ -174,7 +174,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected int $floatPrecision = 17;
protected bool $debug = false;
protected bool $formatCode = true;
protected bool $printBacktraceOnError = false;
protected bool $printBacktraceOnError = true;
protected bool $noLiteralStrings = false;
protected bool $noConsole = false; // Windows: hide console window
protected string $sanitize = ''; // Sanitizer type (address, undefined, etc.)
@ -1800,9 +1800,9 @@ class CompilerBase extends \PhpAot\Core\Translator
} elseif (!$this->isInheritedFrom($objectClass, $returnClass)) {
$this->fatalError($v, 'The return type is `' . $returnClass . '`, cannot return an instance of `' . $objectClass . '`');
}
// 把子类当做父类返回时,父类必须是抽象类
if ($objectClass and $objectClass !== $returnClass and !$this->isAbstractClass($returnClass)) {
$this->fatalError($v, "When returning a subclass `$objectClass` instance as parent type, the parent class `$returnClass` must be abstract");
// 把子类当做父类返回时,父类必须是抽象类或者接口
if ($objectClass and $objectClass !== $returnClass and !$this->isAbstractClass($returnClass) and !$this->hasInterface($returnClass)) {
$this->fatalError($v, "When returning a subclass `$objectClass` instance as parent type, the parent class `$returnClass` must be abstract/interface");
}
}

@ -218,7 +218,8 @@ class SimpleType {
public /* readonly */ string $name;
public /* readonly */ bool $isBuiltin;
public static function fromNode(Node $node): SimpleType {
public static function fromNode(Node $node): SimpleType|ArrayType
{
if ($node instanceof Node\Name) {
if ($node->toLowerString() === 'static') {
// PHP internally considers "static" a builtin type.
@ -244,7 +245,7 @@ class SimpleType {
throw new Exception("Unexpected node type");
}
public static function fromString(string $typeString): SimpleType
public static function fromString(string $typeString): SimpleType|ArrayType
{
switch (strtolower($typeString)) {
case "void":

@ -5,7 +5,8 @@ ref call arg
function main()
{
$zip = new ZipArchive();
if ($zip->open(__DIR__ . '/../../examples/test.zip') === TRUE) {
$path = realpath(__DIR__ . '/../../../data/test.zip');
if ($zip->open($path) === TRUE) {
for ($idx = 0; $s = $zip->statIndex($idx); $idx++) {
$rs = $zip->getExternalAttributesIndex($idx, $opsys, $attr);
var_dump($rs, $idx, $opsys, $attr);
@ -35,6 +36,10 @@ bool(true)
int(2)
int(3)
int(2176057344)
bool(true)
int(3)
int(3)
int(2176057344)
OK
value
foo bar

Loading…
Cancel
Save