Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit issues for calls to is_a(string, class-string, false) #9287

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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