feat(php): 添加命名空间常量并改进魔术方法检测器

- 在关键字列表中添加 'namespace' 常量
- 将魔术方法名称转换为小写进行比较以提高匹配准确性
- 更新 __toString 方法返回类型检查逻辑
- 更新 __serialize 方法返回类型检查逻辑
- 改进魔术方法参数和返回类型的验证机制
- 增强魔术方法检测的大小写不敏感性
pull/1/head
韩天峰 5 months ago
parent e230e66164
commit 2838053e4f
  1. 1
      src/Php/Constants.php
  2. 22
      src/Php/MagicMethodDetector.php

@ -52,6 +52,7 @@ class Constants
'static', 'static',
'pipe', 'pipe',
'template', 'template',
'namespace',
'errno', // Linux error code 'errno', // Linux error code
]; ];

@ -18,44 +18,46 @@ trait MagicMethodDetector
$argInfoList = $methodDef->functionDef->argInfoList; $argInfoList = $methodDef->functionDef->argInfoList;
$returnType = $methodDef->functionDef->returnType; $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) { if (count($argInfoList) != 2) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 2 arguments"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 2 arguments");
} }
if (!$this->checkArgType($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"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take string as argument");
} }
} elseif ($name == '__get') { } elseif ($nameLower == '__get') {
if (count($argInfoList) != 1) { if (count($argInfoList) != 1) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument");
} }
} elseif ($name == '__toString') { } elseif ($nameLower == '__tostring') {
if (!$returnType or $returnType != self::TYPE_STR) { if ($returnType != self::TYPE_VAR and $returnType != self::TYPE_STR) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return string"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return string");
} }
} elseif ($name == '__serialize') { } elseif ($nameLower == '__serialize') {
if (!$returnType or $returnType != self::TYPE_ARRAY) { if ($returnType != self::TYPE_VAR and $returnType != self::TYPE_ARRAY) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array");
} }
} elseif ($name == '__unserialize') { } elseif ($nameLower == '__unserialize') {
if (count($argInfoList) != 1) { if (count($argInfoList) != 1) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument");
} elseif (!$argInfoList[0]->type or $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"); $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) { if (count($argInfoList) != 1) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must take exactly 1 argument");
} }
if (!$argInfoList[0]->type or $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"); $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) { if (!$returnType or $returnType != self::TYPE_ARRAY) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array");
} }
} }
} elseif ($name == '__debugInfo') { } elseif ($nameLower == '__debuginfo') {
if (!$returnType or $returnType != self::TYPE_ARRAY) { if (!$returnType or $returnType != self::TYPE_ARRAY) {
$this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array"); $this->fatalError($v, 'Method ' . $this->class . "::{$name}() must return array");
} }

Loading…
Cancel
Save