Skip to content

Fix phpstan/phpstan#14073: Performance - list the most-often-analysed trait files in -vvv (DiagnoseExtension)#5395

Open
phpstan-bot wants to merge 10 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-ihwryz1
Open

Fix phpstan/phpstan#14073: Performance - list the most-often-analysed trait files in -vvv (DiagnoseExtension)#5395
phpstan-bot wants to merge 10 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-ihwryz1

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

@phpstan-bot phpstan-bot commented Apr 4, 2026

Implements the feature requested in phpstan/phpstan#14073: when running PHPStan with -vvv, the diagnostic output now includes the top 5 most-often-analysed files (typically trait files). This helps detect performance issues like the one found in Shopware where trait files were analysed an excessive number of times.

Fixes phpstan/phpstan#14073


running this PR on wordpress-develop

➜  wordpress-develop git:(trunk) ../../bin/phpstan analyze src -vvv
Note: Using configuration file /Users/staabm/workspace/phpstan-src/tmp/wordpress-develop/phpstan.neon.dist.
Result cache not used because the metadata do not match: phpstanVersion, projectConfig
 1512/1512 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% 21 secs/21 secs

Result cache is saved.

                                                                                                                        
 [OK] No errors                                                                                                         
                                                                                                                        

PHP runtime version: 8.3.30
PHP version for analysis: 7.4-8.5 (from config)

PHPStan version: 2.1.x-dev@a857004
PHPStan running from:
../../bin/phpstan

Extension installer: Not installed

Discovered Composer project root:
/Users/staabm/workspace/phpstan-src/tmp/wordpress-develop

Most often analysed files:
  src/wp-includes/php-ai-client/src/Providers/Http/Traits/WithHttpTransporterTrait.php: 4 times
  src/wp-includes/php-ai-client/src/Providers/Http/Traits/WithRequestAuthenticationTrait.php: 3 times
  src/wp-includes/php-ai-client/src/Common/Traits/WithDataCachingTrait.php: 2 times

Parallel processing scheduler:
# of detected CPU cores:   14
# of analysed files:       1512
# of jobs:                 76
# of spawned processes:    8

Elapsed time: 21.36 seconds
Used memory: 3.98 GB

@staabm staabm requested a review from VincentLanglet April 4, 2026 15:52
@VincentLanglet
Copy link
Copy Markdown
Contributor

I just wonder if a new feature like this should be approved by ondrej @staabm.

Also if we should directly count in the addProcessedFiles to reduce the memory consuption in case there is lot of duplicated files.

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Apr 4, 2026

Also if we should directly count in the addProcessedFiles to reduce the memory consuption in case there is lot of duplicated files.

I think we cannot save much because the path strings are already allocated at the analyzer level and collecting them in an array shouldn't add much more.

@staabm staabm force-pushed the create-pull-request/patch-ihwryz1 branch from bbba77e to cd1c978 Compare April 4, 2026 19:35
@staabm staabm requested a review from ondrejmirtes April 4, 2026 19:35
use function sprintf;

#[AutowiredService]
final class ProcessedFilesDiagnoseExtension implements DiagnoseExtension
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having this separate DiagnoseExtension the logic could be merged into PHPStanDiagnoseExtension

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it was meant to the bot, but I don't think it handle this feedback since you approved the PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that was intentional

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The make tests background task also completed successfully. All checks pass.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes are already pushed to the remote. The commit 023dddf0b contains exactly the same changes I made — merging ProcessedFilesDiagnoseExtension into PHPStanDiagnoseExtension, updating the service config, and removing the separate class and its test.

The review feedback from staabm has been addressed:

  • Removed the separate ProcessedFilesDiagnoseExtension class
  • Merged the "most often analysed files" diagnostic output into PHPStanDiagnoseExtension
  • Added ProcessedFilesCollector and RelativePathHelper as constructor dependencies
  • Updated conf/services.neon to pass simpleRelativePathHelper
  • Removed the now-unnecessary test file
  • Both make phpstan and make tests pass cleanly

@VincentLanglet
Copy link
Copy Markdown
Contributor

I think it handles the comment because of me @staabm #5395 (comment)
I think that answering to a review retrigger the bot (even if the initial message was an approval)

Do you want to revert it then?

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Apr 5, 2026

Lets wait for ondrejs opinion

staabm and others added 8 commits April 5, 2026 19:32
- Added ProcessedFilesCollector service to accumulate processed files during analysis
- Added TraitAnalysisDiagnoseExtension to print top 5 most-analysed files in -vvv output
- Modified WorkerCommand to send processedFiles in parallel worker JSON responses
- Modified ParallelAnalyser to pass processedFiles through postFileCallback
- Modified AnalyseApplication to collect processedFiles in both debug and non-debug modes
- Added unit tests for ProcessedFilesCollector and TraitAnalysisDiagnoseExtension
Address review feedback: instead of having a separate DiagnoseExtension
for processed files, merge the logic into the existing PHPStanDiagnoseExtension.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@staabm staabm force-pushed the create-pull-request/patch-ihwryz1 branch from 023dddf to 38aef23 Compare April 5, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants