|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection\BetterReflection\SourceLocator; |
| 4 | + |
| 5 | +use Override; |
| 6 | +use PHPStan\BetterReflection\Identifier\Identifier; |
| 7 | +use PHPStan\BetterReflection\Identifier\IdentifierType; |
| 8 | +use PHPStan\BetterReflection\Reflection\Reflection; |
| 9 | +use PHPStan\BetterReflection\Reflection\ReflectionEnum; |
| 10 | +use PHPStan\BetterReflection\Reflector\DefaultReflector; |
| 11 | +use PHPStan\BetterReflection\Reflector\Reflector; |
| 12 | +use PHPStan\BetterReflection\SourceLocator\Ast\Locator; |
| 13 | +use PHPStan\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber; |
| 14 | +use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; |
| 15 | +use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator; |
| 16 | +use PHPStan\Cache\Cache; |
| 17 | +use PHPStan\Cache\CacheStorage; |
| 18 | +use PHPStan\Cache\MemoryCacheStorage; |
| 19 | +use PHPStan\Php\PhpVersion; |
| 20 | +use PHPStan\Reflection\BetterReflection\SourceStubber\ExtensionVersionProvider; |
| 21 | +use PHPStan\Testing\PHPStanTestCase; |
| 22 | +use function array_walk_recursive; |
| 23 | +use function is_string; |
| 24 | +use function strpos; |
| 25 | +use function strtolower; |
| 26 | +use function substr; |
| 27 | + |
| 28 | +class CachedPhpInternalSourceLocatorTest extends PHPStanTestCase |
| 29 | +{ |
| 30 | + |
| 31 | + public static function getAdditionalConfigFiles(): array |
| 32 | + { |
| 33 | + return [__DIR__ . '/data/php-8.4.neon']; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * The methods an enum gets from the engine (cases(), from(), tryFrom()) |
| 38 | + * carry their own located source in the cache entry, next to the enum's. |
| 39 | + */ |
| 40 | + public function testEnumFromCacheFilledByAnotherInstallation(): void |
| 41 | + { |
| 42 | + $storage = new MemoryCacheStorage(); |
| 43 | + $this->createReflector($this->createStorageOfAnotherInstallation($storage))->reflectClass('RoundingMode'); |
| 44 | + |
| 45 | + $enum = $this->createReflector($storage, 'RoundingMode')->reflectClass('RoundingMode'); |
| 46 | + $this->assertInstanceOf(ReflectionEnum::class, $enum); |
| 47 | + |
| 48 | + $method = $enum->getMethod('cases'); |
| 49 | + $this->assertNotNull($method); |
| 50 | + |
| 51 | + $fileName = $method->getFileName(); |
| 52 | + $this->assertNotNull($fileName); |
| 53 | + $this->assertFileExists($fileName); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param string|null $cachedOnlyClassName a class that the stubs locator does not find, so it has to come from the cache |
| 58 | + */ |
| 59 | + private function createReflector(CacheStorage $storage, ?string $cachedOnlyClassName = null): Reflector |
| 60 | + { |
| 61 | + $container = self::getContainer(); |
| 62 | + $stubber = $container->getByType(PhpStormStubsSourceStubber::class); |
| 63 | + |
| 64 | + $stubsLocator = new PhpInternalSourceLocator(new Locator($container->getService('php8PhpParser')), $stubber); |
| 65 | + if ($cachedOnlyClassName !== null) { |
| 66 | + $stubsLocator = $this->createLocatorWithoutClass($stubsLocator, $cachedOnlyClassName); |
| 67 | + } |
| 68 | + |
| 69 | + return new DefaultReflector(new CachedPhpInternalSourceLocator( |
| 70 | + $stubsLocator, |
| 71 | + new Cache($storage), |
| 72 | + $container->getByType(PhpVersion::class), |
| 73 | + $stubber, |
| 74 | + $container->getByType(ExtensionVersionProvider::class), |
| 75 | + )); |
| 76 | + } |
| 77 | + |
| 78 | + private function createLocatorWithoutClass(SourceLocator $locator, string $className): SourceLocator |
| 79 | + { |
| 80 | + return new class ($locator, $className) implements SourceLocator { |
| 81 | + |
| 82 | + public function __construct(private SourceLocator $locator, private string $className) |
| 83 | + { |
| 84 | + } |
| 85 | + |
| 86 | + #[Override] |
| 87 | + public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection |
| 88 | + { |
| 89 | + if ($identifier->isClass() && strtolower($identifier->getName()) === strtolower($this->className)) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return $this->locator->locateIdentifier($reflector, $identifier); |
| 94 | + } |
| 95 | + |
| 96 | + #[Override] |
| 97 | + public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array |
| 98 | + { |
| 99 | + return $this->locator->locateIdentifiersByType($reflector, $identifierType); |
| 100 | + } |
| 101 | + |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Saves the entries with the stub paths of an installation that has since |
| 107 | + * been moved or deleted, like a cache directory shared between CI jobs |
| 108 | + * that check out the project into different directories. |
| 109 | + */ |
| 110 | + private function createStorageOfAnotherInstallation(CacheStorage $storage): CacheStorage |
| 111 | + { |
| 112 | + return new class ($storage) implements CacheStorage { |
| 113 | + |
| 114 | + public function __construct(private CacheStorage $storage) |
| 115 | + { |
| 116 | + } |
| 117 | + |
| 118 | + #[Override] |
| 119 | + public function load(string $key, string $variableKey) |
| 120 | + { |
| 121 | + return $this->storage->load($key, $variableKey); |
| 122 | + } |
| 123 | + |
| 124 | + #[Override] |
| 125 | + public function save(string $key, string $variableKey, $data): void |
| 126 | + { |
| 127 | + array_walk_recursive($data, static function (&$value): void { |
| 128 | + if (!is_string($value)) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + $markerPosition = strpos($value, '/jetbrains/phpstorm-stubs/'); |
| 133 | + if ($markerPosition === false) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + $value = '/moved-away/vendor' . substr($value, $markerPosition); |
| 138 | + }); |
| 139 | + |
| 140 | + $this->storage->save($key, $variableKey, $data); |
| 141 | + } |
| 142 | + |
| 143 | + }; |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments