fix(php): 修复严格类型声明和类型检查逻辑

- 移除未使用的 strictTypes 属性
- 在 declare(strict_types=0) 时抛出错误,只支持 strict_types=1
- 简化类型赋值检查逻辑,统一类型相同时可赋值
- 移除宽松类型转换警告相关代码
pull/1/head
韩天峰 3 months ago
parent 92a4ecd24c
commit 31bd1ae256
  1. 21
      src/Php/CompilerBase.php
  2. 2
      version.txt

@ -309,7 +309,6 @@ class CompilerBase extends \PhpAot\Core\Translator
'_ENV' => self::TYPE_ARRAY,
];
protected array $globalVars = [];
protected bool $strictTypes = false;
protected bool $nativeTypes = false;
protected bool $decimalTypes = false;
protected bool $bigintTypes = false;
@ -873,7 +872,6 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function resetFile(): void
{
$this->indentLevel = 0;
$this->strictTypes = false;
$this->nativeTypes = false;
$this->decimalTypes = false;
$this->bigintTypes = false;
@ -5898,7 +5896,9 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->fatalError($v, 'declare(encoding="' . $value . '") is not supported, only UTF-8 is supported');
}
} elseif ($key === 'strict_types') {
$this->strictTypes = boolval(intval($value));
if (!($declare->value instanceof Node\Scalar\Int_) or $declare->value->value !== 1) {
$this->fatalError($v, 'declare(strict_types=0) is not allowed, only strict_types=1 is supported');
}
} else {
$this->fatalError($v, 'declare(' . $key . '=' . $value . ') is not supported');
}
@ -5998,21 +5998,10 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($toType === self::TYPE_REF or $fromType === self::TYPE_REF) {
return true;
}
if ($toType === self::TYPE_OBJECT and $fromType === self::TYPE_OBJECT) {
return true;
}
if ($toType === self::TYPE_ARRAY and $fromType === self::TYPE_ARRAY) {
// 类型一致,可以互相赋值
if ($toType === $fromType) {
return true;
}
if ($toType === self::TYPE_STR) {
if ($fromType === self::TYPE_STR) {
return true;
}
if (!$this->strictTypes) {
$this->warning($left, "Attempt to implicitly convert `{$fromType}` to `{$toType}`");
return true;
}
}
// 原生类型可以互相转换,由 C++ 底层完成
if ($this->isNativeType($toType) and $this->isNativeType($fromType)) {
return true;

@ -1 +1 @@
1053
1054

Loading…
Cancel
Save