diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 1d766a34c0..60b0209b80 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -89,6 +89,9 @@ class ClassReflection implements ReflectionWithFilename /** @var string|false|null */ private $filename; + /** @var string|false|null */ + private $reflectionDocComment; + /** * @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider * @param \PHPStan\Type\FileTypeMapper $fileTypeMapper @@ -933,12 +936,15 @@ public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock return null; } - $docComment = $this->reflection->getDocComment(); - if ($docComment === false) { + if ($this->reflectionDocComment === null) { + $this->reflectionDocComment = $this->reflection->getDocComment(); + } + + if ($this->reflectionDocComment === false) { return null; } - return $this->fileTypeMapper->getResolvedPhpDoc($fileName, $this->getName(), null, null, $docComment); + return $this->fileTypeMapper->getResolvedPhpDoc($fileName, $this->getName(), null, null, $this->reflectionDocComment); } private function getFirstExtendsTag(): ?ExtendsTag diff --git a/src/Type/FileTypeMapper.php b/src/Type/FileTypeMapper.php index 04ca00825a..d75bb0b234 100644 --- a/src/Type/FileTypeMapper.php +++ b/src/Type/FileTypeMapper.php @@ -53,6 +53,9 @@ class FileTypeMapper /** @var array */ private array $alreadyProcessedDependentFiles = []; + /** @var array */ + private array $docKeys = []; + public function __construct( ReflectionProviderProvider $reflectionProviderProvider, Parser $phpParser, @@ -518,7 +521,11 @@ private function getPhpDocKey( string $docComment ): string { - $docComment = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' '); + $cacheKey = md5($docComment); + if (!isset($this->docKeys[$cacheKey])) { + $this->docKeys[$cacheKey] = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' '); + } + $docComment = $this->docKeys[$cacheKey]; return md5(sprintf('%s-%s-%s-%s', $class, $trait, $function, $docComment)); }