diff --git a/src/Command/CommandHelper.php b/src/Command/CommandHelper.php index 935d5eb7bb..4be4654f26 100644 --- a/src/Command/CommandHelper.php +++ b/src/Command/CommandHelper.php @@ -47,7 +47,8 @@ public static function begin( bool $manageMemoryLimitFile = true, bool $debugEnabled = false, ?string $singleReflectionFile = null, - ?string $singleReflectionInsteadOfFile = null + ?string $singleReflectionInsteadOfFile = null, + bool $cleanupContainerCache = true ): InceptionResult { if (!$allowXdebug) { @@ -235,7 +236,9 @@ public static function begin( throw new \PHPStan\Command\InceptionNotSuccessfulException(); } - $containerFactory->clearOldContainers($tmpDir); + if ($cleanupContainerCache) { + $containerFactory->clearOldContainers($tmpDir); + } if (count($paths) === 0) { $errorOutput->writeLineFormatted('At least one path must be specified to analyse.'); diff --git a/src/Command/WorkerCommand.php b/src/Command/WorkerCommand.php index 69dba03e90..a2380080c7 100644 --- a/src/Command/WorkerCommand.php +++ b/src/Command/WorkerCommand.php @@ -107,7 +107,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $allowXdebug, false, false, - $singleReflectionFile + $singleReflectionFile, + null, + false ); } catch (\PHPStan\Command\InceptionNotSuccessfulException $e) { return 1; diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 1e58172194..a6807f4f80 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -12,7 +12,6 @@ use PHPStan\Php\PhpVersion; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Reflection\ReflectionProviderStaticAccessor; -use Symfony\Component\Finder\Finder; use function sys_get_temp_dir; /** @api */ @@ -141,13 +140,15 @@ public function clearOldContainers(string $tempDirectory): void $configurator->setDebugMode(true); $configurator->setTempDirectory($tempDirectory); - $finder = new Finder(); - $finder->name('Container_*')->in($configurator->getContainerCacheDirectory()); + $iterator = new \FilesystemIterator($configurator->getContainerCacheDirectory(), \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::SKIP_DOTS); $twoDaysAgo = time() - 24 * 60 * 60 * 2; - foreach ($finder as $containerFile) { - $path = $containerFile->getRealPath(); - if ($path === false) { + /** + * @var string $fileName + * @var \SplFileInfo $containerFile + */ + foreach ($iterator as $fileName => $containerFile) { + if (preg_match('/^Container_.+\.php(\.meta|\.lock)?$/', $fileName) !== 1) { continue; } if ($containerFile->getATime() > $twoDaysAgo) { @@ -157,6 +158,10 @@ public function clearOldContainers(string $tempDirectory): void continue; } + $path = $containerFile->getRealPath(); + if ($path === false) { + continue; + } @unlink($path); } }