Skip to content

Keep the constant values of shapes that degrade only for their number - #7325

Open
janedbal wants to merge 1 commit into
phpstan:2.4.xfrom
janedbal:keep-values-of-widened-shapes
Open

janedbal wants to merge 1 commit into
phpstan:2.4.xfrom
janedbal:keep-values-of-widened-shapes

Conversation

@janedbal

@janedbal janedbal commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Refs phpstan/phpstan#15951

This is a regression in 2.4.0 from 96f80ac (#7306). A list that code appends to in 16 or more branches loses its literal values. PHPStan 2.3.7 keeps the literal values.

/** @param 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m'|'n'|'o'|'p'|'q' $letter */
function takesLetter(string $letter): void
{
}

$letters = [];
if ($a) {
	$letters[] = 'a';
}
// ... 17 conditional appends in total, 'a' to 'q'

foreach ($letters as $letter) {
	takesLetter($letter);
}
// 2.3.7: $letter is 'a'|'b'|…|'q', no error
// 2.4.0: $letter is literal-string&lowercase-string&non-falsy-string
//        Parameter #1 $letter of function takesLetter expects 'a'|'b'|…|'q', literal-string&non-falsy-string given.

Cause

TypeCombinator::widen() degrades array shapes to an oversized general array in two cases:

OversizedArrayWidening generalizes each constant scalar value in the two cases. In the second case the shapes hold only a small number of values. The shape limit bounds the number of shapes, and ARRAY_COUNT_LIMIT bounds the number of values already.

Change

  • OversizedArrayWidening keeps the constant scalar values when the types hold at most ARRAY_COUNT_LIMIT constant array values. Above this limit it generalizes the values as before.
  • The keys and the oversized-array accessory do not change. The list in the example is now array{}|(non-empty-list<'a'|'b'|…|'q'>&oversized-array).
  • The changed class has no mirror in turbo-ext.

Issue 15951

The snippet of the issue no longer reports the argument.type error, because $picked[0] is (1|2|…|16).

The type after the if is array{}|(non-empty-list<1|2|…|16>&oversized-array). The issue expects array{}|list{0: 1|…|16, 1?: …}. This PR does not change when the widening starts. The merge still counts the list that the narrowing splits as 17 shapes. That part of the issue stays open.

Cost

#7306 widens these unions because the exact shapes have a quadratic cost. This PR keeps that widening. The kept values are one union of constants in one general array. Unconditional appends give the same form today: $b[] = 0; … $b[] = 999; gives non-empty-list<0|1|…|999>&oversized-array.

Each later conditional append adds one constant to that union. The analysis of many conditional appends of different constants becomes more expensive.

The Benchmarks workflow of this PR, compared with the workflow of the base commit 65b9ac9 (497 benchmarks):

Benchmark Instructions Memory
AnalysisBench::benchAnalyse@conditional-list-appends.php (80 appends) 1.024B → 1.413B (+38.0 %) 13.95 MiB → 15.60 MiB
AnalysisBench::benchAnalyse@bug-7581.php 1.807B → 1.770B (-2.0 %) 2.878 MiB → 2.854 MiB
The other 495 benchmarks within ±1 %

Local runs of larger generated files show how the cost increases. The table shows the minimum user CPU time and the median peak RSS of the full process. The command is bin/phpstan analyse -c tests/bench/phpstan.neon --debug <file> on PHP 8.5.13 without the turbo extension. The runs before and after the change are interleaved (8 runs for each state, 3 runs for the last row). A run takes approximately 0.5 s before the analysis of the file starts.

File Before After
160 conditional appends of integers 0.67 s, 170 MB 0.73 s, 176 MB
400 conditional appends of integers 1.31 s, 306 MB 1.78 s, 341 MB
1000 conditional appends of integers 5.03 s, 1040 MB 7.61 s, 1226 MB

Tests

  • tests/PHPStan/Analyser/nsrt/widened-shapes-keep-values.php has three cases: a list with one append in each of 17 branches, a list with several appends in each of 6 branches, and 17 shapes with different keys.
  • tests/PHPStan/Analyser/nsrt/bug-15951.php is the snippet of the issue.
  • CallToFunctionParametersRuleTest::testWidenedShapesKeepValues and testBug15951 analyse the two files and expect no error.
  • The four tests fail without the change in OversizedArrayWidening.php and pass with it.
  • The full test suite, make phpstan and the coding standard pass locally on PHP 8.5 without the turbo extension.

CI

  • The failed checks also fail without this PR. All Benchmarks shards fail on 2.4.x. The Larastan, phpstan-dba, phpstan-laravel, Rector and Symplify test jobs fail on other PRs of the same day, for example Check unreachable return statements when fixing a missing return type #7323.
  • Integration - rectorphp/rector-src passes with this PR. On the other PRs it fails with the error of the issue at RectorConfigBuilder.php:599.

— Claude

TypeCombinator::widen() degrades array shapes to an oversized general
array in two cases. The types hold more constant array values than
ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT, or a union holds more shapes
than ArrayWidening::UNION_SHAPES_LIMIT. 96f80ac (phpstan#7306) added the second
case. OversizedArrayWidening generalized each constant scalar value in the
two cases.

This is a regression in 2.4.0 from 96f80ac (phpstan#7306). A list that gets 17
conditional appends of literal strings became
non-empty-list<literal-string&non-falsy-string>&oversized-array. PHPStan
2.3.7 keeps the literal values.

The shape limit bounds the number of shapes. It does not need to forget the
values, because ARRAY_COUNT_LIMIT bounds their number already.
OversizedArrayWidening now keeps the constant scalar values when the types
hold at most ARRAY_COUNT_LIMIT constant array values. Above this limit it
generalizes them as before. The keys and the oversized-array accessory do
not change.

The list is now non-empty-list<'a'|'b'|...>&oversized-array.
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.

1 participant