Skip to content

Commit 38f3906

Browse files
Fix bug in get_debug_type() DynamicFunctionReturnTypeExtension
1 parent f306b6a commit 38f3906

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

Diff for: src/Type/Php/GetDebugTypeFunctionReturnTypeExtension.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,15 @@ private static function resolveOneType(Type $type): Type
6262
// "resources" type+state is skipped since we cannot infer the state
6363

6464
if ($type->isObject()->yes()) {
65-
$classNames = $type->getObjectClassNames();
6665
$reflections = $type->getObjectClassReflections();
67-
6866
$types = [];
69-
foreach ($classNames as $index => $className) {
67+
foreach ($reflections as $reflection) {
7068
// if the class is not final, the actual returned string might be of a child class
71-
if ($reflections[$index]->isFinal() && !$reflections[$index]->isAnonymous()) {
72-
$types[] = new ConstantStringType($className);
69+
if ($reflection->isFinal() && !$reflection->isAnonymous()) {
70+
$types[] = new ConstantStringType($reflection->getName());
7371
}
7472

75-
if ($reflections[$index]->isAnonymous()) { // phpcs:ignore
73+
if ($reflection->isAnonymous()) { // phpcs:ignore
7674
$types[] = new ConstantStringType('class@anonymous');
7775
}
7876
}

Diff for: tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,13 @@ public function testBug10867(): void
13731373
$this->assertNoErrors($errors);
13741374
}
13751375

1376+
public function testBug11147(): void
1377+
{
1378+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-11147.php');
1379+
$this->assertCount(1, $errors);
1380+
$this->assertSame('Method Bug11147\RedisAdapter::createConnection() has invalid return type Bug11147\NonExistentClass.', $errors[0]->getMessage());
1381+
}
1382+
13761383
/**
13771384
* @param string[]|null $allAnalysedFiles
13781385
* @return Error[]

Diff for: tests/PHPStan/Analyser/data/bug-11147.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug11147;
6+
7+
use Exception;
8+
use Redis;
9+
use function get_debug_type;
10+
use function sprintf;
11+
12+
class RedisAdapter {
13+
public static function createConnection(mixed $url): \Redis|NonExistentClass
14+
{
15+
return new \Redis();
16+
}
17+
}
18+
19+
final class RedisFactory
20+
{
21+
public function __invoke(): Redis
22+
{
23+
$connection = RedisAdapter::createConnection($_ENV['REDISCLOUD_URL']);
24+
25+
if (! $connection instanceof Redis) {
26+
throw new Exception(
27+
sprintf('Wrong Redis instance %s expected %s', get_debug_type($connection), Redis::class),
28+
);
29+
}
30+
31+
return $connection;
32+
}
33+
}

0 commit comments

Comments
 (0)