phpstan
phpstan copied to clipboard
Phpstan array analysis takes forever
Bug report
In our fork, mostly identical to https://github.com/tecnickcom/TCPDF the analysis of fonts/ takes forever.
I was expecting it to be solved in v1.8.4 phpstan, but it seems the font files, mostly consisting of large arrays, are still not analysed effectively. The array shape is degraded probably not when parsing, and the full array shape is still built once which is very slow.
Code snippet that reproduces the problem
n/a, performance problem
How to reproduce:
- clone
mainbranch of https://github.com/tecnickcom/TCPDF repo - analyse the whole repo directory /wo fonts/ directory (and phpstan level 1) - phpstan should finish in about a minute /w about 70 errors
- analyse the whole repo directory again, but now incl. the fonts/ directory - phpstan takes forever (did not finish in more than 30 minutes)
Expected output
phpstan must finish in about the same time for 2. and 3. repro steps
once fixed, the TCPDF repo should be added to integration tests
How does the performance compare in PHPStan 1.8.5?
With phpstan v1.8.5 and fonts/ directory included, phpstan still does not finish.

I finished my update from PHPStan 1.4 to PHPStan 1.8 and the CI runs time (when there is no result cache) went from ~24 mins to ~62 mins. :flushed:
@enumag This isn't helpful nor actionable. Run with -vvv --debug, find which file takes the longest, isolate the problem, and open a separate issue.
I'm running into a similar issue on my codebase, and have created a small sample here to show the structure that phpstan hangs on. The playground times out when the array contains about 2000 values. https://phpstan.org/r/46a17e15-22a3-414a-b8d7-c27617d5ac5b.
If a longer sample is needed, I've attached a txt file with the same class with 10k values that takes 66 seconds to analyse on my machine. Our codebase contains multiple of these maps, and since the upgrade we did from from 1.8.2 to 1.8.8, the CI pipelines times out.
Thanks for all the hard work you're putting into this project, it's absolutely amazing and a complete paradigm shift for php.
@bart-mollie the slowness in your example code is from analysing the array_search call. It's not the same issue, but perhaps some other examples from your codebase are. Please create a separate issue for this.
Thanks for looking into it! I've created https://github.com/phpstan/phpstan/issues/8147
@mvorisek could you double check with the latest development version?
if it is still slow, could you extract the actually slow files so I can reproduce easily?
you may utilize https://gist.github.com/ruudk/41897eb59ff497b271fc9fa3c7d5fb27
feel free to test on TCPDF repo directly or with https://github.com/mvorisek/TCPDF/commit/a458bc1e61bcf5a2060b78164e57619d0f6569cc with https://github.com/mvorisek/TCPDF/blob/a458bc1e61bcf5a2060b78164e57619d0f6569cc/phpstan.neon.dist#L8 line removed (to reproduce, phpstan should finish in about a minute)
Should be fast now. Please test PHPStan 1.9.5 once it's released :)
Let's keep this issue open until released. If ok, I will be happy to provide a regression test for it then as this caused a lot of trouble for me in the past. Thank you.
@mvorisek I couldn't test your linked commit because the branch probably got deleted, or it's in a different fork.
Anyway, this thread got too long, I want people to open new issues with specific examples. I believe many examples from this thread are fixed.
@ondrejmirtes see https://github.com/tecnickcom/TCPDF/commit/37aa6eeb73b9bfeeb321d846e46b0d98e49ff3d3 last week merged officially to TCPDF - so how to test? clone https://github.com/tecnickcom/TCPDF.git and remove fonts/ from excludePaths in the phpstan config
You can now compare 1.9.4 vs 1.9.x-dev and report back.
I can confirm now TCPDF finish in a few minutes. Thank you.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.