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

Fix namespaced function calls

This commit is contained in:
Matthew Brown 2017-01-12 09:42:24 -05:00
parent 91328838e2
commit c20f31855c
3 changed files with 51 additions and 5 deletions

View File

@ -631,6 +631,39 @@ class FunctionChecker extends FunctionLikeChecker
return isset(self::getCallMap()[strtolower($key)]);
}
/**
* @param string $function_name
* @param StatementsSource $source
* @return string
*/
public static function getFQFunctionNameFromString($function_name, StatementsSource $source)
{
if (empty($function_name)) {
throw new \InvalidArgumentException('$function_name cannot be empty');
}
if ($function_name[0] === '\\') {
return substr($function_name, 1);
}
$imported_namespaces = $source->getAliasedFunctions();
if (strpos($function_name, '\\') !== false) {
$function_name_parts = explode('\\', $function_name);
$first_namespace = array_shift($function_name_parts);
if (isset($imported_namespaces[strtolower($first_namespace)])) {
return $imported_namespaces[strtolower($first_namespace)] . '\\' . implode('\\', $function_name_parts);
}
} elseif (isset($imported_namespaces[strtolower($function_name)])) {
return $imported_namespaces[strtolower($function_name)];
}
$namespace = $source->getNamespace();
return ($namespace ? $namespace . '\\' : '') . $function_name;
}
/**
* @return void
*/

View File

@ -151,20 +151,19 @@ class CallChecker
}
} else {
$method_id = implode('\\', $stmt->name->parts);
$aliased_functions = $statements_checker->getAliasedFunctions();
$in_call_map = FunctionChecker::inCallMap($method_id);
if (isset($aliased_functions[strtolower($method_id)])) {
$method_id = $aliased_functions[strtolower($method_id)];
if (!$in_call_map && !$stmt->name instanceof PhpParser\Node\Name\FullyQualified) {
$method_id = FunctionChecker::getFQFunctionNameFromString($method_id, $statements_checker);
}
if ($context->self) {
//$method_id = $statements_checker->getFQCLN() . '::' . $method_id;
}
$in_call_map = FunctionChecker::inCallMap($method_id);
if (!$in_call_map &&
self::checkFunctionExists($statements_checker, $method_id, $context, $code_location) === false
) {

View File

@ -189,4 +189,18 @@ class FunctionCallTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
public function testNamespaced()
{
$stmts = self::$parser->parse('<?php
namespace A;
function f(int $p) : void {}
f(5);
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
}