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"); } }