diff --git a/src/Type/FileTypeMapper.php b/src/Type/FileTypeMapper.php index 919ec6745d..4d8f49d69c 100644 --- a/src/Type/FileTypeMapper.php +++ b/src/Type/FileTypeMapper.php @@ -202,7 +202,7 @@ private function shouldPhpDocNodeBeCachedToDisk(PhpDocNode $phpDocNode): bool private function getResolvedPhpDocMap(string $fileName): array { if (!isset($this->memoryCache[$fileName])) { - $cacheKey = sprintf('%s-phpdocstring-v4-uses', $fileName); + $cacheKey = sprintf('%s-phpdocstring-v5-namespace', $fileName); $variableCacheKey = implode(',', array_map(static function (array $file): string { return sprintf('%s-%d', $file['filename'], $file['modifiedTime']); }, $this->getCachedDependentFilesWithTimestamps($fileName))); @@ -351,8 +351,6 @@ function (\PhpParser\Node $node) use ($fileName, $lookForTrait, $traitMethodAlia $phpDocMap = array_merge($phpDocMap, $traitPhpDocMap); } return null; - } elseif ($node instanceof \PhpParser\Node\Stmt\Namespace_) { - $namespace = (string) $node->name; } elseif ($node instanceof Node\Stmt\ClassMethod) { $functionName = $node->name->name; if (array_key_exists($functionName, $traitMethodAliases)) { @@ -441,7 +439,9 @@ function (\PhpParser\Node $node) use ($fileName, $lookForTrait, $traitMethodAlia return self::POP_TYPE_MAP_STACK; } - if ($node instanceof \PhpParser\Node\Stmt\Use_ && $node->type === \PhpParser\Node\Stmt\Use_::TYPE_NORMAL) { + if ($node instanceof \PhpParser\Node\Stmt\Namespace_) { + $namespace = (string) $node->name; + } elseif ($node instanceof \PhpParser\Node\Stmt\Use_ && $node->type === \PhpParser\Node\Stmt\Use_::TYPE_NORMAL) { foreach ($node->uses as $use) { $uses[strtolower($use->getAlias()->name)] = (string) $use->name; } diff --git a/tests/PHPStan/Rules/PhpDoc/InvalidPhpDocVarTagTypeRuleTest.php b/tests/PHPStan/Rules/PhpDoc/InvalidPhpDocVarTagTypeRuleTest.php index b0fff26157..c47a45a87e 100644 --- a/tests/PHPStan/Rules/PhpDoc/InvalidPhpDocVarTagTypeRuleTest.php +++ b/tests/PHPStan/Rules/PhpDoc/InvalidPhpDocVarTagTypeRuleTest.php @@ -120,4 +120,20 @@ public function testBug4486(): void ]); } + public function testBug4486Namespace(): void + { + $this->analyse([__DIR__ . '/data/bug-4486-ns.php'], [ + [ + 'PHPDoc tag @var for variable $one contains unknown class ClassName1.', + 6, + 'Learn more at https://phpstan.org/user-guide/discovering-symbols', + ], + [ + 'PHPDoc tag @var for variable $two contains unknown class Bug4486Namespace\ClassName1.', + 10, + 'Learn more at https://phpstan.org/user-guide/discovering-symbols', + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/PhpDoc/data/bug-4486-ns.php b/tests/PHPStan/Rules/PhpDoc/data/bug-4486-ns.php new file mode 100644 index 0000000000..e914313cb2 --- /dev/null +++ b/tests/PHPStan/Rules/PhpDoc/data/bug-4486-ns.php @@ -0,0 +1,10 @@ +