refactor(php): 重构参数类型解析和声明处理方法

- 将 parseParameterType 方法从 CompilerBase 移动到 Preprocessor
- 将 parseDeclare 方法从 CompilerBase 移动到 Translator
- 保持原有功能不变但优化代码结构分布
pull/1/head
韩天峰 3 months ago
parent a524211cd3
commit b33cdeb086
  1. 35
      src/Php/CompilerBase.php
  2. 13
      src/Php/Preprocessor.php
  3. 22
      src/Php/Translator.php

@ -2147,19 +2147,6 @@ class CompilerBase extends \PhpAot\Core\Translator
'}';
}
protected function parseParameterType(Node\Param $param, ArgInfo $argInfo, string $var): string
{
if ($param->byRef) {
return self::TYPE_REF;
}
$class = '';
$type = $this->parseTypeDecl($param->type, self::DECL_TYPE_OF_PARAM, $class);
if ($class and !$this->hasInterface($class) and !$this->isAbstractClass($class)) {
$argInfo->class = $class;
}
return $type;
}
/**
* 获取包含路径
*/
@ -4820,28 +4807,6 @@ class CompilerBase extends \PhpAot\Core\Translator
return $phpCode;
}
protected function parseDeclare(mixed $v): void
{
$declares = $v->declares;
foreach ($declares as $declare) {
$key = $this->parseIdentifier($declare->key);
$value = $this->parseIdentifier($declare->value);
if ($key === 'ticks') {
$this->fatalError($v, 'declare(ticks=1) is not supported');
} elseif ($key === 'encoding') {
if (strtolower($value) !== 'utf-8') {
$this->fatalError($v, 'declare(encoding="' . $value . '") is not supported, only UTF-8 is supported');
}
} elseif ($key === 'strict_types') {
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');
}
}
}
protected function parseUse(Node\Stmt\Use_ $v2): string
{
$code = '';

@ -221,6 +221,19 @@ class Preprocessor extends CompilerBase
}
}
protected function parseParameterType(Node\Param $param, ArgInfo $argInfo, string $var): string
{
if ($param->byRef) {
return self::TYPE_REF;
}
$class = '';
$type = $this->parseTypeDecl($param->type, self::DECL_TYPE_OF_PARAM, $class);
if ($class and !$this->hasInterface($class) and !$this->isAbstractClass($class)) {
$argInfo->class = $class;
}
return $type;
}
/**
* @param $params array<Node\Param>
*/

@ -1729,6 +1729,28 @@ CODE;
return $this->getNativeName($methodDef->name, $classDef->namespace, $classDef->name);
}
protected function parseDeclare(mixed $v): void
{
$declares = $v->declares;
foreach ($declares as $declare) {
$key = $this->parseIdentifier($declare->key);
$value = $this->parseIdentifier($declare->value);
if ($key === 'ticks') {
$this->fatalError($v, 'declare(ticks=1) is not supported');
} elseif ($key === 'encoding') {
if (strtolower($value) !== 'utf-8') {
$this->fatalError($v, 'declare(encoding="' . $value . '") is not supported, only UTF-8 is supported');
}
} elseif ($key === 'strict_types') {
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');
}
}
}
protected function parseNamespace(Node\Stmt\Namespace_ $node): string
{
$ns = $node->name ? $this->parseIdentifier($node->name) : '';

Loading…
Cancel
Save