1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Check method defaults to see whether they match

This commit is contained in:
Matthew Brown 2016-12-30 23:40:32 -05:00
parent a524ca8184
commit ee4a3882a7
5 changed files with 47 additions and 2 deletions

View File

@ -84,6 +84,7 @@
<xs:element name="InvalidArrayAccess" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidArrayAssignment" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidClass" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidParamDefault" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidDocblock" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidGlobal" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidIterator" type="IssueHandlerType" minOccurs="0" />

View File

@ -76,7 +76,7 @@ class FunctionChecker extends FunctionLikeChecker
/**
* @param string $function_id
* @param string $file_path
* @return array<FunctionLikeParameter>
* @return array<int, FunctionLikeParameter>
*/
public static function getParams($function_id, $file_path)
{

View File

@ -13,6 +13,7 @@ use Psalm\EffectsAnalyser;
use Psalm\Exception\DocblockParseException;
use Psalm\FunctionLikeParameter;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidParamDefault;
use Psalm\Issue\InvalidReturnType;
use Psalm\Issue\InvalidToString;
use Psalm\Issue\MethodSignatureMismatch;
@ -340,7 +341,7 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
]);
}
foreach ($function_params as $function_param) {
foreach ($function_params as $offset => $function_param) {
$param_type = ExpressionChecker::fleshOutTypes(
clone $function_param->type,
[],
@ -352,6 +353,24 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
throw new \UnexpectedValueException('We should know where this code is');
}
$parser_param = $this->function->getParams()[$offset];
if ($parser_param->default) {
$default_type = StatementsChecker::getSimpleType($parser_param->default);
if ($default_type && !TypeChecker::isContainedBy($default_type, $param_type)) {
if (IssueBuffer::accepts(
new InvalidParamDefault(
'Default value for argument ' . ($offset + 1) . ' of method ' . $this->getMethodId() .
' does not match the given type ' . $param_type,
$function_param->code_location
)
)) {
return false;
}
}
}
foreach ($param_type->types as $atomic_type) {
if ($atomic_type->isObjectType()
&& !$atomic_type->isObject()

View File

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

View File

@ -94,4 +94,23 @@ class MethodSignatureTest extends PHPUnit_Framework_TestCase
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidParamDefault
*/
public function testInvalidDefault()
{
$stmts = self::$parser->parse('<?php
class A {
public function fooFoo(int $a = "hello") : void {
}
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
}