Skip to content

Commit d3f5f86

Browse files
committed
Create FileCachedSourceLocator.php
1 parent a189760 commit d3f5f86

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\BetterReflection\SourceLocator;
4+
5+
use Override;
6+
use PhpParser\Node\Arg;
7+
use PhpParser\Node\Expr\FuncCall;
8+
use PhpParser\Node\Name;
9+
use PhpParser\Node\Scalar\String_;
10+
use PhpParser\Node\Stmt\Const_;
11+
use PHPStan\BetterReflection\Identifier\Identifier;
12+
use PHPStan\BetterReflection\Identifier\IdentifierType;
13+
use PHPStan\BetterReflection\Reflection\Reflection;
14+
use PHPStan\BetterReflection\Reflection\ReflectionConstant;
15+
use PHPStan\BetterReflection\Reflector\Reflector;
16+
use PHPStan\BetterReflection\SourceLocator\Ast\Strategy\NodeToReflection;
17+
use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource;
18+
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
19+
use PHPStan\Cache\Cache;
20+
use PHPStan\Internal\ComposerHelper;
21+
use PHPStan\Node\Expr\TypeExpr;
22+
use PHPStan\Php\PhpVersion;
23+
use PHPStan\Reflection\ConstantNameHelper;
24+
use PHPStan\ShouldNotHappenException;
25+
use PHPStan\Type\ConstantTypeHelper;
26+
use ReflectionClass;
27+
use ReflectionFunction;
28+
use function array_key_exists;
29+
use function array_keys;
30+
use function class_exists;
31+
use function constant;
32+
use function count;
33+
use function defined;
34+
use function function_exists;
35+
use function interface_exists;
36+
use function is_file;
37+
use function is_string;
38+
use function opcache_invalidate;
39+
use function restore_error_handler;
40+
use function set_error_handler;
41+
use function spl_autoload_functions;
42+
use function strtolower;
43+
use function trait_exists;
44+
use const PHP_VERSION_ID;
45+
46+
/**
47+
* Use PHP's built in autoloader to locate a class, without actually loading.
48+
*
49+
* There are some prerequisites...
50+
* - we expect the autoloader to load classes from a file (i.e. using require/include)
51+
*
52+
* Modified code from Roave/BetterReflection, Copyright (c) 2017 Roave, LLC.
53+
*/
54+
final class FileCachedSourceLocator implements SourceLocator
55+
{
56+
/** @var array<string, mixed> */
57+
private array $cached;
58+
59+
public function __construct(
60+
private SourceLocator $locator,
61+
private Cache $cache,
62+
private PhpVersion $phpVersion,
63+
private string $cacheKey,
64+
)
65+
{
66+
$variableCacheKey = $this->getVariableCacheKey();
67+
$this->cached = $this->cache->load($this->cacheKey, $variableCacheKey) ?? [];
68+
}
69+
70+
71+
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?\PHPStan\BetterReflection\Reflection\Reflection
72+
{
73+
$key = $identifier->getName();
74+
75+
$this->cached['identifier'] ??= [];
76+
if (!array_key_exists($key, $this->cached['identifier'])) {
77+
$this->cached['identifier'][$key] = $this->locator->locateIdentifier($reflector, $identifier);
78+
$this->storeCache();
79+
}
80+
81+
return $this->cached['identifier'][$key];
82+
}
83+
84+
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
85+
{
86+
$key = $identifierType->getName();
87+
88+
$this->cached['identifiersByType'] ??= [];
89+
if (!array_key_exists($key, $this->cached['identifiersByType'])) {
90+
$this->cached['identifiersByType'][$key] = $this->locator->locateIdentifiersByType($reflector, $identifierType);
91+
$this->storeCache();
92+
}
93+
94+
return $this->cached['identifiersByType'][$key];
95+
}
96+
97+
private function getVariableCacheKey(): string
98+
{
99+
return sprintf('v1-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString());
100+
}
101+
102+
private function storeCache(): void
103+
{
104+
$variableCacheKey = $this->getVariableCacheKey();
105+
$this->cache->save($this->cacheKey, $variableCacheKey, $this->cached);
106+
}
107+
}

0 commit comments

Comments
 (0)