Skip to content
Merged
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
13 changes: 12 additions & 1 deletion src/Type/Php/GetDebugTypeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use function array_key_first;
use function array_map;
use function count;

Expand All @@ -37,6 +38,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

/**
* @see https://www.php.net/manual/en/function.get-debug-type.php#refsect1-function.get-debug-type-returnvalues
* @see https://github.com/php/php-src/commit/ef0e4478c51540510b67f7781ad240f5e0592ee4
*/
private static function resolveOneType(Type $type): Type
{
Expand Down Expand Up @@ -71,7 +73,16 @@ private static function resolveOneType(Type $type): Type
}

if ($reflection->isAnonymous()) { // phpcs:ignore
$types[] = new ConstantStringType('class@anonymous');
$parentClass = $reflection->getParentClass();
$implementedInterfaces = $reflection->getImmediateInterfaces();
if ($parentClass !== null) {
$types[] = new ConstantStringType($parentClass->getName() . '@anonymous');
} elseif ($implementedInterfaces !== []) {
$firstInterface = $implementedInterfaces[array_key_first($implementedInterfaces)];
$types[] = new ConstantStringType($firstInterface->getName() . '@anonymous');
} else {
$types[] = new ConstantStringType('class@anonymous');
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/nsrt/get-debug-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use function PHPStan\Testing\assertType;

final class A {}
interface B {}
interface C {}
class D {}

/**
* @param double $d
Expand All @@ -18,6 +21,9 @@ function doFoo(bool $b, int $i, float $f, $d, $r, string $s, array $a, $intOrStr
$o = new \stdClass();
$A = new A();
$anonymous = new class {};
$anonymousImplements = new class implements B, C {};
$anonymousExtends = new class extends D {};
$anonymousExtendsImplements = new class extends D implements B, C {};

assertType("'bool'", get_debug_type($b));
assertType("'bool'", get_debug_type(true));
Expand All @@ -35,6 +41,9 @@ function doFoo(bool $b, int $i, float $f, $d, $r, string $s, array $a, $intOrStr
assertType("'int'|'string'", get_debug_type($intOrString));
assertType("'array'|'GetDebugType\\\\A'", get_debug_type($arrayOrObject));
assertType("'class@anonymous'", get_debug_type($anonymous));
assertType("'GetDebugType\\\\B@anonymous'", get_debug_type($anonymousImplements));
assertType("'GetDebugType\\\\D@anonymous'", get_debug_type($anonymousExtends));
assertType("'GetDebugType\\\\D@anonymous'", get_debug_type($anonymousExtendsImplements));
}

/**
Expand Down
Loading