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:
parent
f70f6517a7
commit
1c2bc49838
@ -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) {
|
||||
|
@ -2741,6 +2741,12 @@ class FunctionCallTest extends TestCase
|
||||
',
|
||||
'error_message' => 'RedundantFunctionCall',
|
||||
],
|
||||
'incorrectCallableParamDefault' => [
|
||||
'code' => '<?php
|
||||
function foo(callable $_a = "strlen"): void {}
|
||||
',
|
||||
'error_message' => 'InvalidParamDefault',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user