Skip to content

[TypeDeclaration] Skip trait methods on ParamTypeByMethodCallType rules#8189

Merged
TomasVotruba merged 1 commit into
mainfrom
skip-trait-param-type-by-method-call
Jul 14, 2026
Merged

[TypeDeclaration] Skip trait methods on ParamTypeByMethodCallType rules#8189
TomasVotruba merged 1 commit into
mainfrom
skip-trait-param-type-by-method-call

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Trait methods may be composed into classes that implement an interface contract. A param type inferred from a call inside the trait itself is not safe to add: once the trait is used in a class where the method must match a wider contract signature, the added type breaks it.

Skip ClassMethod nodes whose scope is inside a trait.

 trait SomeTrait
 {
-    public function go($value)
+    public function go(array $value)  // wrong, breaks contract when trait used in class implementing an interface
     {
         $this->use($value);
     }

     private function use(array $value): void
     {
     }
 }

The skip lives in the shared AbstractParamTypeByMethodCallTypeRector::shouldSkipClassMethod(), so it covers all four rules built on it:

  • ArrayParamTypeByMethodCallTypeRector
  • ObjectParamTypeByMethodCallTypeRector
  • ScalarParamTypeByMethodCallTypeRector
  • ParamTypeByMethodCallTypeRector

Trait methods can be used in classes that implement an interface contract.
Adding a param type inferred from a local call would break the signature
when the trait is composed into such a class.
@TomasVotruba TomasVotruba merged commit c607ef0 into main Jul 14, 2026
64 of 65 checks passed
@TomasVotruba TomasVotruba deleted the skip-trait-param-type-by-method-call branch July 14, 2026 15:47
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