|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection\BetterReflection\SourceStubber; |
| 4 | + |
| 5 | +use PHPStan\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber; |
| 6 | +use PHPStan\BetterReflection\SourceLocator\SourceStubber\SourceStubber; |
| 7 | +use PHPStan\BetterReflection\SourceLocator\SourceStubber\StubData; |
| 8 | +use PHPStan\Cache\Cache; |
| 9 | +use PHPStan\Internal\ComposerHelper; |
| 10 | +use PHPStan\Php\PhpVersion; |
| 11 | +use function array_key_exists; |
| 12 | +use function sprintf; |
| 13 | + |
| 14 | +final class CachedPhpStormStubsSourceStubber implements SourceStubber |
| 15 | +{ |
| 16 | + |
| 17 | + /** @var array<string, mixed> */ |
| 18 | + private array $cached; |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + private PhpStormStubsSourceStubber $sourceStubber, |
| 22 | + private Cache $cache, |
| 23 | + private PhpVersion $phpVersion, |
| 24 | + ) |
| 25 | + { |
| 26 | + [$cacheKey, $variableCacheKey] = $this->getCacheKeys(); |
| 27 | + $this->cached = $this->cache->load($cacheKey, $variableCacheKey) ?? []; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return array{non-empty-string, string} |
| 32 | + */ |
| 33 | + private function getCacheKeys(): array |
| 34 | + { |
| 35 | + $stubsVersion = 'dev-master#5fd9e4ef2b4c2a00848ea62d78bf3aa81f43e038'; |
| 36 | + $cacheKey = 'phpstorm-stubs'; |
| 37 | + $variableCacheKey = sprintf('v1-%s-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString(), $stubsVersion); |
| 38 | + |
| 39 | + return [$cacheKey, $variableCacheKey]; |
| 40 | + } |
| 41 | + |
| 42 | + public function generateClassStub(string $className): ?StubData |
| 43 | + { |
| 44 | + $this->cached['classes'] ??= []; |
| 45 | + if (!array_key_exists($className, $this->cached['classes'])) { |
| 46 | + $this->cached['classes'][$className] = $this->sourceStubber->generateClassStub($className); |
| 47 | + $this->storeCache(); |
| 48 | + } |
| 49 | + return $this->cached['classes'][$className]; |
| 50 | + } |
| 51 | + |
| 52 | + public function generateFunctionStub(string $functionName): ?StubData |
| 53 | + { |
| 54 | + $this->cached['functions'] ??= []; |
| 55 | + if (!array_key_exists($functionName, $this->cached['functions'])) { |
| 56 | + $this->cached['functions'][$functionName] = $this->sourceStubber->generateFunctionStub($functionName); |
| 57 | + $this->storeCache(); |
| 58 | + } |
| 59 | + return $this->cached['functions'][$functionName]; |
| 60 | + } |
| 61 | + |
| 62 | + public function generateConstantStub(string $constantName): ?StubData |
| 63 | + { |
| 64 | + $this->cached['constants'] ??= []; |
| 65 | + if (!array_key_exists($constantName, $this->cached['constants'])) { |
| 66 | + $this->cached['constants'][$constantName] = $this->sourceStubber->generateConstantStub($constantName); |
| 67 | + $this->storeCache(); |
| 68 | + } |
| 69 | + return $this->cached['constants'][$constantName]; |
| 70 | + } |
| 71 | + |
| 72 | + public function isPresentClass(string $className): ?bool |
| 73 | + { |
| 74 | + $this->cached['isPresentClass'] ??= []; |
| 75 | + if (!array_key_exists($className, $this->cached['isPresentClass'])) { |
| 76 | + $this->cached['isPresentClass'][$className] = $this->sourceStubber->isPresentClass($className); |
| 77 | + $this->storeCache(); |
| 78 | + } |
| 79 | + return $this->cached['isPresentClass'][$className]; |
| 80 | + } |
| 81 | + |
| 82 | + public function isPresentFunction(string $functionName): ?bool |
| 83 | + { |
| 84 | + $this->cached['isPresentFunction'] ??= []; |
| 85 | + if (!array_key_exists($functionName, $this->cached['isPresentFunction'])) { |
| 86 | + $this->cached['isPresentFunction'][$functionName] = $this->sourceStubber->isPresentFunction($functionName); |
| 87 | + $this->storeCache(); |
| 88 | + } |
| 89 | + return $this->cached['isPresentFunction'][$functionName]; |
| 90 | + } |
| 91 | + |
| 92 | + private function storeCache(): void |
| 93 | + { |
| 94 | + [$cacheKey, $variableCacheKey] = $this->getCacheKeys(); |
| 95 | + $this->cache->save($cacheKey, $variableCacheKey, $this->cached); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments