From d6d9ef72d73c0030e0a309aa5b9d56a3c8b0c4b1 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 24 Mar 2023 09:02:15 +0100 Subject: [PATCH] Detect large .php baseline file in analysed paths and warn about possible PHPStan slowdown --- conf/config.neon | 1 + src/Command/AnalyseCommand.php | 24 ++++++++++++++++++++ src/DependencyInjection/ContainerFactory.php | 1 + 3 files changed, 26 insertions(+) diff --git a/conf/config.neon b/conf/config.neon index 46d85ee605..f844c37c03 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -424,6 +424,7 @@ parametersSchema: additionalConfigFiles: arrayOf(string()) generateBaselineFile: schema(string(), nullable()) analysedPaths: listOf(string()) + allConfigFiles: listOf(string()) composerAutoloaderProjectPaths: listOf(string()) analysedPathsFromConfig: listOf(string()) usedLevel: string() diff --git a/src/Command/AnalyseCommand.php b/src/Command/AnalyseCommand.php index 2dca70d859..8ea2c1002b 100644 --- a/src/Command/AnalyseCommand.php +++ b/src/Command/AnalyseCommand.php @@ -14,6 +14,8 @@ use PHPStan\File\FileWriter; use PHPStan\File\ParentDirectoryRelativePathHelper; use PHPStan\File\PathNotFoundException; +use PHPStan\File\RelativePathHelper; +use PHPStan\Internal\BytesHelper; use PHPStan\ShouldNotHappenException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -23,9 +25,11 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\StreamOutput; use Throwable; +use function array_intersect; use function array_map; use function count; use function dirname; +use function filesize; use function fopen; use function get_class; use function implode; @@ -225,6 +229,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $inceptionResult->handleReturn(0, null); } + $analysedConfigFiles = array_intersect($files, $container->getParameter('allConfigFiles')); + foreach ($analysedConfigFiles as $analysedConfigFile) { + $fileSize = @filesize($analysedConfigFile); + if ($fileSize === false) { + continue; + } + + if ($fileSize <= 512 * 1024) { + continue; + } + + /** @var RelativePathHelper $relativePathHelper */ + $relativePathHelper = $container->getService('relativePathHelper'); + $inceptionResult->getErrorOutput()->getStyle()->warning(sprintf( + 'Configuration file %s (%s) is too big and might slow down PHPStan. Consider adding it to excludePaths.', + $relativePathHelper->getRelativePath($analysedConfigFile), + BytesHelper::bytes($fileSize), + )); + } + $application = $container->getByType(AnalyseApplication::class); $debug = $input->getOption('debug'); diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 2cc7e76957..df53d7c9f5 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -114,6 +114,7 @@ public function create( 'cliArgumentsVariablesRegistered' => ini_get('register_argc_argv') === '1', 'tmpDir' => $tempDirectory, 'additionalConfigFiles' => $additionalConfigFiles, + 'allConfigFiles' => $allConfigFiles, 'composerAutoloaderProjectPaths' => $composerAutoloaderProjectPaths, 'generateBaselineFile' => $generateBaselineFile, 'usedLevel' => $usedLevel,