From 6a48de132151899966a71b723abeaac67ebc733b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 17 Mar 2026 17:35:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E9=AD=94?= =?UTF-8?q?=E6=9C=AF=E6=96=B9=E6=B3=95=E6=A3=80=E6=B5=8B=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E9=AA=8C=E8=AF=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 checkArgType 方法用于统一处理参数类型检查 - 将 argInfoList 和 returnType 提取为局部变量以提高可读性 - 使用新辅助方法简化各个魔术方法的参数验证逻辑 - 减少重复代码并提高类型检查的一致性 --- src/Php/MagicMethodDetector.php | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Php/MagicMethodDetector.php b/src/Php/MagicMethodDetector.php index 46c95754..a6863257 100644 --- a/src/Php/MagicMethodDetector.php +++ b/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"); } }