Skip to content

Conversation

@samsonasik
Copy link
Member

@samsonasik samsonasik commented Dec 1, 2025

@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

…o allow to not make change return on abstract/interface return
@samsonasik
Copy link
Member Author

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

I just find this option name confusing. Could you name it INCLUDE_STANDALONE_CLASS?

Copy link
Member Author

@samsonasik samsonasik Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include standalone class should be false by default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into this tomorrow. Closing to avoid further configuration on this rule.

Copy link
Member Author

@samsonasik samsonasik Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@samsonasik samsonasik deleted the make-config branch December 1, 2025 23:43
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.

NarrowObjectReturnTypeRector replaces interface with implementing concrete class

4 participants