Skip to content
Closed
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
93 changes: 78 additions & 15 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
use function array_key_exists;
use function array_key_first;
use function array_keys;
use function array_last;
use function array_map;
Expand Down Expand Up @@ -115,28 +116,63 @@ public function getResolvedPhpDoc(
return $this->resolvedPhpDocBlockCache[$phpDocKey];
}

if ($this->resolvedPhpDocBlockCacheCount >= $this->resolvedPhpDocBlockCacheCountMax) {
$this->resolvedPhpDocBlockCache = array_slice(
$this->resolvedPhpDocBlockCache,
1,
preserve_keys: true,
);

$this->resolvedPhpDocBlockCacheCount--;
}

$this->resolvedPhpDocBlockCacheCount++;

if ($fileName === null) {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
echo sprintf("Adding to resolvedPhpDocBlockCache: %s (%s, %s, %s, %s)\n", $phpDocKey, $fileName, $className, $traitName, $functionName);
return $this->resolvedPhpDocBlockCache[$phpDocKey] = $this->createResolvedPhpDocBlock($this->phpDocStringResolver->resolve($docComment), new NameScope(null, []), $docComment, null);
}

try {
$nameScope = $this->getNameScope($fileName, $className, $traitName, $functionName);
} catch (NameScopeAlreadyBeingCreatedException) {
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
if ($className !== null && $traitName !== null) {
$reflectionProvider = $this->reflectionProviderProvider->getReflectionProvider();
if ($reflectionProvider->hasClass($traitName)) {
$traitReflection = $reflectionProvider->getClass($traitName);
if ($traitReflection->getFileName() !== null) {
$traitNameScopeKey = $this->getNameScopeKey($traitReflection->getFileName(), $traitName, null, $functionName);
$traitPhpDocKey = $this->getPhpDocKey($traitNameScopeKey, $docComment);
if (isset($this->resolvedPhpDocBlockCache[$traitPhpDocKey])) {
return $this->resolvedPhpDocBlockCache[$traitPhpDocKey];
}

try {
// taky v tomhle pripade neslicovat cache, az pri jistote ze tam budu vytvaret novy klic
$nameScope = $this->getNameScope($traitReflection->getFileName(), $traitName, null, $functionName);
} catch (NameScopeAlreadyBeingCreatedException) {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
echo sprintf("Adding empty 1 to resolvedPhpDocBlockCache: %s (%s, %s, %s, %s)\n", $phpDocKey, $fileName, $className, $traitName, $functionName);
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
$phpDocKey = $traitPhpDocKey;

} else {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
echo sprintf("Adding empty 2 to resolvedPhpDocBlockCache: %s (%s, %s, %s, %s)\n", $phpDocKey, $fileName, $className, $traitName, $functionName);
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
} else {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
echo sprintf("Adding empty 3 to resolvedPhpDocBlockCache: %s (%s, %s, %s, %s)\n", $phpDocKey, $fileName, $className, $traitName, $functionName);
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
} else {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
echo sprintf("Adding empty 4 to resolvedPhpDocBlockCache: %s (%s, %s, %s, %s)\n", $phpDocKey, $fileName, $className, $traitName, $functionName);
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
}

$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;

echo sprintf("Adding to resolvedPhpDocBlockCache: %s (%s, %s, %s, %s)\n", $phpDocKey, $fileName, $className, $traitName, $functionName);

return $this->resolvedPhpDocBlockCache[$phpDocKey] = $this->createResolvedPhpDocBlock(
$this->phpDocStringResolver->resolve($docComment),
$nameScope,
Expand All @@ -145,6 +181,23 @@ public function getResolvedPhpDoc(
);
}

private function sliceResolvedPhpDocBlockCache(): void
{
if ($this->resolvedPhpDocBlockCacheCount < $this->resolvedPhpDocBlockCacheCountMax) {
return;
}

echo sprintf("Evicting from resolvedPhpDocBlockCache: %s\n", array_key_first($this->resolvedPhpDocBlockCache));

$this->resolvedPhpDocBlockCache = array_slice(
$this->resolvedPhpDocBlockCache,
1,
preserve_keys: true,
);

$this->resolvedPhpDocBlockCacheCount--;
}

private function createResolvedPhpDocBlock(
PhpDocNode $phpDocNode,
NameScope $nameScope,
Expand Down Expand Up @@ -329,7 +382,7 @@ private function getNameScopeMap(string $fileName): array
{
if (!isset($this->memoryCache[$fileName])) {
$cacheKey = sprintf('ftm-%s', $fileName);
$variableCacheKey = sprintf('v3-%s', ComposerHelper::getPhpDocParserVersion());
$variableCacheKey = sprintf('v6-traits-%s', ComposerHelper::getPhpDocParserVersion());
$cached = $this->loadCachedPhpDocNodeMap($cacheKey, $variableCacheKey);
if ($cached === null) {
[$nameScopeMap, $files] = $this->createPhpDocNodeMap($fileName, null, null, [], $fileName);
Expand All @@ -343,6 +396,8 @@ private function getNameScopeMap(string $fileName): array
[$nameScopeMap, $files] = $cached;
}
if ($this->memoryCacheCount >= $this->nameScopeMapMemoryCacheCountMax) {
$evictingFileName = array_key_first($this->memoryCache);
echo sprintf("Evicting from getNameScopeMap memoryCache: %s\n", $evictingFileName);
$this->memoryCache = array_slice(
$this->memoryCache,
1,
Expand All @@ -351,6 +406,8 @@ private function getNameScopeMap(string $fileName): array
$this->memoryCacheCount--;
}

echo sprintf("Adding to getNameScopeMap memoryCache: %s\n", $fileName);

$this->memoryCache[$fileName] = [$nameScopeMap, $files];
$this->memoryCacheCount++;
}
Expand Down Expand Up @@ -492,6 +549,10 @@ function (Node $node) use ($fileName, $lookForTrait, &$traitFound, $traitMethodA

if ($node instanceof Node\Stmt\ClassLike || $node instanceof Node\Stmt\ClassMethod || $node instanceof Node\Stmt\Function_ || $node instanceof Node\PropertyHook) {
if ($phpDocNode !== null) {
$templateTagValueNodes = $this->chooseTemplateTagValueNodesByPriority($phpDocNode->getTags());
if ($lookForTrait !== null && $node instanceof Node\Stmt\Trait_ && count($templateTagValueNodes) === 0 && count($traitMethodAliases) === 0) {
return self::SKIP_NODE;
}
if ($node instanceof Node\Stmt\ClassLike) {
$typeAliasStack[] = $this->getTypeAliasesMap($phpDocNode);
}
Expand All @@ -503,12 +564,14 @@ function (Node $node) use ($fileName, $lookForTrait, &$traitFound, $traitMethodA
$uses,
$className,
$functionName,
$this->chooseTemplateTagValueNodesByPriority($phpDocNode->getTags()),
$templateTagValueNodes,
$parentNameScope,
array_last($typeAliasStack) ?? [],
constUses: $constUses,
typeAliasClassName: $lookForTrait,
);
} elseif ($lookForTrait !== null && $node instanceof Node\Stmt\Trait_ && count($traitMethodAliases) === 0) {
return self::SKIP_NODE;
} elseif ($node instanceof Node\Stmt\ClassLike) {
$typeAliasStack[] = [];
} else {
Expand Down
Loading