Skip to content

Commit

Permalink
Fix resolving types in @var above namespace statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 5, 2021
1 parent 4fff79c commit 26fb6dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/InvalidPhpDocVarTagTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]);
}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-4486-ns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* @var ClassName1 $one
*/
namespace Bug4486Namespace;

/**
* @var ClassName1 $two
*/

0 comments on commit 26fb6dd

Please sign in to comment.