From ae6dca5d4f485fde77a1b2c7eaca55fe2ec9b28a Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 21 Dec 2021 18:56:59 +0100 Subject: [PATCH] Make FileTypeMapper more deterministic --- src/PhpDoc/PhpDocBlock.php | 4 ++-- src/Reflection/ClassReflection.php | 7 +++---- src/Type/FileTypeMapper.php | 14 ++++++++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/PhpDoc/PhpDocBlock.php b/src/PhpDoc/PhpDocBlock.php index 64b5fa6037..04315ae415 100644 --- a/src/PhpDoc/PhpDocBlock.php +++ b/src/PhpDoc/PhpDocBlock.php @@ -327,7 +327,7 @@ private static function resolvePhpDocBlockFromClass( array $positionalParameterNames, ): ?self { - if ($classReflection->getFileNameWithPhpDocs() !== null && $classReflection->$hasMethodName($name)) { + if ($classReflection->getFileName() !== null && $classReflection->$hasMethodName($name)) { /** @var PropertyReflection|MethodReflection|ConstantReflection $parentReflection */ $parentReflection = $classReflection->$getMethodName($name); if ($parentReflection->isPrivate()) { @@ -371,7 +371,7 @@ private static function resolvePhpDocBlockFromClass( $classReflection, $trait, $name, - $classReflection->getFileNameWithPhpDocs(), + $classReflection->getFileName(), $explicit, $positionalParameterNames, $positionalMethodParameterNames, diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index cbb36c6f9f..a8d92573e2 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -190,12 +190,11 @@ public function getFileName(): ?string return $this->filename = $fileName; } + /** + * @deprecated Use getFileName() + */ public function getFileNameWithPhpDocs(): ?string { - if ($this->stubPhpDocBlock !== null) { - return $this->stubPhpDocBlock->getFilename(); - } - return $this->getFileName(); } diff --git a/src/Type/FileTypeMapper.php b/src/Type/FileTypeMapper.php index 9affc597fc..1c0736dbec 100644 --- a/src/Type/FileTypeMapper.php +++ b/src/Type/FileTypeMapper.php @@ -202,7 +202,7 @@ private function resolvePhpDocStringToDocNode(string $phpDocString): PhpDocNode private function getNameScopeMap(string $fileName): array { if (!isset($this->memoryCache[$fileName])) { - $cacheKey = sprintf('%s-phpdocstring-v13-namescope', $fileName); + $cacheKey = sprintf('%s-phpdocstring-v14-filename', $fileName); $variableCacheKey = implode(',', array_map(static fn (array $file): string => sprintf('%s-%d', $file['filename'], $file['modifiedTime']), $this->getCachedDependentFilesWithTimestamps($fileName))); $map = $this->cache->load($cacheKey, $variableCacheKey); @@ -222,7 +222,7 @@ private function getNameScopeMap(string $fileName): array */ private function createResolvedPhpDocMap(string $fileName): array { - $nameScopeMap = $this->createNameScopeMap($fileName, null, null); + $nameScopeMap = $this->createNameScopeMap($fileName, null, null, [], $fileName); $resolvedNameScopeMap = []; try { @@ -249,7 +249,8 @@ private function createNameScopeMap( string $fileName, ?string $lookForTrait, ?string $traitUseClass, - array $traitMethodAliases = [], + array $traitMethodAliases, + string $originalClassFileName, ): array { /** @var (callable(): NameScope)[] $nameScopeMap */ @@ -274,7 +275,7 @@ private function createNameScopeMap( $uses = []; $this->processNodes( $this->phpParser->parseFile($fileName), - function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, &$nameScopeMap, &$classStack, &$typeAliasStack, &$namespace, &$functionStack, &$uses, &$typeMapStack): ?int { + function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, $originalClassFileName, &$nameScopeMap, &$classStack, &$typeAliasStack, &$namespace, &$functionStack, &$uses, &$typeMapStack): ?int { if ($node instanceof Node\Stmt\ClassLike) { if ($lookForTrait !== null) { if (!$node instanceof Node\Stmt\Trait_) { @@ -357,7 +358,7 @@ function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, &$name $typeMapCb = $typeMapStack[count($typeMapStack) - 1] ?? null; $typeAliasesMap = $typeAliasStack[count($typeAliasStack) - 1] ?? []; - $nameScopeKey = $this->getNameScopeKey($fileName, $className, $lookForTrait, $functionName); + $nameScopeKey = $this->getNameScopeKey($originalClassFileName, $className, $lookForTrait, $functionName); if ( $node instanceof Node\Stmt && !$node instanceof Node\Stmt\Namespace_ @@ -456,6 +457,7 @@ function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, &$name $traitName, $className, $traitMethodAliases[$traitName] ?? [], + $originalClassFileName, ); $finalTraitPhpDocMap = []; foreach ($traitPhpDocMap as $nameScopeTraitKey => $callback) { @@ -612,7 +614,7 @@ private function getNameScopeKey( return md5(sprintf('%s', $file)); } - return md5(sprintf('%s-%s-%s', $class, $trait, $function)); + return md5(sprintf('%s-%s-%s-%s', $file, $class, $trait, $function)); } /**