Skip to content

use conditional parameter for is_a() via stub #1306

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

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
use function strtolower;

/**
* @implements Rule<Node\Expr\FuncCall>
Expand Down Expand Up @@ -35,9 +34,6 @@ public function processNode(Node $node, Scope $scope): array
}

$functionName = (string) $node->name;
if (strtolower($functionName) === 'is_a') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because I dropped this early-return, I had to compensate a few false positives in the IsAFunctionTypeSpecifyingExtension - added a few early returns.

return [];
}
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node);
if ($isAlways === null) {
return [];
Expand Down
13 changes: 13 additions & 0 deletions src/Type/Php/IsAFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\MixedType;
use function count;
use function strtolower;

Expand Down Expand Up @@ -42,6 +45,16 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false);
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));

// early exit in some cases, so we don't create false positives via IsAFunctionTypeSpecifyingHelper
if ($classType instanceof ClassStringType && !$classType instanceof GenericClassStringType) {
Copy link
Contributor Author

@staabm staabm May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

especially this condition is pretty ugly .. would love feedback on how to improve it.

at first I only had the $classType instanceof ClassStringType part, but later on needed to add !$classType instanceof GenericClassStringType to prevent regressions of existing tests

if ($objectOrClassType->isString()->yes() && $allowString) {
return new SpecifiedTypes([], []);
}
if (!$objectOrClassType->isString()->yes() && !$objectOrClassType instanceof MixedType) {
return new SpecifiedTypes([], []);
}
}

if (!$classType instanceof ConstantStringType && !$context->truthy()) {
return new SpecifiedTypes([], []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
'in_array',
'is_numeric',
'is_int',
'is_a',
'is_array',
'is_bool',
'is_callable',
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/pr-1244.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7144.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7144-composer-integration.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4371.php');
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4371.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Bug4371;

use function PHPStan\Testing\assertType;
use Exception;

class HelloWorld
{
/**
* @param Exception $exception
* @param class-string $classString
*/
public function sayHello(Exception $exception, $classString): void
{
assertType('bool', is_a($exception, $classString));
assertType('bool', is_a($exception, $classString, true));
assertType('bool', is_a($exception, $classString, false));

$exceptionClass = get_class($exception);
assertType('false', is_a($exceptionClass, $classString));
assertType('bool', is_a($exceptionClass, $classString, true));
assertType('false', is_a($exceptionClass, $classString, false));
Copy link
Contributor Author

@staabm staabm May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with 3e955af I dropped the conditional return type stub again, as I am no longer sure its worth to report about string vs. class-string type errors for $class.. might be tedious.

since we already have extensions for is_a in place the stub itself was nevertheless only partly usefull, I think.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function testImpossibleCheckTypeFunctionCall(): void
'Call to function is_int() with string will always evaluate to false.',
31,
],
[
'Call to function is_a() with arguments \'Foo\', \'Throwable\' and true will always evaluate to false.',
35,
],
[
'Call to function is_callable() with array<int> will always evaluate to false.',
44,
Expand Down Expand Up @@ -248,6 +252,10 @@ public function testImpossibleCheckTypeFunctionCallWithoutAlwaysTrue(): void
'Call to function is_int() with string will always evaluate to false.',
31,
],
[
'Call to function is_a() with arguments \'Foo\', \'Throwable\' and true will always evaluate to false.',
35,
],
[
'Call to function is_callable() with array<int> will always evaluate to false.',
44,
Expand Down Expand Up @@ -535,4 +543,20 @@ public function testNonEmptySpecifiedString(): void
$this->analyse([__DIR__ . '/data/non-empty-string-impossible-type.php'], []);
}

public function testBug4371(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-4371.php'], [
[
"Call to function is_a() with 'Bug4371\\\\Bar' and 'Bug4371\\\\Foo' will always evaluate to false.",
14,
],
[
"Call to function is_a() with arguments 'Bug4371\\\\Bar', 'Bug4371\\\\Foo' and false will always evaluate to false.",
21,
],
]);
}

}
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-4371.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Bug4371;

class Foo {
}

class Bar extends Foo {

}

class HalloWorld {
public function doFoo() {
if(is_a(Bar::class, Foo::class)) { // should be reported
echo "This will never be true";
} else {
echo "NO";
}
}
public function doBar() {
if(is_a(Bar::class, Foo::class, false)) { // should be reported
echo "This will never be true";
} else {
echo "NO";
}
}
}

20 changes: 19 additions & 1 deletion tests/PHPStan/Rules/Comparison/data/check-type-function-call.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function doFoo(

}
$className = 'Foo';
if (is_a($className, \Throwable::class, true)) { // should be fine
if (is_a($className, \Throwable::class, true)) { // always false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is no longer considered "fine" because Foo is not a subclass of Throwable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But also please test Foo::class (which is different thanks to namespace resolution), the class isn't final so a subclass might implement Throwable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm I guess you mean I should test class-string<Foo> as a parameter not Foo::class.. at least thats what I did for now.. see added tests at the end of this file


}
if (is_array($callable)) {
Expand Down Expand Up @@ -860,3 +860,21 @@ public function doFoo(int $i, array $is): void
}

}

class NonFinalSubclassMightImplementInterface {
/**
* @param class-string<Foo> $classString
*/
public function doFoo($classString, Foo $obj): void {
if (is_a($classString, \Throwable::class, true)) { // should be fine

}

if (is_a($obj, \Throwable::class, true)) { // should be fine

}
if (is_a($obj, \Throwable::class)) { // should be fine

}
}
}