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 1 commit
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
Next Next commit
Avoid false error on is_subclass_of
  • Loading branch information
VincentLanglet committed Oct 24, 2025
commit f2663f1231e14264c291a13a56a6c21c1cb94e89
9 changes: 7 additions & 2 deletions src/Type/Php/IsAFunctionTypeSpecifyingHelper.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ public function determineType(

return 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): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
if ($type instanceof ConstantStringType) {
if (!$allowSameClass && $objectOrClassTypeClassNames === [$type->getValue()]) {
return new NeverType();
// For objectType we cannot be sure since 'Foo' is used for both
// - the Foo class
// - a child of foo class
if ($objectOrClassType->isString()->yes()) {
return new NeverType();
}
}
if ($allowString) {
return TypeCombinator::union(
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$resultType = $this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, false);

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($classType->getConstantStrings() === [] && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
if ($resultType->isSuperTypeOf($objectOrClassType)->yes()) {
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
Copy link
Contributor

@staabm staabm Oct 29, 2025
edited
Loading

Choose a reason for hiding this comment

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

IIRC - as we have $b = new B(); we can make this NEVER, as its implicitly final

Copy link
Contributor Author

@VincentLanglet VincentLanglet Oct 29, 2025

Choose a reason for hiding this comment

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

I agree that here it could be never.

But correct me if I'm wrong, currently in the type system we cannot make a difference between

$b = new B();

and

function getB(): B {}
$b = getB();

no ?

Therefor, currently I feel like it's better to

Rather than the opposite.

Copy link
Contributor

@staabm staabm Oct 29, 2025
edited
Loading

Choose a reason for hiding this comment

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

is it implemented in hasFinalByKeywordOverride with 3019d39 ?

}
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 @@ -389,17 +389,29 @@ public function testBug6305(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-6305.php'], [
[
'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\A\' will always evaluate to true.',
11,
],
[
'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 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'], []);
}

public function testBug6698(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
14 changes: 14 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,14 @@
<?php

declare(strict_types = 1);

namespace Bug13713;

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

class test extends \stdClass {}
debug(new test);
35 changes: 35 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,35 @@
<?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, A::class)) {
echo 'x';
}
}

if (is_subclass_of($b, B::class)) {
if (is_subclass_of($b, B::class)) {
echo 'y';
}
}

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

if (is_subclass_of($b, A::class)) {
if (is_subclass_of($b, A::class)) {
echo 'x';
}
}

if (is_subclass_of($b, B::class)) {
if (is_subclass_of($b, B::class)) {
echo 'y';
}
}
Loading

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