Repository navigation
Description
Bug report
I have some code that downloads a large quantity of data and then analyses it. The data consists of compressed csv and json files stored into a deeply nested directory structure (~8k files, ~8k symlinks, ~20k directories). Within that structure, related data items have symbolic links between them, both ways.
The data is excluded from source control through use of .gitignore. The data is also excluded from phpstan completely via a .phpstan/config.neon file that has:
parameters:
excludePaths:
analyseAndScan:
- ../data-dir (?)
- ../analysis-dir (?)The trailing (?) is to indicate that the directories might not exist at first.
When there is a large quantity of data in those directories, phpstan runs significantly slower than expected and than it would do otherwise:
- With data present, we have to wait anywhere between 1 min 40 and 3 min for analysis to complete, every single time (~170 MB consumed).
- Without data present, analysis takes 8 secs (~ 170 MB consumed), faster with caching.
Also, phpstan has occasionally got itself into a state where it keeps crashing while analysing. From the phpstan stack trace (which I don't have a record of now, sorry!), it appeared that it was crashing in phpstan RecursiveDirectoryIterator code, apparently deeply nested following symlinks in the data directory, jumping back and forth between related data items until it hits some recursion limit - even thought it has no good reason to be probing through that directory at all. Clearing phpstan's cache via phpstan clear-result-cache seemed to help with that.
With data present, I also observe when running with -vvv that it is taking ~30 secs just to restore the result cache. Not sure why that is. The resultCache.php file is 266K in size and does not appear to reference the data directories except in relation to them being excluded via the project config.
When running with --debug -vvv I see that 26 files are being analysed, one of which takes ~3 secs to process, one of which takes ~2 secs to process, while all others take a fraction of a second. The time is being taken up by something else.
I think that phpstan may be unnecessarily iterating over all file system objects in the excluded directory for some reason, perhaps sometimes also following symlinks in that directory (and possibly failing to notice that it has returned back to where it started, so permitting infinite looping), and that this is slowing phpstan down significantly. It is slow with or without utilising the cache, but since restoring the cache is reported as a slow process, there may be a problem there too.
There is an opportunity here to improve performance for the benefit of everyone by ensuring that the structure and contents of excluded directories does not impact on the execution of phpstan.
Code snippet that reproduces the problem
No response
Expected output
Same, but with the time taken to analyse the code being independent of and not being slowed down by the content of excluded directories.
Did PHPStan help you today? Did it make you happy in any way?
PHPStan has helped us update modernise (autloading, strict typing throughout, PHP 7 to 8 migration) a PHP library and two internal applications recently.
Activity
mergeable commentedon Jul 15, 2025
This bug report is missing a link to reproduction at phpstan.org/try.
It will most likely be closed after manual review.
mind-bending-forks commentedon Jul 15, 2025
The specific code is not relevant in this case.
ondrejmirtes commentedon Jul 15, 2025
Hello,
please create a reproducing repository that shows this behaviour. You've described the problem in depth but it's not actionable. I need to see what's going on myself.
mind-bending-forks commentedon Jul 15, 2025
https://github.com/mind-bending-forks/phpstan-13244
mind-bending-forks commentedon Jul 16, 2025
On my system, this gives the following times for analysing a single php file with contents
<?php declare(strict_types=1);only:So, comparing the cases where there was no result cache at the start, we see that:
when irrelevant files are present in an excluded directory. Whereas, it shouldn't make a difference.
I'm experiencing a much bigger slow down in my real-life case than in this example, but hopefully it's sufficient for probing the issue. The difference in slow downs compared to my real-life case may be because of larger data files, or because of the presence of symlinks in the excluded directories too, or it could be that the problem scales with the size of the application being analysed. Not sure.
Footnotes
Time was not displayed. It was probably rounded down to zero seconds and treated as empty. ↩
ondrejmirtes commentedon Jul 17, 2025
You could put the data directory completely outside of analysed paths, so that you don't need to exclude it at all, and PHPStan doesn't need to decide for each file if it's supposed to be excluded or not.
mind-bending-forks commentedon Jul 18, 2025
Thanks for pointing out the workaround. You're correct that once you've worked that there is a problem and what it is, it can be worked around by reorganising files in the project so the data directory sits outside the defined scan paths, or by reconfiguring the defined scan paths in PHPStan configuration so as to not include the data directories.
If you're following the PHPStan documentation to setup your project though:
https://phpstan.org/user-guide/ignoring-errors#excluding-whole-files
which has as examples of using
analyseAndScanexclude data directories, you might not in the first instance even realise that PHPStan is running slower than it ought to, thinking instead that that is it's normal speed. Once you do realise that it is running unusually slow, you might not know why or that it has anything to do with the structure of non-php files in directories that you have excluded. It took me a while to work it out.My report was intending to make you aware that this issue exists, and as per my original post, to note that
You said that if the data file is outside the scan paths (rather than excluded from within the scan paths), then:
The implication here is that PHPStan needs to every scan/enumerate every single file - even in directories that have been marked as excluded - and then take a decision on each one as to whether it should be excluded or not. I don't think that's true. Could PHPStan iterate over the file structure starting from the top level files and sub-directories, deciding whether they are excluded, and only then go down to the next level for sub-directories that have not been excluded (or have not had all their contents excluded)?
I'm not sufficiently familiar with the PHPStan code to know what part does what and for what purpose. A search for
RecursiveDirectoryIteratorled to aFileCacheStorageclass that seems to suggest that when PHPStan is doing caching of files, it is not accounting for exclusions at all, and so is iterating over everything. Might this explain why the speed of result cache generation and restoration is affected by files in excluded directories?ondrejmirtes commentedon Jul 21, 2025
Sure, some optimization could be done. Right now PHPStan asks
FileExcluder::isExcludedFromAnalysing()for each file it finds becauseexcludePathsmight contain fnmatch patterns.The class where the optimization could happen is FileFinder. It uses Symfony Finder.
There was even a PR I didn't merge phpstan/phpstan-src#2503 based on symfony/symfony#50877 + symfony/symfony#50884.
When we can update to Symfony 6.4 (and start using
filtermethod on Finder) we'll get the update and optimization automatically.mind-bending-forks commentedon Jul 21, 2025
Thanks @ondrejmirtes. I see that the documentation for the
findermethod of Symfony 6.4+ saysSo yes, that sounds like exactly what would be required to get this enhancement implemented straightforwardly.