Skip to content
Permalink
Browse files

Fix #1859 - allow function_exists introspection when function exists

  • Loading branch information...
muglug committed Jun 28, 2019
1 parent 6514f20 commit 5943f6036e4889d9d674505311df81e446da858b
Showing with 41 additions and 17 deletions.
  1. +24 −17 src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
  2. +9 −0 tests/ClassStringTest.php
  3. +8 −0 tests/FunctionCallTest.php
@@ -150,8 +150,12 @@ public function analyze(
// hoist functions to the top
foreach ($stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
$function_analyzer = new FunctionAnalyzer($stmt, $this->source);
$this->function_analyzers[strtolower($stmt->name->name)] = $function_analyzer;
try {
$function_analyzer = new FunctionAnalyzer($stmt, $this->source);
$this->function_analyzers[strtolower($stmt->name->name)] = $function_analyzer;
} catch (\UnexpectedValueException $e) {
// do nothing
}
}
}
@@ -542,25 +546,28 @@ function ($line) {
$config = Config::getInstance();
$function_context->collect_references = $codebase->collect_references;
$function_context->collect_exceptions = $config->check_for_throws_docblock;
$this->function_analyzers[$function_id]->analyze($function_context, $context);
if ($config->reportIssueInFile('InvalidReturnType', $this->getFilePath())) {
$method_id = $this->function_analyzers[$function_id]->getMethodId();
if (isset($this->function_analyzers[$function_id])) {
$this->function_analyzers[$function_id]->analyze($function_context, $context);
$function_storage = $codebase->functions->getStorage(
$this,
$method_id
);
if ($config->reportIssueInFile('InvalidReturnType', $this->getFilePath())) {
$method_id = $this->function_analyzers[$function_id]->getMethodId();
$function_storage = $codebase->functions->getStorage(
$this,
$method_id
);
$return_type = $function_storage->return_type;
$return_type_location = $function_storage->return_type_location;
$return_type = $function_storage->return_type;
$return_type_location = $function_storage->return_type_location;
$this->function_analyzers[$function_id]->verifyReturnType(
$this,
$return_type,
$this->getFQCLN(),
$return_type_location
);
$this->function_analyzers[$function_id]->verifyReturnType(
$this,
$return_type,
$this->getFQCLN(),
$return_type_location
);
}
}
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Expression) {
@@ -611,6 +611,15 @@ class A {}
new \RuntimeException();
}'
],
'noCrashWhenClassExistsNegated' => [
'<?php
class A {}
if (!class_exists(A::class)) {
new \RuntimeException();
}'
],
];
}
@@ -1760,6 +1760,14 @@ function takesMixed($f) : void {
$f->bar();
}'
],
'functionExists' => [
'<?php
if (!function_exists("in_array")) {
function in_array($a, $b) {
return true;
}
}'
],
];
}

0 comments on commit 5943f60

Please sign in to comment.
You can’t perform that action at this time.