Skip to content

Narrow the native type of an array that array_is_list() rejects from the native type - #7235

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
array-is-list-native-type
Nov 28, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
array-is-list-native-type

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Since 0143e40, the false branch of array_is_list() intersected the argument's PHPDoc type with non-empty-array. That sure type was applied to the native type as well, so with treatPhpDocTypesAsCertain: false, a native ?array with a list<object>|null PHPDoc became *NEVER* and code in that branch reported function.alreadyNarrowedType (seen in symfony's ControllerEvent::setController()).

Both types are now narrowed by non-empty-array alone, so each keeps its own key and value types. array{a?: string} still becomes array{a: string}.

MatchExpressionRuleTest::testBug8937 now reports the native type non-empty-array instead of non-empty-array<string>: the parameter is natively array|string, so <string> came from the PHPDoc.

Closes phpstan/phpstan#15875

Amp thread: https://ampcode.com/threads/T-01a22076-5e23-72be-a499-cc7242c24f93

…the native type

The false branch of array_is_list() made the argument non-empty by
intersecting its PHPDoc type with non-empty-array, and that sure type
was applied to the native type too. A native array with a PHPDoc list
type then became *NEVER*. Narrow both by non-empty-array alone instead.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22076-5e23-72be-a499-cc7242c24f93
@ondrejmirtes
ondrejmirtes merged commit 3038398 into 2.3.x Nov 28, 2026
899 of 916 checks passed
@ondrejmirtes
ondrejmirtes deleted the array-is-list-native-type branch November 28, 2026 05:00
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.

!array_is_list() narrows the native type to *NEVER* with treatPhpDocTypesAsCertain: false (since phpstan-src 0143e40d2)

1 participant