Skip to content

Commit 2be81ac

Browse files
committed
FileTypeMapper - optimization of type aliases
1 parent a4fa95a commit 2be81ac

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/Type/FileTypeMapper.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Type;
44

55
use Closure;
6-
use PhpParser\Comment\Doc;
76
use PhpParser\Node;
87
use PHPStan\Analyser\NameScope;
98
use PHPStan\BetterReflection\Util\GetLastDocComment;
@@ -435,7 +434,12 @@ function (Node $node) use ($fileName, $lookForTrait, $phpDocNodeMap, &$traitFoun
435434
}
436435

437436
$traitFound = true;
438-
$typeAliasStack[] = $this->getTypeAliasesMap($node->getDocComment());
437+
$traitNameScopeKey = $this->getNameScopeKey($originalClassFileName, $classStack[count($classStack) - 1] ?? null, $lookForTrait, null);
438+
if (array_key_exists($traitNameScopeKey, $phpDocNodeMap)) {
439+
$typeAliasStack[] = $this->getTypeAliasesMap($phpDocNodeMap[$traitNameScopeKey]);
440+
} else {
441+
$typeAliasStack[] = [];
442+
}
439443
$functionStack[] = null;
440444
} else {
441445
if ($node->name === null) {
@@ -453,7 +457,12 @@ function (Node $node) use ($fileName, $lookForTrait, $phpDocNodeMap, &$traitFoun
453457
$className = ltrim(sprintf('%s\\%s', $namespace, $node->name->name), '\\');
454458
}
455459
$classStack[] = $className;
456-
$typeAliasStack[] = $this->getTypeAliasesMap($node->getDocComment());
460+
$classNameScopeKey = $this->getNameScopeKey($originalClassFileName, $className, $lookForTrait, null);
461+
if (array_key_exists($classNameScopeKey, $phpDocNodeMap)) {
462+
$typeAliasStack[] = $this->getTypeAliasesMap($phpDocNodeMap[$classNameScopeKey]);
463+
} else {
464+
$typeAliasStack[] = [];
465+
}
457466
$functionStack[] = null;
458467
}
459468
} elseif ($node instanceof Node\Stmt\ClassMethod) {
@@ -716,13 +725,8 @@ static function (Node $node, $callbackResult) use (&$namespace, &$functionStack,
716725
/**
717726
* @return array<string, true>
718727
*/
719-
private function getTypeAliasesMap(?Doc $docComment): array
728+
private function getTypeAliasesMap(PhpDocNode $phpDocNode): array
720729
{
721-
if ($docComment === null) {
722-
return [];
723-
}
724-
725-
$phpDocNode = $this->phpDocStringResolver->resolve($docComment->getText());
726730
$nameScope = new NameScope(null, []);
727731

728732
$aliasesMap = [];

0 commit comments

Comments
 (0)