Skip to content

Commit

Permalink
Fix #56 - functions in root namespaces should not need slash
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 15, 2017
1 parent ff2e7b1 commit 8836f05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
29 changes: 19 additions & 10 deletions src/Psalm/Checker/Statements/Expression/CallChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -1407,24 +1407,33 @@ public static function checkFunctionArgumentType(
*/
protected static function checkFunctionExists(
StatementsChecker $statements_checker,
$function_id,
&$function_id,
Context $context,
CodeLocation $code_location
) {
$cased_function_id = $function_id;
$function_id = strtolower($function_id);

if (!FunctionChecker::functionExists($function_id, $statements_checker->getFilePath())) {
if (IssueBuffer::accepts(
new UndefinedFunction(
'Function ' . $cased_function_id . ' does not exist',
$code_location
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
$root_function_id = preg_replace('/.*\\\/', '', $function_id);

if ($function_id !== $root_function_id &&
FunctionChecker::functionExists($root_function_id, $statements_checker->getFilePath())
) {
$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
}

return false;
}
return false;
}

return true;
Expand Down
23 changes: 22 additions & 1 deletion tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,33 @@ public function testNamespaced()
$stmts = self::$parser->parse('<?php
namespace A;
function f(int $p) : void {}
/** @return void */
function f(int $p) {}
f(5);
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}

/**
* @return void
*/
public function testNamespacedRootFunctionCall()
{
$stmts = self::$parser->parse('<?php
namespace {
/** @return void */
function foo() { }
}
namespace A\B\C {
foo();
}
');

$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
}
3 changes: 2 additions & 1 deletion tests/NamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function testEmptyNamespace()
{
$stmts = self::$parser->parse('<?php
namespace A {
function foo() : void {
/** @return void */
function foo() {
}
Expand Down

0 comments on commit 8836f05

Please sign in to comment.