[TypeDeclaration] Make configurable on NarrowObjectReturnTypeRector to allow to not make change return on abstract/interface return#7695
[TypeDeclaration] Make configurable on NarrowObjectReturnTypeRector to allow to not make change return on abstract/interface return#7695samsonasik wants to merge 2 commits intomainfrom
Conversation
…o allow to not make change return on abstract/interface return
|
All checks have passed 🎉 @TomasVotruba it is ready for review. |
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->ruleWithConfiguration(NarrowObjectReturnTypeRector::class, [ | ||
| NarrowObjectReturnTypeRector::IS_ALLOW_ABSTRACT_AND_INTERFACE => false, |
There was a problem hiding this comment.
Looks good 👍
I just find this option name confusing. Could you name it INCLUDE_STANDALONE_CLASS?
There was a problem hiding this comment.
That means the value need to be true, default false, but "include" term means "also", which a bit contradictive imo.
For better word, I think it can be ALLOW_CONTRACT_RETURN
It is about adding return type which return type is contract.
There was a problem hiding this comment.
Include standalone class should be false by default.
There was a problem hiding this comment.
It is nothing todo with standalone class or not, it is about returning abstract or interface as return type, whether class extends another class or not, if the method return interface, this config will skip.
There was a problem hiding this comment.
I see. I've checked the original report again.
In that case we don't need any configuration. That case should be skipped, as it would create breaking code.
There was a problem hiding this comment.
we had discussion about this on original PR with @Orest-Divintari and @ruudk , the use of replace interface/abstract class to concrete class is valid,
But as @calebdw and @WyriHaximus pointed, there are use case which keep interface/abstract as return type make sense, usually on unit test purpose which still use mock object everywhere.
There was a problem hiding this comment.
Yes, but how do you make a boundary which interface should be replaced and which not? This rule replace e.g. all getRule() in phsptan tests with exact return type of the rule. It doesn't add any value, as parent return type is generic on purpose. It's part of contract.
Once we start adding such configuration, this rule turns into manual labour I want to avoid. This rule should only replace one object type with more specific one. Not all interface types with exact returns, which sparks last 2 reports.
There was a problem hiding this comment.
I'll look into this tomorrow. Closing to avoid further configuration on this rule.
There was a problem hiding this comment.
Common interface type to be skipped:
- Interface return with
Psr\\prefix - Interface return with
Interop\\prefix
Common interface type should be allowed to be replaced:
- Doctrine\Common\Collections\Collection
- PhpParser\Node
This is what I proposed in old PR 3 weeks ago
and closed.
@Orest-Divintari @ruudk @calebdw per our discussion on the original PR:
the contract return was returned, but this cause some issue reported while interface return is valid, see issue:
This PR make it configurable, for allow to skip abstract/interface return, for whatever default value is, this imo should be configurable.
Closes rectorphp/rector#9525