Skip to content

Commit

Permalink
Merge pull request #9287 from weirdan/fix-4092
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Feb 14, 2023
2 parents f73fe36 + 39c992c commit 121eef9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,42 @@ public static function handle(
}
}
}

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(
Expand Down
48 changes: 48 additions & 0 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2693,6 +2693,54 @@ function takesArrayShapeWithZeroOrPositiveInt(array $foo): void
',
'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',
],
];
}

Expand Down

0 comments on commit 121eef9

Please sign in to comment.