Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Avoid false error on is_subclass_of #4472

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

Open
VincentLanglet wants to merge 2 commits into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from VincentLanglet:isSubClass
Open
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: 0 additions & 6 deletions phpstan-baseline.neon
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1587,12 +1587,6 @@ parameters:
count: 1
path: src/Type/Php/IsAFunctionTypeSpecifyingExtension.php

-
rawMessage: 'Doing instanceof PHPStan\Type\Generic\GenericClassStringType is error-prone and deprecated. Use Type::isClassStringType() and Type::getClassStringObjectType() instead.'
identifier: phpstanApi.instanceofType
count: 2
path: src/Type/Php/IsSubclassOfFunctionTypeSpecifyingExtension.php

-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.'
identifier: phpstanApi.instanceofType
Expand Down
4 changes: 1 addition & 3 deletions src/Type/Php/IsAFunctionTypeSpecifyingExtension.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));

$resultType = $this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, true);

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($classType->getConstantStrings() === [] && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
if ($resultType === null) {
return new SpecifiedTypes([], []);
}

Expand Down
38 changes: 33 additions & 5 deletions src/Type/Php/IsAFunctionTypeSpecifyingHelper.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\UnionType;
use function array_unique;
use function array_values;
use function in_array;

#[AutowiredService]
final class IsAFunctionTypeSpecifyingHelper
Expand All @@ -26,7 +27,7 @@ public function determineType(
Type $classType,
bool $allowString,
bool $allowSameClass,
): Type
): ?Type
{
$objectOrClassTypeClassNames = $objectOrClassType->getObjectClassNames();
if ($allowString) {
Expand All @@ -36,15 +37,35 @@ public function determineType(
$objectOrClassTypeClassNames = array_values(array_unique($objectOrClassTypeClassNames));
}

return TypeTraverser::map(
$isUncertain = $classType->getConstantStrings() === [];
Copy link
Contributor Author

@VincentLanglet VincentLanglet Oct 24, 2025

Choose a reason for hiding this comment

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

From the original check

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
		if ($classType->getConstantStrings() === [] && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
			return new SpecifiedTypes([], []);
		}


$resultType = TypeTraverser::map(
$classType,
static function (Type $type, callable $traverse) use ($objectOrClassTypeClassNames, $allowString, $allowSameClass): Type {
static function (Type $type, callable $traverse) use ($objectOrClassType, $objectOrClassTypeClassNames, $allowString, $allowSameClass, &$isUncertain): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof ConstantStringType) {
if (!$allowSameClass && $objectOrClassTypeClassNames === [$type->getValue()]) {
return new NeverType();
if (!$allowSameClass) {
// For objectType we cannot be sure since 'Foo' is used for both
// - the Foo class
// - a child of foo class
if (
$objectOrClassTypeClassNames === [$type->getValue()]
&& $objectOrClassType->isString()->yes()
) {
return new NeverType();
}

if (
// For object, as soon as the exact same type is provided
// in the list we cannot be sure of the result
in_array($type->getValue(), $objectOrClassTypeClassNames, true)
// This also occurs for generic class string
|| ($allowString && $objectOrClassTypeClassNames === [] && $objectOrClassType->isSuperTypeOf($type)->yes())
) {
$isUncertain = true;
}
}
if ($allowString) {
return TypeCombinator::union(
Expand Down Expand Up @@ -75,6 +96,13 @@ static function (Type $type, callable $traverse) use ($objectOrClassTypeClassNam
return new ObjectWithoutClassType();
},
);

// prevent false-positives
if ($isUncertain && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
return null;
}

return $resultType;
}

}
10 changes: 1 addition & 9 deletions src/Type/Php/IsSubclassOfFunctionTypeSpecifyingExtension.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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 @@ -45,15 +44,8 @@ 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) {
return new SpecifiedTypes([], []);
}

$resultType = $this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, false);

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($classType->getConstantStrings() === [] && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
if ($resultType === null) {
return new SpecifiedTypes([], []);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-6305.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ class B extends A {}
}

if (is_subclass_of($b, B::class)) {
assertType('*NEVER*', $b);
assertType('Bug6305Types\B', $b);// Could be NEVER
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/is-subclass-of.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function (Bar $a, Bar $b, Bar $c, Bar $d) {
if (is_subclass_of($a, Bar::class)) {
\PHPStan\Testing\assertType('*NEVER*', $a);
\PHPStan\Testing\assertType('IsSubclassOf\Bar', $a);// Can still be a Bar child
}

if (is_subclass_of($b, Foo::class)) {
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,31 @@ public function testBug6305(): void
'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\A\' will always evaluate to true.',
11,
],
// [
Copy link
Contributor Author

@VincentLanglet VincentLanglet Oct 24, 2025

Choose a reason for hiding this comment

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

Currently this is too complicated to be detected since we don't make a difference in ObjectType between the exact class and one of the children.

// 'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\B\' will always evaluate to false.',
// 14,
// ],
]);
}

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

public function testBug13713(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-13713.php'], [
[
'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\B\' will always evaluate to false.',
14,
"Call to function is_subclass_of() with arguments Bug13713\\test, 'stdClass' and false will always evaluate to true.",
12,
],
[
"Call to function is_subclass_of() with arguments class-string<Bug13713\\test>, 'stdClass' and true will always evaluate to true.",
25,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-13713.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types = 1);

namespace Bug13713;

function debug(object $object): void {
if ($object instanceof \stdClass) {
echo var_export(\is_subclass_of($object, \stdClass::class, false), true) . \PHP_EOL;
Copy link
Contributor Author

@VincentLanglet VincentLanglet Oct 24, 2025

Choose a reason for hiding this comment

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

With this PR there is no error here which fix phpstan/phpstan#13713
but I dunno why the bot is not detecting the change...

}
if ($object instanceof test) {
echo var_export(\is_subclass_of($object, \stdClass::class, false), true) . \PHP_EOL;
}
}

class test extends \stdClass {}
debug(new test);

/**
* @param class-string<\stdClass> $stdClass
* @param class-string<test> $test
*/
function debugWithClass(string $stdClass, string $test): void {
echo var_export(\is_subclass_of($stdClass, \stdClass::class, true), true) . \PHP_EOL;
echo var_export(\is_subclass_of($test, \stdClass::class, true), true) . \PHP_EOL;
}

debugWithClass(test::class, test::class);
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-6305b.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug6305b;

class A {}

class B extends A {}

$b = mt_rand(0, 1) === 0 ? new B() : new A();

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

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

$b = mt_rand(0, 1) === 0 ? A::class : B::class;

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

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

AltStyle によって変換されたページ (->オリジナル) /