Skip to content

[TypeDeclarationDocblocks] Fix invalid array-in-key docblock when narrowing long array unions - #8430

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-invalid-array-key-docblock-9865
Sep 2, 2026
Merged

[TypeDeclarationDocblocks] Fix invalid array-in-key docblock when narrowing long array unions#8430
TomasVotruba merged 1 commit into
mainfrom
fix-invalid-array-key-docblock-9865

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9865

ClassMethodArrayDocblockParamFromLocalCallsRector could emit an invalid docblock with an array type in the array-key slot, e.g. @param array<mixed[], mixed> $data.

Cause

When a resolved param type is a union of arrays whose printed form exceeds the max length, TypeNormalizer::generalizeConstantTypes() narrows it via narrowToAlwaysKnownArrayType():

$arrayUniqueKeyType = $this->arrayTypeLeastCommonDenominatorResolver->sharedArrayStructure(...);
return new ArrayType($arrayUniqueKeyType, new MixedType());

sharedArrayStructure() already returns a full array structure (e.g. array<int, mixed>), but the code dropped it into the key slot of a new ArrayType, yielding array<array<int, mixed>, mixed> which prints as the invalid array<mixed[], mixed>. The variable name $arrayUniqueKeyType shows the structure was mistaken for a key type.

Fix

Return the shared array structure directly. The heterogeneous case from the issue now narrows to a plain mixed[], which the existing mixed-array guard skips, so no invalid docblock is written.

Added a skip_* fixture reproducing the exact case from the issue.

…rowing long array unions

sharedArrayStructure() returns a full array structure, but narrowToAlwaysKnownArrayType()
placed it in the key slot of a new ArrayType, producing invalid types like
array<mixed[], mixed>. Return the shared structure directly instead.
@TomasVotruba
TomasVotruba merged commit 0919678 into main Sep 2, 2026
44 checks passed
@TomasVotruba
TomasVotruba deleted the fix-invalid-array-key-docblock-9865 branch September 2, 2026 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Array with invalid key in doc from ClassMethodArrayDocblockParamFromLocalCallsRector

1 participant