Skip to content

Commit

Permalink
Make FileTypeMapper more deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 22, 2021
1 parent 42d367c commit ae6dca5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/PhpDoc/PhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -371,7 +371,7 @@ private static function resolvePhpDocBlockFromClass(
$classReflection,
$trait,
$name,
$classReflection->getFileNameWithPhpDocs(),
$classReflection->getFileName(),
$explicit,
$positionalParameterNames,
$positionalMethodParameterNames,
Expand Down
7 changes: 3 additions & 4 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
14 changes: 8 additions & 6 deletions src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 {
Expand All @@ -249,7 +249,8 @@ private function createNameScopeMap(
string $fileName,
?string $lookForTrait,
?string $traitUseClass,
array $traitMethodAliases = [],
array $traitMethodAliases,
string $originalClassFileName,
): array
{
/** @var (callable(): NameScope)[] $nameScopeMap */
Expand All @@ -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_) {
Expand Down Expand Up @@ -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_
Expand Down Expand Up @@ -456,6 +457,7 @@ function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, &$name
$traitName,
$className,
$traitMethodAliases[$traitName] ?? [],
$originalClassFileName,
);
$finalTraitPhpDocMap = [];
foreach ($traitPhpDocMap as $nameScopeTraitKey => $callback) {
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit ae6dca5

Please sign in to comment.