Skip to content

Commit 311ac20

Browse files
phpstan-botclaude
andcommitted
Resolve class-node lookup inside the single-element loop instead of suppressing errors
The class branch's $files is always a single-element [$file], so the now non-empty array inference proved the post-loop $fetchedClassNode === null check dead and left $file as a foreach value variable that PHPStan treats as possibly-undefined after the loop. Building the reflection inside the loop drops the dead check and the post-loop variable use, removing both @PHPStan-Ignore comments rather than suppressing the warnings. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 80280f6 commit 311ac20

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
111111
}
112112

113113
if ($identifier->isClass()) {
114-
$fetchedClassNode = null;
115114
foreach ($files as $file) {
116115
$fetchedClassNodes = $this->fileNodesFetcher->fetchNodes($file)->getClassNodes();
117116

@@ -121,17 +120,13 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
121120

122121
/** @var FetchedNode<Node\Stmt\ClassLike> $fetchedClassNode */
123122
$fetchedClassNode = current($fetchedClassNodes[$identifierName]);
124-
}
125-
126-
if ($fetchedClassNode === null) { // @phpstan-ignore identical.alwaysFalse
127-
return null;
128-
}
129123

130-
[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($file, $identifier); // @phpstan-ignore variable.undefined
131-
$classReflection = $this->nodeToReflection($reflector, $fetchedClassNode);
132-
$this->cache->save($reflectionCacheKey, $variableCacheKey, $classReflection->exportToCache());
124+
[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($file, $identifier);
125+
$classReflection = $this->nodeToReflection($reflector, $fetchedClassNode);
126+
$this->cache->save($reflectionCacheKey, $variableCacheKey, $classReflection->exportToCache());
133127

134-
return $classReflection;
128+
return $classReflection;
129+
}
135130
} elseif ($identifier->isFunction()) {
136131
$fetchedFunctionNode = null;
137132
foreach ($files as $file) {

0 commit comments

Comments
 (0)