Skip to content

Commit

Permalink
Fix #19 - InvalidDocblock errors have proper casing now
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 19, 2016
1 parent 9a32b5f commit e79dded
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
19 changes: 17 additions & 2 deletions src/Psalm/Checker/FunctionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,29 @@ public static function getFunctionReturnTypeLocation($function_id, $file_name)
return self::$function_return_type_locations[$file_name][$function_id] ?: null;
}

/**
* @param string $function_id
* @param string $file_name
* @return string
*/
public static function getCasedFunctionId($function_id, $file_name)
{
if (!isset(self::$existing_functions[$file_name][$function_id])) {
throw new \InvalidArgumentException('Do not know function ' . $function_id . ' in file ' . $file_name);
}

return self::$existing_functions[$file_name][$function_id];
}

/**
* @param PhpParser\Node\Stmt\Function_ $function
* @param string $file_name
* @return null|false
*/
protected function registerFunction(PhpParser\Node\Stmt\Function_ $function, $file_name)
{
$function_id = ($this->namespace ? strtolower($this->namespace) . '\\' : '') . strtolower($function->name);
$cased_function_id = ($this->namespace ? $this->namespace . '\\' : '') . $function->name;
$function_id = strtolower($cased_function_id);

if (isset(self::$have_registered_function[$file_name][$function_id])) {
return null;
Expand All @@ -204,7 +219,7 @@ protected function registerFunction(PhpParser\Node\Stmt\Function_ $function, $fi
self::$have_registered_function[$file_name][$function_id] = true;

self::$function_namespaces[$file_name][$function_id] = $this->namespace;
self::$existing_functions[$file_name][$function_id] = true;
self::$existing_functions[$file_name][$function_id] = $cased_function_id;

self::$file_function_params[$file_name][$function_id] = [];

Expand Down
12 changes: 10 additions & 2 deletions src/Psalm/Checker/FunctionLikeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,14 @@ protected function improveParamsFromDocblock(
) {
$docblock_param_vars = [];

$method_id = $cased_method_id = $this->getMethodId();

if ($method_id) {
$cased_method_id = $this instanceof MethodChecker
? MethodChecker::getCasedMethodId($method_id)
: FunctionChecker::getCasedFunctionId($method_id, $this->file_name);
}

foreach ($docblock_params as $docblock_param) {
$param_name = $docblock_param['name'];
$line_number = $docblock_param['line_number'];
Expand All @@ -790,7 +798,7 @@ protected function improveParamsFromDocblock(
if (IssueBuffer::accepts(
new InvalidDocblock(
'Parameter $' . $param_name .' does not appear in the argument list for ' .
$this->getMethodId(),
$cased_method_id,
$code_location
)
)) {
Expand Down Expand Up @@ -848,7 +856,7 @@ protected function improveParamsFromDocblock(
if (IssueBuffer::accepts(
new InvalidDocblock(
'Parameter $' . $function_signature_param->name .' does not appear in the docbock for ' .
$this->getMethodId(),
$cased_method_id,
$function_signature_param->code_location
)
)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ function foo(array $bar) : void {

/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidDocblock
* @expectedExceptionMessage InvalidDocblock - somefile.php:3 - Parameter $bar does not appear in the argument list for fooBar
*/
public function testExtraneousDocblockParam()
{
$stmts = self::$parser->parse('<?php
/**
* @param int $bar
*/
function foo() : void {
function fooBar() : void {
}
');

Expand Down

0 comments on commit e79dded

Please sign in to comment.