1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add a check for duplicated params

This commit is contained in:
Matthew Brown 2017-01-02 01:20:47 -05:00
parent 998ddb11ef
commit 4c3e714253
6 changed files with 64 additions and 0 deletions

View File

@ -74,6 +74,7 @@
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ContinueOutsideLoop" type="IssueHandlerType" minOccurs="0" />
<xs:element name="DeprecatedMethod" type="IssueHandlerType" minOccurs="0" />
<xs:element name="DuplicateParam" type="IssueHandlerType" minOccurs="0" />
<xs:element name="FailedTypeResolution" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ForbiddenCode" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ImplicitToStringCast" type="IssueHandlerType" minOccurs="0" />

View File

@ -7,6 +7,7 @@ use Psalm\Config;
use Psalm\EffectsAnalyser;
use Psalm\Exception\DocblockParseException;
use Psalm\FunctionLikeParameter;
use Psalm\Issue\DuplicateParam;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidReturnType;
use Psalm\IssueBuffer;
@ -210,6 +211,19 @@ class FunctionChecker extends FunctionLikeChecker
);
$storage->params[] = $param_array;
if (isset($function_param_names[$param->name])) {
if (IssueBuffer::accepts(
new DuplicateParam(
'Duplicate param $' . $param->name . ' in docblock for ' . $this->function->name,
new CodeLocation($this, $param, true)
),
$this->suppressed_issues
)) {
return false;
}
}
$function_param_names[$param->name] = $param_array->type;
}

View File

@ -12,6 +12,7 @@ use Psalm\Context;
use Psalm\EffectsAnalyser;
use Psalm\Exception\DocblockParseException;
use Psalm\FunctionLikeParameter;
use Psalm\Issue\DuplicateParam;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidParamDefault;
use Psalm\Issue\InvalidReturnType;
@ -225,6 +226,19 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
);
$function_params[] = $param_array;
if (isset($function_param_names[$param->name])) {
if (IssueBuffer::accepts(
new DuplicateParam(
'Duplicate param $' . $param->name . ' in closure docblock',
new CodeLocation($this, $param, true)
),
$this->suppressed_issues
)) {
return false;
}
}
$function_param_names[$param->name] = $param_array->type;
}

View File

@ -6,6 +6,7 @@ use Psalm\CodeLocation;
use Psalm\Config;
use Psalm\Exception\DocblockParseException;
use Psalm\Issue\DeprecatedMethod;
use Psalm\Issue\DuplicateParam;
use Psalm\Issue\InaccessibleMethod;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidStaticInvocation;
@ -320,6 +321,19 @@ class MethodChecker extends FunctionLikeChecker
);
$storage->params[] = $param_array;
if (isset($function_param_names[$param->name])) {
if (IssueBuffer::accepts(
new DuplicateParam(
'Duplicate param $' . $param->name . ' in docblock for ' . $cased_method_id,
new CodeLocation($this, $param, true)
),
$this->suppressed_issues
)) {
return false;
}
}
$method_param_names[$param->name] = $param_array->type;
}

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class DuplicateParam extends CodeError
{
}

View File

@ -141,4 +141,19 @@ class FunctionCallTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage DuplicateParam
*/
public function testDuplicateParam()
{
$stmts = self::$parser->parse('<?php
function f($p, $p) {}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
}