Skip to content

Commit

Permalink
Fix static return type in php-8-stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 20, 2024
1 parent f61d324 commit 162f774
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Reflection/SignatureMap/Php8SignatureMapProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function __construct(
private FileTypeMapper $fileTypeMapper,
private PhpVersion $phpVersion,
private InitializerExprTypeResolver $initializerExprTypeResolver,
private ReflectionProviderProvider $reflectionProviderProvider,
)
{
$this->map = new Php8StubsMap($phpVersion->getVersionId());
Expand Down Expand Up @@ -392,14 +394,21 @@ private function getSignature(
$phpDocReturnType = $phpDoc->getReturnTag()->getType();
}
}

$classReflection = null;
if ($className !== null) {
$reflectionProvider = $this->reflectionProviderProvider->getReflectionProvider();
$classReflection = $reflectionProvider->getClass($className);
}

$parameters = [];
$variadic = false;
foreach ($function->getParams() as $param) {
$name = $param->var;
if (!$name instanceof Variable || !is_string($name->name)) {
throw new ShouldNotHappenException();
}
$parameterType = ParserNodeTypeToPHPStanType::resolve($param->type, null);
$parameterType = ParserNodeTypeToPHPStanType::resolve($param->type, $classReflection);
$parameters[] = new ParameterSignature(
$name->name,
$param->default !== null || $param->variadic,
Expand All @@ -417,7 +426,7 @@ private function getSignature(
$variadic = $variadic || $param->variadic;
}

$returnType = ParserNodeTypeToPHPStanType::resolve($function->getReturnType(), null);
$returnType = ParserNodeTypeToPHPStanType::resolve($function->getReturnType(), $classReflection);

return new FunctionSignature(
$parameters,
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12077.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php // lint >= 8.3

namespace Bug12077;

use ReflectionMethod;
use function PHPStan\Testing\assertType;

function (): void {
$methodInfo = ReflectionMethod::createFromMethodName("Exception::getMessage");
assertType(ReflectionMethod::class, $methodInfo);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\PassedByReference;
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -162,6 +163,7 @@ private function createProvider(): Php8SignatureMapProvider
self::getContainer()->getByType(FileTypeMapper::class),
$phpVersion,
self::getContainer()->getByType(InitializerExprTypeResolver::class),
self::getContainer()->getByType(ReflectionProviderProvider::class),
);
}

Expand Down

0 comments on commit 162f774

Please sign in to comment.