Skip to content

Commit ae6dca5

Browse files
committed
Make FileTypeMapper more deterministic
1 parent 42d367c commit ae6dca5

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

Diff for: src/PhpDoc/PhpDocBlock.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ private static function resolvePhpDocBlockFromClass(
327327
array $positionalParameterNames,
328328
): ?self
329329
{
330-
if ($classReflection->getFileNameWithPhpDocs() !== null && $classReflection->$hasMethodName($name)) {
330+
if ($classReflection->getFileName() !== null && $classReflection->$hasMethodName($name)) {
331331
/** @var PropertyReflection|MethodReflection|ConstantReflection $parentReflection */
332332
$parentReflection = $classReflection->$getMethodName($name);
333333
if ($parentReflection->isPrivate()) {
@@ -371,7 +371,7 @@ private static function resolvePhpDocBlockFromClass(
371371
$classReflection,
372372
$trait,
373373
$name,
374-
$classReflection->getFileNameWithPhpDocs(),
374+
$classReflection->getFileName(),
375375
$explicit,
376376
$positionalParameterNames,
377377
$positionalMethodParameterNames,

Diff for: src/Reflection/ClassReflection.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,11 @@ public function getFileName(): ?string
190190
return $this->filename = $fileName;
191191
}
192192

193+
/**
194+
* @deprecated Use getFileName()
195+
*/
193196
public function getFileNameWithPhpDocs(): ?string
194197
{
195-
if ($this->stubPhpDocBlock !== null) {
196-
return $this->stubPhpDocBlock->getFilename();
197-
}
198-
199198
return $this->getFileName();
200199
}
201200

Diff for: src/Type/FileTypeMapper.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private function resolvePhpDocStringToDocNode(string $phpDocString): PhpDocNode
202202
private function getNameScopeMap(string $fileName): array
203203
{
204204
if (!isset($this->memoryCache[$fileName])) {
205-
$cacheKey = sprintf('%s-phpdocstring-v13-namescope', $fileName);
205+
$cacheKey = sprintf('%s-phpdocstring-v14-filename', $fileName);
206206
$variableCacheKey = implode(',', array_map(static fn (array $file): string => sprintf('%s-%d', $file['filename'], $file['modifiedTime']), $this->getCachedDependentFilesWithTimestamps($fileName)));
207207
$map = $this->cache->load($cacheKey, $variableCacheKey);
208208

@@ -222,7 +222,7 @@ private function getNameScopeMap(string $fileName): array
222222
*/
223223
private function createResolvedPhpDocMap(string $fileName): array
224224
{
225-
$nameScopeMap = $this->createNameScopeMap($fileName, null, null);
225+
$nameScopeMap = $this->createNameScopeMap($fileName, null, null, [], $fileName);
226226
$resolvedNameScopeMap = [];
227227

228228
try {
@@ -249,7 +249,8 @@ private function createNameScopeMap(
249249
string $fileName,
250250
?string $lookForTrait,
251251
?string $traitUseClass,
252-
array $traitMethodAliases = [],
252+
array $traitMethodAliases,
253+
string $originalClassFileName,
253254
): array
254255
{
255256
/** @var (callable(): NameScope)[] $nameScopeMap */
@@ -274,7 +275,7 @@ private function createNameScopeMap(
274275
$uses = [];
275276
$this->processNodes(
276277
$this->phpParser->parseFile($fileName),
277-
function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, &$nameScopeMap, &$classStack, &$typeAliasStack, &$namespace, &$functionStack, &$uses, &$typeMapStack): ?int {
278+
function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, $originalClassFileName, &$nameScopeMap, &$classStack, &$typeAliasStack, &$namespace, &$functionStack, &$uses, &$typeMapStack): ?int {
278279
if ($node instanceof Node\Stmt\ClassLike) {
279280
if ($lookForTrait !== null) {
280281
if (!$node instanceof Node\Stmt\Trait_) {
@@ -357,7 +358,7 @@ function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, &$name
357358
$typeMapCb = $typeMapStack[count($typeMapStack) - 1] ?? null;
358359
$typeAliasesMap = $typeAliasStack[count($typeAliasStack) - 1] ?? [];
359360

360-
$nameScopeKey = $this->getNameScopeKey($fileName, $className, $lookForTrait, $functionName);
361+
$nameScopeKey = $this->getNameScopeKey($originalClassFileName, $className, $lookForTrait, $functionName);
361362
if (
362363
$node instanceof Node\Stmt
363364
&& !$node instanceof Node\Stmt\Namespace_
@@ -456,6 +457,7 @@ function (Node $node) use ($fileName, $lookForTrait, $traitMethodAliases, &$name
456457
$traitName,
457458
$className,
458459
$traitMethodAliases[$traitName] ?? [],
460+
$originalClassFileName,
459461
);
460462
$finalTraitPhpDocMap = [];
461463
foreach ($traitPhpDocMap as $nameScopeTraitKey => $callback) {
@@ -612,7 +614,7 @@ private function getNameScopeKey(
612614
return md5(sprintf('%s', $file));
613615
}
614616

615-
return md5(sprintf('%s-%s-%s', $class, $trait, $function));
617+
return md5(sprintf('%s-%s-%s-%s', $file, $class, $trait, $function));
616618
}
617619

618620
/**

0 commit comments

Comments
 (0)