Skip to content

[deprecation] Deprecate AddReturnDocblockForDimFetchArrayFromAssignsRector - #8338

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-return-docblock-dim-fetch-array
Aug 10, 2026
Merged

[deprecation] Deprecate AddReturnDocblockForDimFetchArrayFromAssignsRector#8338
TomasVotruba merged 1 commit into
mainfrom
deprecate-return-docblock-dim-fetch-array

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

The rule guesses an array shape from conditional assigns inside a method body, then writes it as a @return docblock.

That guess only holds if the method body is the whole story. Any later assign, a loop, a dynamic key or a caller-side mutation widens the real type, and the docblock is then too narrow - a silently wrong annotation that PHPStan trusts.

 final class SomeClass
 {
+    /**
+     * @return array<string, string>
+     */
     public function toArray(): array
     {
         $items = [];

         if (mt_rand(0, 1)) {
             $items['key'] = 'value';
         }

         return $items;
     }
 }

The narrow array<string, string> is only correct by accident here; one $items[] = new Item() elsewhere makes it a lie.

Add the docblock manually, or write a custom rule with project-specific knowledge.

Changes:

  • rule implements DeprecatedInterface and throws on refactor()
  • removed from the TypeDeclarationDocblocksLevel set
  • test case + fixtures removed

…ector, as the guessed array shape is vague and unreliable

The rule guesses the array shape from conditional assigns inside a method.
Any later assign, loop or dynamic key can widen the real type, so the added
@return docblock is often too narrow. Add the docblock manually instead.
@TomasVotruba
TomasVotruba merged commit 091e1ef into main Aug 10, 2026
54 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-return-docblock-dim-fetch-array branch August 10, 2026 19:16
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.

1 participant