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:
parent
a524ca8184
commit
ee4a3882a7
@ -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" />
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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()
|
||||
|
6
src/Psalm/Issue/InvalidParamDefault.php
Normal file
6
src/Psalm/Issue/InvalidParamDefault.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace Psalm\Issue;
|
||||
|
||||
class InvalidParamDefault extends CodeError
|
||||
{
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user