DocBlockAnalyzer - cache per Node

This commit is contained in:
Tomas Votruba 2018-09-21 15:02:22 +02:00
parent b30fa8a047
commit d04152076b

View File

@ -30,6 +30,11 @@ final class DocBlockAnalyzer
*/
private $currentNodeProvider;
/**
* @var PhpDocInfo[]
*/
private $cachedPhpDocInfoByNode = [];
public function __construct(
PhpDocInfoFactory $phpDocInfoFactory,
PhpDocInfoPrinter $phpDocInfoPrinter,
@ -168,6 +173,11 @@ final class DocBlockAnalyzer
private function createPhpDocInfoFromNode(Node $node): PhpDocInfo
{
$key = spl_object_hash($node);
if (isset($this->cachedPhpDocInfoByNode[$key])) {
return $this->cachedPhpDocInfoByNode[$key];
}
$this->currentNodeProvider->setCurrentNode($node);
if ($node->getDocComment() === null) {
throw new ShouldNotHappenException(sprintf(
@ -176,6 +186,8 @@ final class DocBlockAnalyzer
));
}
return $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
return $this->cachedPhpDocInfoByNode[$key] = $this->phpDocInfoFactory->createFrom(
$node->getDocComment()->getText()
);
}
}