Skip to content

Fix #11968: Large enums are slow#5091

Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-xrf75pg
Closed

Fix #11968: Large enums are slow#5091
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-xrf75pg

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

Issue #11968 reported that large enums (~7500 cases) cause slow PHPStan analysis. The fix was already implemented in PR #3695 which optimized IntersectionType::getEnumCases() by replacing expensive md5(describe()) key computation with simple string concatenation (className . '::' . caseName), achieving a 5x speedup. This PR adds a regression test to prevent the issue from resurfacing.

Changes

  • Added testBug11968() method in tests/PHPStan/Analyser/AnalyserIntegrationTest.php
  • Added test data file tests/PHPStan/Analyser/data/bug-11968.php with:
    • A 500-case UnitEnum implementing an interface
    • A match($this) expression mapping all 500 cases to string values
    • Method calls exercising the enum type resolution
    • Helper functions using instanceof narrowing and in_array with the enum type

Root cause

The original performance issue was caused by IntersectionType::getEnumCases() using md5($enumCase->describe(VerbosityLevel::typeOnly())) as the array key for each enum case when computing the intersection of enum cases. For large enums, the describe() call and md5() computation were expensive. The fix (PR #3695) replaced this with a simple $enumCase->getClassName() . '::' . $enumCase->getEnumCaseName() concatenation.

Test

The regression test runs full analysis on a PHP file containing a 500-case UnitEnum with a match expression. It verifies that analysis completes without errors and in reasonable time. This follows the same pattern as existing enum performance tests (bug-10772, bug-10979, bug-11263).

Fixes phpstan/phpstan#11968

- New integration test testBug11968 in AnalyserIntegrationTest
- Test data file with 500-case UnitEnum with match expression and method calls
- Covers the pattern from the issue: large enum with match($this) returning strings
- Ensures analysis of large enums completes in reasonable time without errors

Closes phpstan/phpstan#11968
@staabm staabm closed this Feb 28, 2026
@staabm staabm deleted the create-pull-request/patch-xrf75pg branch February 28, 2026 09:42
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.

2 participants