1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #1859 - allow function_exists introspection when function exists

This commit is contained in:
Brown 2019-06-28 10:48:30 -04:00
parent 6514f2084c
commit 5943f6036e
3 changed files with 41 additions and 17 deletions

View File

@ -150,8 +150,12 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
// hoist functions to the top
foreach ($stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
try {
$function_analyzer = new FunctionAnalyzer($stmt, $this->source);
$this->function_analyzers[strtolower($stmt->name->name)] = $function_analyzer;
} catch (\UnexpectedValueException $e) {
// do nothing
}
}
}
@ -542,6 +546,8 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
$config = Config::getInstance();
$function_context->collect_references = $codebase->collect_references;
$function_context->collect_exceptions = $config->check_for_throws_docblock;
if (isset($this->function_analyzers[$function_id])) {
$this->function_analyzers[$function_id]->analyze($function_context, $context);
if ($config->reportIssueInFile('InvalidReturnType', $this->getFilePath())) {
@ -563,6 +569,7 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
);
}
}
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Expression) {
if (ExpressionAnalyzer::analyze($this, $stmt->expr, $context, false, $global_context) === false) {
return false;

View File

@ -611,6 +611,15 @@ class ClassStringTest extends TestCase
new \RuntimeException();
}'
],
'noCrashWhenClassExistsNegated' => [
'<?php
class A {}
if (!class_exists(A::class)) {
new \RuntimeException();
}'
],
];
}

View File

@ -1760,6 +1760,14 @@ class FunctionCallTest extends TestCase
$f->bar();
}'
],
'functionExists' => [
'<?php
if (!function_exists("in_array")) {
function in_array($a, $b) {
return true;
}
}'
],
];
}