Skip to content
Draft
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
79 changes: 64 additions & 15 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,56 @@ 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++;
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++;
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
$phpDocKey = $traitPhpDocKey;

} else {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
} else {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
} else {
$this->sliceResolvedPhpDocBlockCache();
$this->resolvedPhpDocBlockCacheCount++;
return $this->resolvedPhpDocBlockCache[$phpDocKey] = ResolvedPhpDocBlock::createEmpty();
}
}

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

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

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

$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 +372,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 Down Expand Up @@ -492,6 +535,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 +550,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