1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00

Avoid TooManyArguments issues on mock classes

This commit is contained in:
Matthew Brown 2016-10-18 18:27:16 -04:00
parent a8cbb89ce7
commit 900f86507d

View File

@ -3062,12 +3062,18 @@ class StatementsChecker
return; return;
} }
$has_mock = false;
if ($class_type && is_string($stmt->name)) { if ($class_type && is_string($stmt->name)) {
$return_type = null; $return_type = null;
foreach ($class_type->types as $type) { foreach ($class_type->types as $type) {
$absolute_class = $type->value; $absolute_class = $type->value;
$is_mock = self::isMock($absolute_class);
$has_mock = $has_mock || $is_mock;
switch ($absolute_class) { switch ($absolute_class) {
case 'null': case 'null':
if (IssueBuffer::accepts( if (IssueBuffer::accepts(
@ -3118,7 +3124,11 @@ class StatementsChecker
// fall through to default // fall through to default
default: default:
if (!method_exists($absolute_class, '__call') && !self::isMock($absolute_class) && !$this->isPhantomClass($absolute_class)) { if (method_exists($absolute_class, '__call') || $is_mock || $this->isPhantomClass($absolute_class)) {
$return_type = Type::getMixed();
continue;
}
$does_class_exist = ClassLikeChecker::checkAbsoluteClassOrInterface( $does_class_exist = ClassLikeChecker::checkAbsoluteClassOrInterface(
$absolute_class, $absolute_class,
$this->checked_file_name, $this->checked_file_name,
@ -3180,16 +3190,12 @@ class StatementsChecker
$return_type = Type::getMixed(); $return_type = Type::getMixed();
} }
} }
else {
$return_type = Type::getMixed();
}
}
} }
$stmt->inferredType = $return_type; $stmt->inferredType = $return_type;
} }
if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine()) === false) { if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine(), $has_mock) === false) {
return false; return false;
} }
} }
@ -3369,10 +3375,16 @@ class StatementsChecker
return; return;
} }
$has_mock = false;
foreach ($lhs_type->types as $lhs_type_part) { foreach ($lhs_type->types as $lhs_type_part) {
$absolute_class = $lhs_type_part->value; $absolute_class = $lhs_type_part->value;
if (is_string($stmt->name) && !method_exists($absolute_class, '__callStatic') && !self::isMock($absolute_class)) { $is_mock = self::isMock($absolute_class);
$has_mock = $has_mock || $is_mock;
if (is_string($stmt->name) && !method_exists($absolute_class, '__callStatic') && !$is_mock) {
$method_id = $absolute_class . '::' . strtolower($stmt->name); $method_id = $absolute_class . '::' . strtolower($stmt->name);
$cased_method_id = $absolute_class . '::' . $stmt->name; $cased_method_id = $absolute_class . '::' . $stmt->name;
@ -3428,7 +3440,7 @@ class StatementsChecker
} }
} }
if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine()) === false) { if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine(), $has_mock) === false) {
return false; return false;
} }
} }
@ -3540,9 +3552,10 @@ class StatementsChecker
* @param string $method_id * @param string $method_id
* @param Context $context * @param Context $context
* @param int $line_number * @param int $line_number
* @param boolean $is_mock
* @return false|null * @return false|null
*/ */
protected function checkFunctionArguments(array $args, $method_id, Context $context, $line_number) protected function checkFunctionArguments(array $args, $method_id, Context $context, $line_number, $is_mock = false)
{ {
$function_params = null; $function_params = null;
@ -3552,7 +3565,7 @@ class StatementsChecker
$function_params = FunctionLikeChecker::getParamsById($method_id, $args, $this->file_name); $function_params = FunctionLikeChecker::getParamsById($method_id, $args, $this->file_name);
if (strpos($method_id, '::')) { if (strpos($method_id, '::')) {
$is_variadic = MethodChecker::isVariadic($method_id); $is_variadic = $is_mock || MethodChecker::isVariadic($method_id);
} }
else { else {
$is_variadic = FunctionChecker::isVariadic(strtolower($method_id), $this->file_name); $is_variadic = FunctionChecker::isVariadic(strtolower($method_id), $this->file_name);