Skip to content
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
8 changes: 7 additions & 1 deletion src/Type/Accessory/HasMethodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Accessory;

use Closure;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
Expand All @@ -17,6 +18,7 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonGenericTypeTrait;
Expand Down Expand Up @@ -83,7 +85,11 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
return $otherType->isSuperTypeOf($this);
}

if ($this->isCallable()->yes() && $otherType->isCallable()->yes()) {
if (
$this->isCallable()->yes()
&& $otherType->isCallable()->yes()
&& !(new ObjectType(Closure::class))->isSuperTypeOf($otherType)->yes()
) {
return IsSuperTypeOfResult::createYes();
}

Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,17 @@ public function testBug13469(): void
]);
}

public static function dataBug13975(): iterable
{
yield [__DIR__ . '/data/bug-13975-1.php'];
yield [__DIR__ . '/data/bug-13975-2.php'];
}

#[DataProvider('dataBug13975')]
public function testBug13975(string $file): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([$file], []);
}

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

namespace Bug139751;

function foo(): callable
{
return new class () {
public function __invoke(): void
{
}
};
}

$foo = foo();

if (\is_object($foo) && method_exists($foo, '__invoke') && !$foo instanceof \Closure) {
echo 'true';
}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-13975-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Bug139752;

class X {}

function foo(): callable
{
return new class () {
public function __invoke(): void
{
}
};
}

$foo = foo();

$class = \Closure::class;
if (rand(0, 1)) {
$class = X::class;
}

if (\is_object($foo) && method_exists($foo, '__invoke') && !$foo instanceof $class) {
echo 'true';
}
Loading