Skip to content

[Rector] Add PreferGenericTemplateOnSameUnionTypeRule - #349

Merged
TomasVotruba merged 2 commits into
mainfrom
tv-prefer-generic-template-rule
Oct 12, 2026
Merged

TomasVotruba merged 2 commits into
mainfrom
tv-prefer-generic-template-rule

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Extracts PreferGenericTemplateOnSameUnionTypeRule from rector-src utils/phpstan into this package, under the Rules/Rector namespace and registered in config/rector-rules.neon.

The rule flags a method whose param and return share the same class-type union, e.g. (A|B $node): A|B|null. Such a method usually returns the passed node, so a @template generic narrows the return to the passed type.

Skips methods that already declare a @template, scalar unions, and mismatched unions.

PHP 7.4 compatible - no native union param type, strpos() instead of str_contains().

@TomasVotruba
TomasVotruba merged commit 912bd59 into main Oct 12, 2026
9 checks passed
@TomasVotruba
TomasVotruba deleted the tv-prefer-generic-template-rule branch October 12, 2026 08:44
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.

1 participant