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