Skip to content

Commit bca8902

Browse files
committed
FileTypeMapper::getNameScope()
1 parent e52dec7 commit bca8902

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDoc;
4+
5+
use Exception;
6+
7+
final class NameScopeAlreadyBeingCreatedException extends Exception
8+
{
9+
10+
}

src/Type/FileTypeMapper.php

+25-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Broker\AnonymousClassNameHelper;
1010
use PHPStan\File\FileHelper;
1111
use PHPStan\Parser\Parser;
12+
use PHPStan\PhpDoc\NameScopeAlreadyBeingCreatedException;
1213
use PHPStan\PhpDoc\PhpDocNodeResolver;
1314
use PHPStan\PhpDoc\PhpDocStringResolver;
1415
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
@@ -99,22 +100,42 @@ public function getResolvedPhpDoc(
99100
return $this->createResolvedPhpDocBlock($phpDocKey, new NameScope(null, []), $docComment, null);
100101
}
101102

103+
try {
104+
$nameScope = $this->getNameScope($fileName, $className, $traitName, $functionName);
105+
} catch (NameScopeAlreadyBeingCreatedException) {
106+
return ResolvedPhpDocBlock::createEmpty();
107+
}
108+
109+
return $this->createResolvedPhpDocBlock($phpDocKey, $nameScope, $docComment, $fileName);
110+
}
111+
112+
/**
113+
* @throws NameScopeAlreadyBeingCreatedException
114+
*/
115+
public function getNameScope(
116+
string $fileName,
117+
?string $className,
118+
?string $traitName,
119+
?string $functionName,
120+
): NameScope
121+
{
122+
$nameScopeKey = $this->getNameScopeKey($fileName, $className, $traitName, $functionName);
102123
$nameScopeMap = [];
103124

104125
if (!isset($this->inProcess[$fileName])) {
105126
$nameScopeMap = $this->getNameScopeMap($fileName);
106127
}
107128

108129
if (isset($nameScopeMap[$nameScopeKey])) {
109-
return $this->createResolvedPhpDocBlock($phpDocKey, $nameScopeMap[$nameScopeKey], $docComment, $fileName);
130+
return $nameScopeMap[$nameScopeKey];
110131
}
111132

112133
if (!isset($this->inProcess[$fileName][$nameScopeKey])) { // wrong $fileName due to traits
113-
return ResolvedPhpDocBlock::createEmpty();
134+
throw new NameScopeAlreadyBeingCreatedException();
114135
}
115136

116137
if ($this->inProcess[$fileName][$nameScopeKey] === true) { // PHPDoc has cyclic dependency
117-
return ResolvedPhpDocBlock::createEmpty();
138+
throw new NameScopeAlreadyBeingCreatedException();
118139
}
119140

120141
if (is_callable($this->inProcess[$fileName][$nameScopeKey])) {
@@ -123,7 +144,7 @@ public function getResolvedPhpDoc(
123144
$this->inProcess[$fileName][$nameScopeKey] = $resolveCallback();
124145
}
125146

126-
return $this->createResolvedPhpDocBlock($phpDocKey, $this->inProcess[$fileName][$nameScopeKey], $docComment, $fileName);
147+
return $this->inProcess[$fileName][$nameScopeKey];
127148
}
128149

129150
private function createResolvedPhpDocBlock(string $phpDocKey, NameScope $nameScope, string $phpDocString, ?string $fileName): ResolvedPhpDocBlock

0 commit comments

Comments
 (0)