Skip to content

[TypeDeclaration] Add PrivateMethodReturnTypeFromStrictNewArrayRector, split private methods out#8153

Merged
TomasVotruba merged 2 commits into
mainfrom
split-private-method-return-type-strict-new-array
Jul 8, 2026
Merged

[TypeDeclaration] Add PrivateMethodReturnTypeFromStrictNewArrayRector, split private methods out#8153
TomasVotruba merged 2 commits into
mainfrom
split-private-method-return-type-strict-new-array

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Splits private-method handling out of ReturnTypeFromStrictNewArrayRector into a dedicated, safer rule.

Private methods have no external contract (can't be overridden), so they're the safest case for adding a strict array return type. The new rule runs above the original in the type-declaration level order, and the original now skips private methods.

New rule: PrivateMethodReturnTypeFromStrictNewArrayRector

Handles private methods only. Drops the ClassMethodReturnTypeOverrideGuard check the original needs, since private methods can't be overridden.

 final class SomeClass
 {
-    private function run()
+    private function run(): array
     {
         $values = [];

         return $values;
     }
 }

Changed: ReturnTypeFromStrictNewArrayRector

Now skips private methods (handled by the new rule above); still covers functions and public/protected methods.

 private function shouldSkip(ClassMethod|Function_|Closure $node, Scope $scope): bool
 {
     if ($node->returnType instanceof Node) {
         return true;
     }

+    // private methods are handled by PrivateMethodReturnTypeFromStrictNewArrayRector
+    if ($node instanceof ClassMethod && $node->isPrivate()) {
+        return true;
+    }
+
     return $node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod(...);
 }

New rule registered directly above the original in TypeDeclarationLevel.

@TomasVotruba
TomasVotruba force-pushed the split-private-method-return-type-strict-new-array branch from 3f6bf59 to 8c7656f Compare July 7, 2026 17:03
…, split private methods from ReturnTypeFromStrictNewArrayRector
@TomasVotruba
TomasVotruba force-pushed the split-private-method-return-type-strict-new-array branch from 8c7656f to 0e65933 Compare July 7, 2026 17:14
@TomasVotruba
TomasVotruba merged commit 30f9d4f into main Jul 8, 2026
65 checks passed
@TomasVotruba
TomasVotruba deleted the split-private-method-return-type-strict-new-array branch July 8, 2026 10:19
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