Skip to content
Closed
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
14 changes: 12 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\PhpDoc\Tag\VarTag;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
Expand Down Expand Up @@ -267,6 +268,7 @@ public function __construct(
#[AutowiredParameter(ref: '@defaultAnalysisParser')]
private readonly Parser $parser,
private readonly FileTypeMapper $fileTypeMapper,
private readonly StubPhpDocProvider $stubPhpDocProvider,
private readonly PhpVersion $phpVersion,
private readonly PhpDocInheritanceResolver $phpDocInheritanceResolver,
private readonly FileHelper $fileHelper,
Expand Down Expand Up @@ -7446,12 +7448,20 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
return $param->var->name;
}, $node->getParams());
$currentResolvedPhpDoc = null;
if ($docComment !== null) {
if ($class !== null) {
$currentResolvedPhpDoc = $this->stubPhpDocProvider->findMethodPhpDoc(
$class,
$class,
$functionName,
$positionalParameterNames,
);
}
if ($currentResolvedPhpDoc === null && $docComment !== null) {
$currentResolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$file,
$class,
$trait,
$node->name->name,
$functionName,
$docComment,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PHPStan\Node\DeepNodeCloner;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\ClassReflectionFactory;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Rules\DirectRegistry as DirectRuleRegistry;
Expand Down Expand Up @@ -102,6 +103,7 @@ protected function createNodeScopeResolver(): NodeScopeResolver
self::getContainer()->getByType(ParameterOutTypeExtensionProvider::class),
$this->getParser(),
self::getContainer()->getByType(FileTypeMapper::class),
self::getContainer()->getByType(StubPhpDocProvider::class),
self::getContainer()->getByType(PhpVersion::class),
self::getContainer()->getByType(PhpDocInheritanceResolver::class),
self::getContainer()->getByType(FileHelper::class),
Expand Down
2 changes: 2 additions & 0 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Node\InClassNode;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\Reflection\ClassReflectionFactory;
use PHPStan\Reflection\InitializerExprTypeResolver;
Expand Down Expand Up @@ -77,6 +78,7 @@ protected static function createNodeScopeResolver(): NodeScopeResolver
$container->getByType(ParameterOutTypeExtensionProvider::class),
self::getParser(),
$container->getByType(FileTypeMapper::class),
$container->getByType(StubPhpDocProvider::class),
$container->getByType(PhpVersion::class),
$container->getByType(PhpDocInheritanceResolver::class),
$container->getByType(FileHelper::class),
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Parser\RichParser;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\ClassReflectionFactory;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Rules\AlwaysFailRule;
Expand Down Expand Up @@ -818,6 +819,7 @@ private function createAnalyser(): Analyser
$container->getByType(ParameterOutTypeExtensionProvider::class),
$this->getParser(),
$fileTypeMapper,
$container->getByType(StubPhpDocProvider::class),
$container->getByType(PhpVersion::class),
$phpDocInheritanceResolver,
$fileHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Node\DeepNodeCloner;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\ClassReflectionFactory;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Rules\IdentifierRuleError;
Expand Down Expand Up @@ -122,6 +123,7 @@ protected function createNodeScopeResolver(): NodeScopeResolver
self::getContainer()->getByType(ParameterOutTypeExtensionProvider::class),
$this->getParser(),
self::getContainer()->getByType(FileTypeMapper::class),
self::getContainer()->getByType(StubPhpDocProvider::class),
self::getContainer()->getByType(PhpVersion::class),
self::getContainer()->getByType(PhpDocInheritanceResolver::class),
self::getContainer()->getByType(FileHelper::class),
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Node\DeepNodeCloner;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\ClassReflectionFactory;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
Expand Down Expand Up @@ -55,6 +56,7 @@ protected static function createNodeScopeResolver(): NodeScopeResolver
$container->getByType(ParameterOutTypeExtensionProvider::class),
self::getParser(),
$container->getByType(FileTypeMapper::class),
$container->getByType(StubPhpDocProvider::class),
$container->getByType(PhpVersion::class),
$container->getByType(PhpDocInheritanceResolver::class),
$container->getByType(FileHelper::class),
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/StubMethodBodyTypesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class StubMethodBodyTypesTest extends TypeInferenceTestCase
{

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/stub-method-body-types.php');
}

/**
* @param mixed ...$args
*/
#[DataProvider('dataFileAsserts')]
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/data/stub-method-body-types.neon',
];
}

}
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/data/stub-method-body-types.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
stubFiles:
- stub-method-body-types.stub
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/data/stub-method-body-types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use function PHPStan\Testing\assertType;

class StubMethodBodyTypesClass
{
/**
* Source declares @param string — stub overrides to @param callable-string
* @param string $callback
* @return string
*/
public function process($callback): string
{
// Stub should override the source file's @param type inside the method body
assertType('callable-string', $callback);
return $callback;
}
}

class StubMethodBodyTypesExternalCaller
{
public function test(StubMethodBodyTypesClass $obj): void
{
// Stub should also work for external callers (already worked before regression)
assertType('non-empty-string', $obj->process('strlen'));
}
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/stub-method-body-types.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

class StubMethodBodyTypesClass
{
/**
* @param callable-string $callback
* @return non-empty-string
*/
public function process($callback): string {}
}
Loading