1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\SourceLocator\Type; |
6: | |
7: | use InvalidArgumentException; |
8: | use ReflectionClass; |
9: | use PHPStan\BetterReflection\Identifier\Identifier; |
10: | use PHPStan\BetterReflection\SourceLocator\Ast\Locator; |
11: | use PHPStan\BetterReflection\SourceLocator\Exception\InvalidFileLocation; |
12: | use PHPStan\BetterReflection\SourceLocator\Located\EvaledLocatedSource; |
13: | use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; |
14: | use PHPStan\BetterReflection\SourceLocator\SourceStubber\SourceStubber; |
15: | use PHPStan\BetterReflection\Util\ClassExistenceChecker; |
16: | |
17: | use function is_file; |
18: | |
19: | final class EvaledCodeSourceLocator extends AbstractSourceLocator |
20: | { |
21: | private SourceStubber $stubber; |
22: | public function __construct(Locator $astLocator, SourceStubber $stubber) |
23: | { |
24: | $this->stubber = $stubber; |
25: | parent::__construct($astLocator); |
26: | } |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | protected function createLocatedSource(Identifier $identifier): ?\PHPStan\BetterReflection\SourceLocator\Located\LocatedSource |
35: | { |
36: | $classReflection = $this->getInternalReflectionClass($identifier); |
37: | |
38: | if ($classReflection === null) { |
39: | return null; |
40: | } |
41: | |
42: | $stubData = $this->stubber->generateClassStub($classReflection->getName()); |
43: | |
44: | if ($stubData === null) { |
45: | return null; |
46: | } |
47: | |
48: | return new EvaledLocatedSource($stubData->getStub(), $classReflection->getName()); |
49: | } |
50: | |
51: | private function getInternalReflectionClass(Identifier $identifier): ?\ReflectionClass |
52: | { |
53: | if (! $identifier->isClass()) { |
54: | return null; |
55: | } |
56: | |
57: | $name = $identifier->getName(); |
58: | |
59: | if (! ClassExistenceChecker::exists($name, false)) { |
60: | return null; |
61: | } |
62: | |
63: | $reflection = new ReflectionClass($name); |
64: | $sourceFile = $reflection->getFileName(); |
65: | |
66: | return $sourceFile !== false && is_file($sourceFile) |
67: | ? null : $reflection; |
68: | } |
69: | } |
70: | |