1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #984 - allow magic methods to be invoked on $this->xxx

This commit is contained in:
Matthew Brown 2018-09-09 11:45:52 -04:00
parent 046025576e
commit 53e265e963
2 changed files with 66 additions and 55 deletions

View File

@ -310,67 +310,65 @@ class MethodCallChecker extends \Psalm\Checker\Statements\Expression\CallChecker
$statements_checker->getSource()
)
) {
if ($var_id !== '$this') {
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
$class_storage = $project_checker->classlike_storage_provider->get($fq_class_name);
if (isset($class_storage->pseudo_methods[$method_name_lc])) {
$has_valid_method_call_type = true;
$existent_method_ids[] = $method_id;
if (isset($class_storage->pseudo_methods[$method_name_lc])) {
$has_valid_method_call_type = true;
$existent_method_ids[] = $method_id;
$pseudo_method_storage = $class_storage->pseudo_methods[$method_name_lc];
$pseudo_method_storage = $class_storage->pseudo_methods[$method_name_lc];
if (self::checkFunctionArguments(
$statements_checker,
$args,
$pseudo_method_storage->params,
$method_id,
$context
) === false) {
return false;
if (self::checkFunctionArguments(
$statements_checker,
$args,
$pseudo_method_storage->params,
$method_id,
$context
) === false) {
return false;
}
$generic_params = [];
if (self::checkFunctionLikeArgumentsMatch(
$statements_checker,
$args,
null,
$pseudo_method_storage->params,
$pseudo_method_storage,
null,
$generic_params,
$code_location,
$context
) === false) {
return false;
}
if ($pseudo_method_storage->return_type) {
$return_type_candidate = clone $pseudo_method_storage->return_type;
if (!$return_type) {
$return_type = $return_type_candidate;
} else {
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
}
$generic_params = [];
continue;
}
} else {
if (self::checkFunctionArguments(
$statements_checker,
$args,
null,
null,
$context
) === false) {
return false;
}
if (self::checkFunctionLikeArgumentsMatch(
$statements_checker,
$args,
null,
$pseudo_method_storage->params,
$pseudo_method_storage,
null,
$generic_params,
$code_location,
$context
) === false) {
return false;
}
if ($pseudo_method_storage->return_type) {
$return_type_candidate = clone $pseudo_method_storage->return_type;
if (!$return_type) {
$return_type = $return_type_candidate;
} else {
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
}
continue;
}
} else {
if (self::checkFunctionArguments(
$statements_checker,
$args,
null,
null,
$context
) === false) {
return false;
}
if ($class_storage->sealed_methods) {
$non_existent_method_ids[] = $method_id;
continue;
}
if ($class_storage->sealed_methods) {
$non_existent_method_ids[] = $method_id;
continue;
}
}

View File

@ -231,6 +231,19 @@ class MagicMethodAnnotationTest extends TestCase
public function __call(string $s) {}
}',
],
'magicMethodInternalCall' => [
'<?php
/**
* @method I[] work()
*/
class I {
function __call(string $method, array $args) { return [new I, new I]; }
function zugzug(): void {
echo count($this->work());
}
}',
],
];
}