refactor(php): 优化魔术方法检测器的参数验证逻辑

- 添加 checkArgType 方法用于统一处理参数类型检查
- 将 argInfoList 和 returnType 提取为局部变量以提高可读性
- 使用新辅助方法简化各个魔术方法的参数验证逻辑
- 减少重复代码并提高类型检查的一致性
pull/1/head
韩天峰 5 months ago
parent 41e46c2479
commit 6a48de1321
  1. 33
      src/Php/MagicMethodDetector.php

@ -13,47 +13,58 @@ use PhpParser\NodeAbstract;
trait MagicMethodDetector
{
protected function checkArgType(string $givenType, string $expectType, bool $canBeVar = true): bool
{
if ($canBeVar and $givenType == self::TYPE_VAR) {
return true;
}
return $givenType == $expectType;
}
public function checkRequiredArgNum(string $name, MethodDef $methodDef, NodeAbstract $v): void
{
$argInfoList = $methodDef->functionDef->argInfoList;
$returnType = $methodDef->functionDef->returnType;
if ($name == '__call' or $name == '__callStatic' or $name == '__set') {
if (count($methodDef->functionDef->argInfoList) != 2) {
if (count($argInfoList) != 2) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 2 arguments");
}
if ($methodDef->functionDef->argInfoList[0]->type and $methodDef->functionDef->argInfoList[0]->type != self::TYPE_STR) {
if (!$this->checkArgType($argInfoList[0]->type, self::TYPE_STR)) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take string as argument");
}
} elseif ($name == '__get') {
if (count($methodDef->functionDef->argInfoList) != 1) {
if (count($argInfoList) != 1) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument");
}
} elseif ($name == '__toString') {
if (!$methodDef->functionDef->returnType or $methodDef->functionDef->returnType != self::TYPE_STR) {
if (!$returnType or $returnType != self::TYPE_STR) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return string");
}
} elseif ($name == '__serialize') {
if (!$methodDef->functionDef->returnType or $methodDef->functionDef->returnType != self::TYPE_ARRAY) {
if (!$returnType or $returnType != self::TYPE_ARRAY) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array");
}
} elseif ($name == '__unserialize') {
if (count($methodDef->functionDef->argInfoList) != 1) {
if (count($argInfoList) != 1) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument");
} elseif (!$methodDef->functionDef->argInfoList[0]->type or $methodDef->functionDef->argInfoList[0]->type != self::TYPE_ARRAY) {
} elseif (!$argInfoList[0]->type or $argInfoList[0]->type != self::TYPE_ARRAY) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take array as argument");
}
} elseif ($name == '__isset' or $name == '__unset' or $name == '__set_state') {
if (count($methodDef->functionDef->argInfoList) != 1) {
if (count($argInfoList) != 1) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument");
}
if (!$methodDef->functionDef->argInfoList[0]->type or $methodDef->functionDef->argInfoList[0]->type != self::TYPE_STR) {
if (!$argInfoList[0]->type or $argInfoList[0]->type != self::TYPE_STR) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take string as argument");
}
if ($name == '__set_state') {
if (!$methodDef->functionDef->returnType or $methodDef->functionDef->returnType != self::TYPE_ARRAY) {
if (!$returnType or $returnType != self::TYPE_ARRAY) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array");
}
}
} elseif ($name == '__debugInfo') {
if (!$methodDef->functionDef->returnType or $methodDef->functionDef->returnType != self::TYPE_ARRAY) {
if (!$returnType or $returnType != self::TYPE_ARRAY) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array");
}
}

Loading…
Cancel
Save