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

Merge pull request #9287 from weirdan/fix-4092

This commit is contained in:
Bruce Weirdan 2023-02-14 15:53:07 -04:00 committed by GitHub
commit 121eef9b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 0 deletions

View File

@ -514,6 +514,42 @@ class NamedFunctionCallHandler
}
}
}
if ($first_arg
&& $function_id === 'is_a'
// assertion reconsiler already emits relevant (but different) issues
&& !$context->inside_conditional
) {
$first_arg_type = $statements_analyzer->node_data->getType($first_arg->value);
if ($first_arg_type && $first_arg_type->isString()) {
$third_arg = $stmt->getArgs()[2] ?? null;
if ($third_arg) {
$third_arg_type = $statements_analyzer->node_data->getType($third_arg->value);
} else {
$third_arg_type = Type::getFalse();
}
if ($third_arg_type
&& $third_arg_type->isSingle()
&& $third_arg_type->isFalse()
) {
if ($first_arg_type->from_docblock) {
IssueBuffer::maybeAdd(new RedundantFunctionCallGivenDocblockType(
'Call to is_a always return false when first argument is string '
. 'unless third argument is true',
new CodeLocation($statements_analyzer, $function_name),
));
} else {
IssueBuffer::maybeAdd(new RedundantFunctionCall(
'Call to is_a always return false when first argument is string '
. 'unless third argument is true',
new CodeLocation($statements_analyzer, $function_name),
));
}
}
}
}
}
private static function handleDependentTypeFunction(

View File

@ -2693,6 +2693,54 @@ class FunctionCallTest extends TestCase
',
'error_message' => 'InvalidArgument',
],
'is_a_withAStringAndNoThirdArg' => [
'code' => '<?php
is_a("Foo", Exception::class);
',
'error_message' => 'RedundantFunctionCall',
],
'is_a_withAStringAndFalseThirdArg' => [
'code' => '<?php
is_a("Foo", Exception::class, false);
',
'error_message' => 'RedundantFunctionCall',
],
'is_a_withAUnionOfStringsAndNoThirdArg' => [
'code' => '<?php
is_a(rand(0, 1) ? "Foo" : "Bar", Exception::class);
',
'error_message' => 'RedundantFunctionCall',
],
'is_a_withAUnionOfStringsAndFalseThirdArg' => [
'code' => '<?php
is_a(rand(0, 1) ? "Foo" : "Bar", Exception::class, false);
',
'error_message' => 'RedundantFunctionCall',
],
'is_a_withAClassStringAndNoThirdArg' => [
'code' => '<?php
is_a(InvalidArgumentException::class, Exception::class);
',
'error_message' => 'RedundantFunctionCall',
],
'is_a_withAClassStringAndFalseThirdArg' => [
'code' => '<?php
is_a(InvalidArgumentException::class, Exception::class, false);
',
'error_message' => 'RedundantFunctionCall',
],
'is_a_withAUnionOfClassStringsAndNoThirdArg' => [
'code' => '<?php
is_a(rand(0, 1) ? InvalidArgumentException::class : RuntimeException::class, Exception::class);
',
'error_message' => 'RedundantFunctionCall',
],
'is_a_withAUnionOfClassStringsAndFalseThirdArg' => [
'code' => '<?php
is_a(rand(0, 1) ? InvalidArgumentException::class : RuntimeException::class, Exception::class, false);
',
'error_message' => 'RedundantFunctionCall',
],
];
}