Skip to content

Commit

Permalink
FileTypeMapper - optimization of type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 25, 2023
1 parent a4fa95a commit 2be81ac
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PHPStan\Type;

use Closure;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PHPStan\Analyser\NameScope;
use PHPStan\BetterReflection\Util\GetLastDocComment;
Expand Down Expand Up @@ -435,7 +434,12 @@ function (Node $node) use ($fileName, $lookForTrait, $phpDocNodeMap, &$traitFoun
}

$traitFound = true;
$typeAliasStack[] = $this->getTypeAliasesMap($node->getDocComment());
$traitNameScopeKey = $this->getNameScopeKey($originalClassFileName, $classStack[count($classStack) - 1] ?? null, $lookForTrait, null);
if (array_key_exists($traitNameScopeKey, $phpDocNodeMap)) {
$typeAliasStack[] = $this->getTypeAliasesMap($phpDocNodeMap[$traitNameScopeKey]);
} else {
$typeAliasStack[] = [];
}
$functionStack[] = null;
} else {
if ($node->name === null) {
Expand All @@ -453,7 +457,12 @@ function (Node $node) use ($fileName, $lookForTrait, $phpDocNodeMap, &$traitFoun
$className = ltrim(sprintf('%s\\%s', $namespace, $node->name->name), '\\');
}
$classStack[] = $className;
$typeAliasStack[] = $this->getTypeAliasesMap($node->getDocComment());
$classNameScopeKey = $this->getNameScopeKey($originalClassFileName, $className, $lookForTrait, null);
if (array_key_exists($classNameScopeKey, $phpDocNodeMap)) {
$typeAliasStack[] = $this->getTypeAliasesMap($phpDocNodeMap[$classNameScopeKey]);
} else {
$typeAliasStack[] = [];
}
$functionStack[] = null;
}
} elseif ($node instanceof Node\Stmt\ClassMethod) {
Expand Down Expand Up @@ -716,13 +725,8 @@ static function (Node $node, $callbackResult) use (&$namespace, &$functionStack,
/**
* @return array<string, true>
*/
private function getTypeAliasesMap(?Doc $docComment): array
private function getTypeAliasesMap(PhpDocNode $phpDocNode): array
{
if ($docComment === null) {
return [];
}

$phpDocNode = $this->phpDocStringResolver->resolve($docComment->getText());
$nameScope = new NameScope(null, []);

$aliasesMap = [];
Expand Down

0 comments on commit 2be81ac

Please sign in to comment.