diff --git a/conf/config.neon b/conf/config.neon index 23c2364e6b..c9b573604a 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -461,6 +461,13 @@ services: tags: - phpstan.stubFilesExtension + - + class: PHPStan\PhpDoc\DefaultStubFilesProvider + arguments: + stubFiles: %stubFiles% + autowired: + - PHPStan\PhpDoc\StubFilesProvider + - class: PHPStan\Analyser\Analyser arguments: @@ -511,7 +518,6 @@ services: tempResultCachePath: %tempResultCachePath% analysedPaths: %analysedPaths% composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths% - stubFiles: %stubFiles% usedLevel: %usedLevel% cliAutoloadFile: %cliAutoloadFile% bootstrapFiles: %bootstrapFiles% @@ -615,8 +621,6 @@ services: - implement: PHPStan\File\FileExcluderRawFactory - arguments: - stubFiles: %stubFiles% fileExcluderAnalyse: class: PHPStan\File\FileExcluder @@ -1773,7 +1777,6 @@ services: class: PHPStan\PhpDoc\StubPhpDocProvider arguments: parser: @defaultAnalysisParser - stubFiles: %stubFiles% # Reflection providers diff --git a/conf/config.stubValidator.neon b/conf/config.stubValidator.neon index d08129c1d2..6c4c8bde25 100644 --- a/conf/config.stubValidator.neon +++ b/conf/config.stubValidator.neon @@ -12,7 +12,6 @@ services: class: PHPStan\PhpDoc\StubSourceLocatorFactory arguments: php8Parser: @php8PhpParser - stubFiles: %stubFiles% nodeScopeResolverClassReflector: factory: @stubReflector diff --git a/src/Analyser/IgnoredError.php b/src/Analyser/IgnoredError.php index ab3c1faf6c..52bacade42 100644 --- a/src/Analyser/IgnoredError.php +++ b/src/Analyser/IgnoredError.php @@ -5,6 +5,7 @@ use Nette\Utils\Strings; use PHPStan\File\FileExcluder; use PHPStan\File\FileHelper; +use PHPStan\PhpDoc\EmptyStubFilesProvider; use function count; use function implode; use function is_array; @@ -59,7 +60,7 @@ public static function shouldIgnore( return false; } - $fileExcluder = new FileExcluder($fileHelper, [$path], []); + $fileExcluder = new FileExcluder($fileHelper, new EmptyStubFilesProvider(), [$path]); $isExcluded = $fileExcluder->isExcludedFromAnalysing($error->getFilePath()); if (!$isExcluded && $error->getTraitFilePath() !== null) { return $fileExcluder->isExcludedFromAnalysing($error->getTraitFilePath()); diff --git a/src/Analyser/ResultCache/ResultCacheManager.php b/src/Analyser/ResultCache/ResultCacheManager.php index 9780ba1cf7..65ae5db4fb 100644 --- a/src/Analyser/ResultCache/ResultCacheManager.php +++ b/src/Analyser/ResultCache/ResultCacheManager.php @@ -14,6 +14,7 @@ use PHPStan\File\FileFinder; use PHPStan\File\FileReader; use PHPStan\File\FileWriter; +use PHPStan\PhpDoc\StubFilesProvider; use PHPStan\Reflection\ReflectionProvider; use PHPStan\ShouldNotHappenException; use Throwable; @@ -54,7 +55,6 @@ class ResultCacheManager /** * @param string[] $analysedPaths * @param string[] $composerAutoloaderProjectPaths - * @param string[] $stubFiles * @param string[] $bootstrapFiles * @param string[] $scanFiles * @param string[] $scanDirectories @@ -64,11 +64,11 @@ public function __construct( private ExportedNodeFetcher $exportedNodeFetcher, private FileFinder $scanFileFinder, private ReflectionProvider $reflectionProvider, + private StubFilesProvider $stubFilesProvider, private string $cacheFilePath, private string $tempResultCachePath, private array $analysedPaths, private array $composerAutoloaderProjectPaths, - private array $stubFiles, private string $usedLevel, private ?string $cliAutoloadFile, private array $bootstrapFiles, @@ -180,7 +180,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? $filteredExportedNodes = []; $newFileAppeared = false; - foreach ($this->stubFiles as $stubFile) { + foreach ($this->getStubFiles() as $stubFile) { if (!array_key_exists($stubFile, $errors)) { continue; } @@ -817,7 +817,7 @@ private function getComposerInstalled(): array private function getStubFiles(): array { $stubFiles = []; - foreach ($this->stubFiles as $stubFile) { + foreach ($this->stubFilesProvider->getStubFiles() as $stubFile) { $stubFiles[$stubFile] = $this->getFileHash($stubFile); } diff --git a/src/File/FileExcluder.php b/src/File/FileExcluder.php index 3c5cf85f3b..ff4f63b613 100644 --- a/src/File/FileExcluder.php +++ b/src/File/FileExcluder.php @@ -2,6 +2,7 @@ namespace PHPStan\File; +use PHPStan\PhpDoc\StubFilesProvider; use function array_merge; use function fnmatch; use function in_array; @@ -32,15 +33,14 @@ class FileExcluder /** * @param string[] $analyseExcludes - * @param string[] $stubFiles */ public function __construct( FileHelper $fileHelper, + StubFilesProvider $stubFilesProvider, array $analyseExcludes, - array $stubFiles, ) { - foreach (array_merge($analyseExcludes, $stubFiles) as $exclude) { + foreach (array_merge($analyseExcludes, $stubFilesProvider->getStubFiles()) as $exclude) { $len = strlen($exclude); $trailingDirSeparator = ($len > 0 && in_array($exclude[$len - 1], ['\\', '/'], true)); diff --git a/src/PhpDoc/DefaultStubFilesProvider.php b/src/PhpDoc/DefaultStubFilesProvider.php new file mode 100644 index 0000000000..e0e70e7057 --- /dev/null +++ b/src/PhpDoc/DefaultStubFilesProvider.php @@ -0,0 +1,40 @@ +cachedFiles !== null) { + return $this->cachedFiles; + } + + $files = $this->stubFiles; + $extensions = $this->container->getServicesByTag(StubFilesExtension::EXTENSION_TAG); + foreach ($extensions as $extension) { + foreach ($extension->getFiles() as $extensionFile) { + $files[] = $extensionFile; + } + } + + return $this->cachedFiles = $files; + } + +} diff --git a/src/PhpDoc/EmptyStubFilesProvider.php b/src/PhpDoc/EmptyStubFilesProvider.php new file mode 100644 index 0000000000..d4ad07c653 --- /dev/null +++ b/src/PhpDoc/EmptyStubFilesProvider.php @@ -0,0 +1,13 @@ +> */ private array $knownFunctionParameterNames = []; - /** - * @param string[] $stubFiles - */ public function __construct( private Parser $parser, private FileTypeMapper $fileTypeMapper, - private Container $container, - private array $stubFiles, + private StubFilesProvider $stubFilesProvider, ) { } @@ -269,7 +264,7 @@ private function initializeKnownElements(): void $this->initializing = true; try { - foreach ($this->getStubFiles() as $stubFile) { + foreach ($this->stubFilesProvider->getStubFiles() as $stubFile) { $nodes = $this->parser->parseFile($stubFile); foreach ($nodes as $node) { $this->initializeKnownElementNode($stubFile, $node); @@ -281,22 +276,6 @@ private function initializeKnownElements(): void } } - /** - * @return string[] - */ - private function getStubFiles(): array - { - $stubFiles = $this->stubFiles; - $extensions = $this->container->getServicesByTag(StubFilesExtension::EXTENSION_TAG); - foreach ($extensions as $extension) { - foreach ($extension->getFiles() as $extensionFile) { - $stubFiles[] = $extensionFile; - } - } - - return $stubFiles; - } - private function initializeKnownElementNode(string $stubFile, Node $node): void { if ($node instanceof Node\Stmt\Namespace_) { diff --git a/src/PhpDoc/StubSourceLocatorFactory.php b/src/PhpDoc/StubSourceLocatorFactory.php index 90770fae6f..597b761166 100644 --- a/src/PhpDoc/StubSourceLocatorFactory.php +++ b/src/PhpDoc/StubSourceLocatorFactory.php @@ -9,21 +9,16 @@ use PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator; use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator; -use PHPStan\DependencyInjection\Container; use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository; class StubSourceLocatorFactory { - /** - * @param string[] $stubFiles - */ public function __construct( private Parser $php8Parser, private PhpStormStubsSourceStubber $phpStormStubsSourceStubber, private OptimizedSingleFileSourceLocatorRepository $optimizedSingleFileSourceLocatorRepository, - private Container $container, - private array $stubFiles, + private StubFilesProvider $stubFilesProvider, ) { } @@ -32,15 +27,7 @@ public function create(): SourceLocator { $locators = []; $astPhp8Locator = new Locator($this->php8Parser); - $stubFiles = $this->stubFiles; - $extensions = $this->container->getServicesByTag(StubFilesExtension::EXTENSION_TAG); - foreach ($extensions as $extension) { - foreach ($extension->getFiles() as $extensionFile) { - $stubFiles[] = $extensionFile; - } - } - - foreach ($stubFiles as $stubFile) { + foreach ($this->stubFilesProvider->getStubFiles() as $stubFile) { $locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($stubFile); } diff --git a/tests/PHPStan/File/FileExcluderTest.php b/tests/PHPStan/File/FileExcluderTest.php index 6b816dcf40..439a900cfe 100644 --- a/tests/PHPStan/File/FileExcluderTest.php +++ b/tests/PHPStan/File/FileExcluderTest.php @@ -2,6 +2,7 @@ namespace PHPStan\File; +use PHPStan\PhpDoc\EmptyStubFilesProvider; use PHPStan\Testing\PHPStanTestCase; class FileExcluderTest extends PHPStanTestCase @@ -19,7 +20,7 @@ public function testFilesAreExcludedFromAnalysingOnWindows( { $this->skipIfNotOnWindows(); - $fileExcluder = new FileExcluder($this->getFileHelper(), $analyseExcludes, []); + $fileExcluder = new FileExcluder($this->getFileHelper(), new EmptyStubFilesProvider(), $analyseExcludes); $this->assertSame($isExcluded, $fileExcluder->isExcludedFromAnalysing($filePath)); } @@ -127,7 +128,7 @@ public function testFilesAreExcludedFromAnalysingOnUnix( { $this->skipIfNotOnUnix(); - $fileExcluder = new FileExcluder($this->getFileHelper(), $analyseExcludes, []); + $fileExcluder = new FileExcluder($this->getFileHelper(), new EmptyStubFilesProvider(), $analyseExcludes); $this->assertSame($isExcluded, $fileExcluder->isExcludedFromAnalysing($filePath)); }