forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoloadFunctionsSourceLocator.php
More file actions
83 lines (71 loc) · 2.84 KB
/
Copy pathAutoloadFunctionsSourceLocator.php
File metadata and controls
83 lines (71 loc) · 2.84 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
<?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 function class_exists;
use function function_exists;
use function interface_exists;
use function PHPStan\autoloadFunctions;
use function PHPStan\autoloadFunctionsPrependedToComposer;
use function trait_exists;
final class AutoloadFunctionsSourceLocator implements SourceLocator
{
/**
* @param bool $prependedToComposer When true, consult only the autoloaders
* registered before Composer's class loader (prepended); otherwise consult
* the ones registered after it (appended).
*/
public function __construct(
private AutoloadSourceLocator $autoloadSourceLocator,
private ReflectionClassSourceLocator $reflectionClassSourceLocator,
private bool $prependedToComposer,
)
{
}
#[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;
}
// If the name is already a defined function, this locator must not run the bootstrap
// autoloaders for it: a catch-all autoloader (e.g. PHP_CodeSniffer's, which falls back to
// Composer's findFile()) would resolve the name to the function's own file and plain-include
// it a second time - it was loaded once already, e.g. by a package that ships one function
// per PSR-4 path and requires it from its bootstrap - fatally redeclaring the function.
// Returning null only declines this locator; a class and a function may share a name in PHP,
// and a class that genuinely exists under this name in another file is still located by the
// later source locators in the chain. See https://github.com/phpstan/phpstan/issues/14988
if (function_exists($className)) {
return null;
}
$autoloadFunctions = $this->prependedToComposer
? autoloadFunctionsPrependedToComposer()
: autoloadFunctions();
foreach ($autoloadFunctions as $autoloadFunction) {
$autoloadFunction($className);
$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 [];
}
}