mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Support callable checks on inline-defined functions
This commit is contained in:
parent
13d98b803d
commit
a7baa7e928
@ -2228,17 +2228,22 @@ class CallChecker
|
||||
) {
|
||||
$function_id = $root_function_id;
|
||||
} else {
|
||||
if (IssueBuffer::accepts(
|
||||
new UndefinedFunction(
|
||||
'Function ' . $cased_function_id . ' does not exist',
|
||||
$code_location
|
||||
),
|
||||
$statements_checker->getSuppressedIssues()
|
||||
)) {
|
||||
// fall through
|
||||
}
|
||||
$existing_function_checkers = $statements_checker->getFunctionCheckers();
|
||||
|
||||
return false;
|
||||
// check whether it was defined inline
|
||||
if (!isset($existing_function_checkers[$function_id])) {
|
||||
if (IssueBuffer::accepts(
|
||||
new UndefinedFunction(
|
||||
'Function ' . $cased_function_id . ' does not exist',
|
||||
$code_location
|
||||
),
|
||||
$statements_checker->getSuppressedIssues()
|
||||
)) {
|
||||
// fall through
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,6 +45,11 @@ class StatementsChecker extends SourceChecker implements StatementsSource
|
||||
*/
|
||||
public static $stub_constants = [];
|
||||
|
||||
/**
|
||||
* @var array<string, FunctionChecker>
|
||||
*/
|
||||
private $function_checkers = [];
|
||||
|
||||
/**
|
||||
* @param StatementsSource $source
|
||||
*/
|
||||
@ -72,13 +77,11 @@ class StatementsChecker extends SourceChecker implements StatementsSource
|
||||
) {
|
||||
$has_returned = false;
|
||||
|
||||
$function_checkers = [];
|
||||
|
||||
// hoist functions to the top
|
||||
foreach ($stmts as $stmt) {
|
||||
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
||||
$function_checker = new FunctionChecker($stmt, $this->source);
|
||||
$function_checkers[$stmt->name] = $function_checker;
|
||||
$this->function_checkers[$stmt->name] = $function_checker;
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,13 +204,13 @@ class StatementsChecker extends SourceChecker implements StatementsSource
|
||||
if (!$project_checker->register_global_functions) {
|
||||
$function_context = new Context($context->self);
|
||||
$function_context->collect_references = $project_checker->collect_references;
|
||||
$function_checkers[$stmt->name]->analyze($function_context, $context);
|
||||
$this->function_checkers[$stmt->name]->analyze($function_context, $context);
|
||||
|
||||
$config = Config::getInstance();
|
||||
|
||||
if ($config->reportIssueInFile('InvalidReturnType', $this->getFilePath())) {
|
||||
/** @var string */
|
||||
$method_id = $function_checkers[$stmt->name]->getMethodId();
|
||||
$method_id = $this->function_checkers[$stmt->name]->getMethodId();
|
||||
|
||||
$function_storage = FunctionChecker::getStorage(
|
||||
$project_checker,
|
||||
@ -218,7 +221,7 @@ class StatementsChecker extends SourceChecker implements StatementsSource
|
||||
$return_type = $function_storage->return_type;
|
||||
$return_type_location = $function_storage->return_type_location;
|
||||
|
||||
$function_checkers[$stmt->name]->verifyReturnType(
|
||||
$this->function_checkers[$stmt->name]->verifyReturnType(
|
||||
false,
|
||||
$return_type,
|
||||
$this->getFQCLN(),
|
||||
@ -1079,6 +1082,14 @@ class StatementsChecker extends SourceChecker implements StatementsSource
|
||||
return $this->file_checker;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, FunctionChecker>
|
||||
*/
|
||||
public function getFunctionCheckers()
|
||||
{
|
||||
return $this->function_checkers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user