1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Forbid non-null defaults for callable parameters

Fixes vimeo/psalm#3284
This commit is contained in:
Bruce Weirdan 2023-02-16 01:28:47 -04:00
parent f70f6517a7
commit 1c2bc49838
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
3 changed files with 29 additions and 0 deletions

View File

@ -1256,6 +1256,21 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
),
);
}
if ($default_type
&& !$default_type->isNull()
&& $param_type->isSingleAndMaybeNullable()
&& $param_type->getCallableTypes()
) {
IssueBuffer::maybeAdd(
new InvalidParamDefault(
'Default value type for ' . $param_type->getId() . ' argument ' . ($offset + 1)
. ' of method ' . $cased_method_id
. ' can only be null, ' . $default_type->getId() . ' specified',
$function_param->type_location,
),
);
}
}
if ($has_template_types) {

View File

@ -2741,6 +2741,12 @@ class FunctionCallTest extends TestCase
',
'error_message' => 'RedundantFunctionCall',
],
'incorrectCallableParamDefault' => [
'code' => '<?php
function foo(callable $_a = "strlen"): void {}
',
'error_message' => 'InvalidParamDefault',
],
];
}

View File

@ -1562,6 +1562,14 @@ class MethodCallTest extends TestCase
}',
'error_message' => 'UndefinedMethod',
],
'incorrectCallableParamDefault' => [
'code' => '<?php
class A {
public function foo(callable $_a = "strlen"): void {}
}
',
'error_message' => 'InvalidParamDefault',
],
];
}
}