|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection\BetterReflection\SourceLocator; |
| 4 | + |
| 5 | +use LogicException; |
| 6 | +use PhpParser\PrettyPrinter\Standard; |
| 7 | +use PHPStan\BetterReflection\Identifier\Identifier; |
| 8 | +use PHPStan\BetterReflection\Identifier\IdentifierType; |
| 9 | +use PHPStan\BetterReflection\Reflector\DefaultReflector; |
| 10 | +use PHPStan\BetterReflection\SourceLocator\Ast\Locator; |
| 11 | +use PHPStan\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber; |
| 12 | +use PHPStan\Testing\PHPStanTestCase; |
| 13 | +use TestSingleFileSourceLocator\AFoo; |
| 14 | +use function class_alias; |
| 15 | +use function class_exists; |
| 16 | + |
| 17 | +class AutoloadFunctionsSourceLocatorTest extends PHPStanTestCase |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * A bootstrap autoloader that throws for names outside its own scope must not abort the |
| 22 | + * analysis: this locator asks every registered autoloader, while at runtime the class loader |
| 23 | + * that resolves the class first means the throwing one is never invoked for that name. |
| 24 | + * |
| 25 | + * @see https://github.com/phpstan/phpstan/issues/14976 |
| 26 | + */ |
| 27 | + public function testAThrowingAutoloaderDoesNotStopTheRemainingOnes(): void |
| 28 | + { |
| 29 | + require_once __DIR__ . '/data/a.php'; |
| 30 | + $this->assertFalse(class_exists('ThrowingAutoloaderAlias', false), 'precondition: the alias does not exist yet'); |
| 31 | + |
| 32 | + $GLOBALS['__phpstanAutoloadFunctions'] = [ |
| 33 | + static function (string $class): void { |
| 34 | + throw new LogicException('this should not happen'); |
| 35 | + }, |
| 36 | + static function (string $class): void { |
| 37 | + if ($class !== 'ThrowingAutoloaderAlias') { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + class_alias(AFoo::class, 'ThrowingAutoloaderAlias'); |
| 42 | + }, |
| 43 | + ]; |
| 44 | + |
| 45 | + try { |
| 46 | + $locator = $this->createLocator(); |
| 47 | + $reflection = $locator->locateIdentifier( |
| 48 | + new DefaultReflector($locator), |
| 49 | + new Identifier('ThrowingAutoloaderAlias', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)), |
| 50 | + ); |
| 51 | + |
| 52 | + $this->assertNotNull($reflection, 'the class defined by the second autoloader should be located'); |
| 53 | + $this->assertSame(AFoo::class, $reflection->getName()); |
| 54 | + } finally { |
| 55 | + unset($GLOBALS['__phpstanAutoloadFunctions']); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Nothing resolves the name, so the locator declines - the point is that it declines instead of |
| 61 | + * letting the autoloader's exception surface as an internal error. |
| 62 | + */ |
| 63 | + public function testAThrowingAutoloaderMakesTheLocatorDecline(): void |
| 64 | + { |
| 65 | + $GLOBALS['__phpstanAutoloadFunctions'] = [ |
| 66 | + static function (string $class): void { |
| 67 | + throw new LogicException('this should not happen'); |
| 68 | + }, |
| 69 | + ]; |
| 70 | + |
| 71 | + try { |
| 72 | + $locator = $this->createLocator(); |
| 73 | + $reflection = $locator->locateIdentifier( |
| 74 | + new DefaultReflector($locator), |
| 75 | + new Identifier('NeverDefinedByAnyAutoloader', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)), |
| 76 | + ); |
| 77 | + |
| 78 | + $this->assertNull($reflection); |
| 79 | + } finally { |
| 80 | + unset($GLOBALS['__phpstanAutoloadFunctions']); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private function createLocator(): AutoloadFunctionsSourceLocator |
| 85 | + { |
| 86 | + $container = self::getContainer(); |
| 87 | + |
| 88 | + return new AutoloadFunctionsSourceLocator( |
| 89 | + new AutoloadSourceLocator($container->getByType(FileNodesFetcher::class), false), |
| 90 | + new ReflectionClassSourceLocator( |
| 91 | + new Locator($container->getService('phpParserDecorator')), |
| 92 | + new ReflectionSourceStubber(new Standard()), |
| 93 | + ), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments