From 2838053e4f92bda6f94589781f1446b4f56486c0 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 25 Mar 2026 18:40:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E5=B8=B8=E9=87=8F=E5=B9=B6=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E9=AD=94=E6=9C=AF=E6=96=B9=E6=B3=95=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在关键字列表中添加 'namespace' 常量 - 将魔术方法名称转换为小写进行比较以提高匹配准确性 - 更新 __toString 方法返回类型检查逻辑 - 更新 __serialize 方法返回类型检查逻辑 - 改进魔术方法参数和返回类型的验证机制 - 增强魔术方法检测的大小写不敏感性 --- src/Php/Constants.php | 1 + src/Php/MagicMethodDetector.php | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Php/Constants.php b/src/Php/Constants.php index 5587a994..2fca7b6f 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -52,6 +52,7 @@ class Constants 'static', 'pipe', 'template', + 'namespace', 'errno', // Linux error code ]; diff --git a/src/Php/MagicMethodDetector.php b/src/Php/MagicMethodDetector.php index 0165c3cc..0078d669 100644 --- a/src/Php/MagicMethodDetector.php +++ b/src/Php/MagicMethodDetector.php @@ -18,44 +18,46 @@ trait MagicMethodDetector $argInfoList = $methodDef->functionDef->argInfoList; $returnType = $methodDef->functionDef->returnType; - if ($name == '__call' or $name == '__callStatic' or $name == '__set') { + $nameLower = strtolower($name); + + if ($nameLower == '__call' or $nameLower == '__callstatic' or $nameLower == '__set') { if (count($argInfoList) != 2) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 2 arguments"); } if (!$this->checkArgType($argInfoList[0]->type, self::TYPE_STR)) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take string as argument"); } - } elseif ($name == '__get') { + } elseif ($nameLower == '__get') { if (count($argInfoList) != 1) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument"); } - } elseif ($name == '__toString') { - if (!$returnType or $returnType != self::TYPE_STR) { + } elseif ($nameLower == '__tostring') { + if ($returnType != self::TYPE_VAR and $returnType != self::TYPE_STR) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return string"); } - } elseif ($name == '__serialize') { - if (!$returnType or $returnType != self::TYPE_ARRAY) { + } elseif ($nameLower == '__serialize') { + if ($returnType != self::TYPE_VAR and $returnType != self::TYPE_ARRAY) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array"); } - } elseif ($name == '__unserialize') { + } elseif ($nameLower == '__unserialize') { if (count($argInfoList) != 1) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument"); } 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') { + } elseif ($nameLower == '__isset' or $nameLower == '__unset' or $nameLower == '__set_state') { if (count($argInfoList) != 1) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument"); } 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 ($nameLower == '__set_state') { if (!$returnType or $returnType != self::TYPE_ARRAY) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array"); } } - } elseif ($name == '__debugInfo') { + } elseif ($nameLower == '__debuginfo') { if (!$returnType or $returnType != self::TYPE_ARRAY) { $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array"); }