Repository navigation
Expand file tree
/
Copy pathAutoloadFunctionsSourceLocator.php
More file actions
78 lines (66 loc) · 2.6 KB
/
Copy pathAutoloadFunctionsSourceLocator.php
File metadata and controls
78 lines (66 loc) · 2.6 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
<?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\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use Throwable;
use function class_exists;
use function interface_exists;
use function PHPStan\autoloadFunctions;
use function trait_exists;
final class AutoloadFunctionsSourceLocator implements SourceLocator
{
public function __construct(
private AutoloadSourceLocator $autoloadSourceLocator,
private ReflectionClassSourceLocator $reflectionClassSourceLocator,
private BootstrapAutoloaderExceptionReporter $exceptionReporter,
)
{
}
#[Override]
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
if (!$identifier->isClass()) {
return null;
}
$className = $identifier->getName();
if (class_exists($className, false) || interface_exists($className, false) || trait_exists($className, false)) {
return null;
}
$autoloadFunctions = autoloadFunctions();
foreach ($autoloadFunctions as $autoloadFunction) {
try {
$autoloadFunction($className);
} catch (Throwable $e) {
// This locator asks every bootstrap-registered autoloader for the class, which is not
// the order PHP uses: at runtime the class loader that resolves the class first is
// often another one, so an autoloader that throws for names outside its own scope is
// never invoked for them and its exception cannot happen. Letting it propagate here
// aborts the analysis of the file with an internal error instead, so it is swallowed
// and the remaining autoloaders and source locators get their turn. The class may
// still have been defined before the throw, so the locators below run either way.
// The autoloader ran for real, so its message is its real reason - with --debug,
// the reporter prints it.
// See https://github.com/phpstan/phpstan/issues/14976
$this->exceptionReporter->report($className, $e);
}
$reflection = $this->autoloadSourceLocator->locateIdentifier($reflector, $identifier);
if ($reflection !== null) {
return $reflection;
}
$reflection = $this->reflectionClassSourceLocator->locateIdentifier($reflector, $identifier);
if ($reflection !== null) {
return $reflection;
}
}
return null;
}
#[Override]
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
return [];
}
}