Skip to content

Commit c02b70e

Browse files
committed
fix
1 parent a008245 commit c02b70e

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Internal\ComposerHelper;
1717
use PHPStan\Php\PhpVersion;
1818
use PHPStan\Reflection\ConstantNameHelper;
19+
use PHPStan\ShouldNotHappenException;
1920
use function array_key_exists;
2021
use function register_shutdown_function;
2122
use function sprintf;
@@ -24,11 +25,14 @@
2425
final class FileCachedSourceLocator implements SourceLocator
2526
{
2627

27-
/** @var array{classes: array<string, ReflectionClass>, functions: array<string, ReflectionFunction>, constants: array<string, ReflectionConstant>}|null */
28+
/** @var array{classes: array<string, ?Reflection>, functions: array<string, ?Reflection>, constants: array<string, ?Reflection>}|null */
2829
private ?array $cachedSymbols = null;
2930

3031
private bool $storeOnShutdown = false;
3132

33+
/**
34+
* @param non-empty-string $cacheKey
35+
*/
3236
public function __construct(
3337
private SourceLocator $locator,
3438
private Cache $cache,
@@ -47,7 +51,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
4751
$className = strtolower($identifier->getName());
4852

4953
if (!array_key_exists($className, $this->cachedSymbols['classes'])) {
50-
$this->cachedSymbols['classes'][$className] ??= $this->locator->locateIdentifier($reflector, $identifier);
54+
$this->cachedSymbols['classes'][$className] = $this->locator->locateIdentifier($reflector, $identifier);
5155
$this->storeOnShutdown();
5256
}
5357
return $this->cachedSymbols['classes'][$className];
@@ -56,7 +60,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
5660
$className = strtolower($identifier->getName());
5761

5862
if (!array_key_exists($className, $this->cachedSymbols['functions'])) {
59-
$this->cachedSymbols['functions'][$className] ??= $this->locator->locateIdentifier($reflector, $identifier);
63+
$this->cachedSymbols['functions'][$className] = $this->locator->locateIdentifier($reflector, $identifier);
6064
$this->storeOnShutdown();
6165
}
6266
return $this->cachedSymbols['functions'][$className];
@@ -65,7 +69,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
6569
$constantName = ConstantNameHelper::normalize($identifier->getName());
6670

6771
if (!array_key_exists($constantName, $this->cachedSymbols['constants'])) {
68-
$this->cachedSymbols['constants'][$constantName] ??= $this->locator->locateIdentifier($reflector, $identifier);
72+
$this->cachedSymbols['constants'][$constantName] = $this->locator->locateIdentifier($reflector, $identifier);
6973
$this->storeOnShutdown();
7074
}
7175
return $this->cachedSymbols['constants'][$constantName];
@@ -80,6 +84,7 @@ public function locateIdentifiersByType(Reflector $reflector, IdentifierType $id
8084
return $this->locator->locateIdentifiersByType($reflector, $identifierType);
8185
}
8286

87+
/** @return non-empty-string */
8388
private function getVariableCacheKey(): string
8489
{
8590
return sprintf('v2-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString());
@@ -95,7 +100,7 @@ private function storeOnShutdown(): void
95100
register_shutdown_function([$this, 'storeCache']);
96101
}
97102

98-
/** @return array{classes: array<string, ReflectionClass>, functions: array<string, ReflectionFunction>, constants: array<string, ReflectionConstant>} */
103+
/** @return array{classes: array<string, ReflectionClass|null>, functions: array<string, ReflectionFunction|null>, constants: array<string, ReflectionConstant|null>} */
99104
private function loadCache(Reflector $reflector): array
100105
{
101106
$variableCacheKey = $this->getVariableCacheKey();
@@ -156,6 +161,15 @@ private function storeCache(): void
156161
$exported[$type][$name] = $reflection;
157162
continue;
158163
}
164+
165+
if (
166+
!$reflection instanceof ReflectionClass
167+
&& !$reflection instanceof ReflectionFunction
168+
&& !$reflection instanceof ReflectionConstant
169+
) {
170+
throw new ShouldNotHappenException();
171+
}
172+
159173
$exported[$type][$name] = $reflection->exportToCache();
160174
}
161175
}

0 commit comments

Comments
 (0)