From 2e612596bcd74de5b33e8f276d92cb9f169b4862 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 24 Jun 2020 22:27:49 +0200 Subject: [PATCH] Attempt to load remaining classes after skipping them in ClassBlacklistSourceLocator if they're not in Composer paths --- build/phpstan.neon | 3 -- .../BetterReflectionSourceLocatorFactory.php | 2 + .../ClassWhitelistSourceLocator.php | 53 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/Reflection/BetterReflection/SourceLocator/ClassWhitelistSourceLocator.php diff --git a/build/phpstan.neon b/build/phpstan.neon index 1bd5001f61..90fc22eb59 100644 --- a/build/phpstan.neon +++ b/build/phpstan.neon @@ -36,9 +36,6 @@ parameters: stubFiles: - stubs/ReactChildProcess.php - stubs/ReactStreams.php - staticReflectionClassNamePatterns!: - - '#^PhpParser\\#' - - '#^Hoa\\#' services: - class: PHPStan\Build\ServiceLocatorDynamicReturnTypeExtension diff --git a/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php b/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php index fe2f2efe75..4a86556848 100644 --- a/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php @@ -5,6 +5,7 @@ use PHPStan\DependencyInjection\Container; use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator; use PHPStan\Reflection\BetterReflection\SourceLocator\ClassBlacklistSourceLocator; +use PHPStan\Reflection\BetterReflection\SourceLocator\ClassWhitelistSourceLocator; use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository; use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository; @@ -170,6 +171,7 @@ public function create(): SourceLocator } $locators[] = $locator; } + $locators[] = new ClassWhitelistSourceLocator($this->autoloadSourceLocator, $this->staticReflectionClassNamePatterns); $locators[] = new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber); $locators[] = new EvaledCodeSourceLocator($astLocator, $this->reflectionSourceStubber); diff --git a/src/Reflection/BetterReflection/SourceLocator/ClassWhitelistSourceLocator.php b/src/Reflection/BetterReflection/SourceLocator/ClassWhitelistSourceLocator.php new file mode 100644 index 0000000000..e0cd896a2b --- /dev/null +++ b/src/Reflection/BetterReflection/SourceLocator/ClassWhitelistSourceLocator.php @@ -0,0 +1,53 @@ +sourceLocator = $sourceLocator; + $this->patterns = $patterns; + } + + public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection + { + if ($identifier->isClass()) { + foreach ($this->patterns as $pattern) { + if (Strings::match($identifier->getName(), $pattern) !== null) { + return $this->sourceLocator->locateIdentifier($reflector, $identifier); + } + } + + return null; + } + + return $this->sourceLocator->locateIdentifier($reflector, $identifier); + } + + public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array + { + return $this->sourceLocator->locateIdentifiersByType($reflector, $identifierType); + } + +}