Skip to content

Commit 6d1e13c

Browse files
committed
Fix invokable objects incorrectly labeled as instances of Closure
1 parent 47936f2 commit 6d1e13c

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Type/Accessory/HasMethodType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Type\Accessory;
44

5+
use Closure;
56
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
67
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
78
use PHPStan\Reflection\ClassMemberAccessAnswerer;
@@ -83,7 +84,11 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
8384
return $otherType->isSuperTypeOf($this);
8485
}
8586

86-
if ($this->isCallable()->yes() && $otherType->isCallable()->yes()) {
87+
if (
88+
$this->isCallable()->yes()
89+
&& $otherType->isCallable()->yes()
90+
&& !($otherType->isObject()->yes() && $otherType->isInstanceOf(Closure::class)->yes())
91+
) {
8792
return IsSuperTypeOfResult::createYes();
8893
}
8994

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,12 @@ public function testBug13980(): void
16191619
$this->assertNoErrors($errors);
16201620
}
16211621

1622+
public function testBug13975(): void
1623+
{
1624+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-13975.php');
1625+
$this->assertNoErrors($errors);
1626+
}
1627+
16221628
/**
16231629
* @param string[]|null $allAnalysedFiles
16241630
* @return list<Error>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug13975;
4+
5+
function foo(): callable
6+
{
7+
return new class () {
8+
public function __invoke(): void
9+
{
10+
}
11+
};
12+
}
13+
14+
$foo = foo();
15+
16+
if (\is_object($foo) && method_exists($foo, '__invoke') && !$foo instanceof \Closure) {
17+
echo 'true';
18+
}

0 commit comments

Comments
 (0)