Skip to content

Conversation

@gnutix
Copy link
Contributor

@gnutix gnutix commented Feb 2, 2026

🤖 Generated by Claude Code

Fixes phpstan/phpstan#8636 (which has happened on our project too)

Summary

  • Add new configuration parameter constantArrayTypeBuilderArrayCountLimit that allows users to customize the array count limit
  • Default value remains 256 (current behavior)
  • Follow the existing BleedingEdgeToggle pattern with a static accessor class
  • Keep the original constant for backward compatibility (marked as deprecated)

Usage

Users can configure the limit in their phpstan.neon:

parameters:
    constantArrayTypeBuilderArrayCountLimit: 512

Test plan

  • Unit tests pass: vendor/bin/phpunit tests/PHPStan/Type/Constant/ConstantArrayTypeBuilderTest.php (7 tests, 41 assertions)
  • Syntax validation: All modified files pass php -l syntax check
  • Manual verification with test script demonstrating the feature:
<?php
use PHPStan\DependencyInjection\ConstantArrayTypeLimitAccessor;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantStringType;

// Build array with 300 elements
$builder = ConstantArrayTypeBuilder::createEmpty();
for ($i = 0; $i < 300; $i++) {
    $builder->setOffsetValueType(new ConstantStringType("key_$i"), new ConstantStringType("value_$i"));
}

// With default limit (256): array is degraded
$type = $builder->getArray();
// Result: PHPStan\Type\IntersectionType (degraded)

// With increased limit (512): array is tracked precisely
ConstantArrayTypeLimitAccessor::setLimit(512);
$builder2 = ConstantArrayTypeBuilder::createEmpty();
for ($i = 0; $i < 300; $i++) {
    $builder2->setOffsetValueType(new ConstantStringType("key_$i"), new ConstantStringType("value_$i"));
}
$type2 = $builder2->getArray();
// Result: PHPStan\Type\Constant\ConstantArrayType with 300 keys (precise)

Test results:

Array Size Limit Result
200 256 ✓ Precise (ConstantArrayType)
300 256 ✗ Degraded (IntersectionType)
300 512 ✓ Precise (ConstantArrayType)
600 512 ✗ Degraded (IntersectionType)

Add new configuration parameter `constantArrayTypeBuilderArrayCountLimit`
that allows users to customize the array count limit (default: 256).

This follows the existing BleedingEdgeToggle pattern with a static accessor.
The original constant is kept for backward compatibility but marked deprecated.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@staabm
Copy link
Contributor

staabm commented Feb 2, 2026

you should measure the perf impact when using different values for this limit, as it could get very slow depending on your code

@ondrejmirtes
Copy link
Member

Hi, I disagree with this solution. I don't want to let people shoot themselves in the foot by slowing down PHPStan.

The right solution to this problem is to improve ConstantArrayType performance so we can raise the limit.

If you have some examples where the current limit limits you, please open issues about them and we'll try to make the limit higher by making PHPStan faster.

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.

Oversimplified huge const array

3 participants