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

improve is_subclass_of type checks #1321

Merged
merged 12 commits into from
May 17, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Type/Php/IsSubclassOfFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\Generic\GenericClassStringType;
use function count;
use function strtolower;

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

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($objectOrClassType instanceof GenericClassStringType && $classType instanceof GenericClassStringType) {
Copy link
Contributor Author

@staabm staabm May 16, 2022

Choose a reason for hiding this comment

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

obviously this is not a great fix, but with this PR we fix the existing/reported false-positives.
proper support for generic-class-string in is_subclass_of could be added later.. since there are not existing tests for this case, this should be fine for now

return new SpecifiedTypes([], []);
}

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, false),
Expand Down
10 changes: 9 additions & 1 deletion stubs/core.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

/**
* @param ($allow_string is false ? object : object|string) $object_or_class
* @param class-string $class
Copy link
Contributor Author

@staabm staabm May 16, 2022

Choose a reason for hiding this comment

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

changed both, is_a and is_subclass_of $class arg back to string for max compat with 1.6.x

* @param string $class
* @param bool $allow_string
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool)
*/
function is_subclass_of($object_or_class, string $class, $allow_string = true): bool{}

/**
* @param ($allow_string is false ? object : object|string) $object_or_class
* @param string $class
* @param bool $allow_string
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,11 @@ public function testNonEmptySpecifiedString(): void
$this->analyse([__DIR__ . '/data/non-empty-string-impossible-type.php'], []);
}

public function testBug2755(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-2755.php'], []);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-2755.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug2755;

/**
* @param array<class-string<object>> $interfaces
* @param array<class-string<object>> $classes
*/
function foo(array $interfaces, array $classes): void
{
foreach ($interfaces as $interface) {
foreach ($classes as $class) {
if (is_subclass_of($class, $interface)) {

}
}
}
}
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1001,4 +1001,42 @@ public function testBug4371(): void
$this->analyse([__DIR__ . '/data/bug-4371.php'], $errors);
}

public function testIsSubclassAllowString(): void
{
$errors = [
[
'Parameter #1 $object_or_class of function is_subclass_of expects object, string given.',
11,
],
[
'Parameter #1 $object_or_class of function is_subclass_of expects object, string given.',
14,
],
[
'Parameter #1 $object_or_class of function is_subclass_of expects object, string given.',
17,
],
];

if (PHP_VERSION_ID < 80000) {
// php 7.x had different parameter names
$errors = [
[
'Parameter #1 $object_or_string of function is_subclass_of expects object, string given.',
11,
],
[
'Parameter #1 $object_or_string of function is_subclass_of expects object, string given.',
14,
],
[
'Parameter #1 $object_or_string of function is_subclass_of expects object, string given.',
17,
],
];
}

$this->analyse([__DIR__ . '/data/is-subclass-allow-string.php'], $errors);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Functions/data/is-subclass-allow-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace IsSubclassAllowString;

class A {}

class B extends A {}

$b = new B();

if (is_subclass_of(A::class, A::class, false)) { // should error
}

if (is_subclass_of(B::class, B::class, false)) { // should error
}

if (is_subclass_of(B::class, A::class, false)) { // should error
}

if (is_subclass_of($b, A::class)) { // fine
}

if (is_subclass_of($b, B::class)) { // fine
}