Skip to content

Narrow generic object return type #9462

@Orest-Divintari

Description

@Orest-Divintari

There is this rule already that narrows union return types.

There is also this case of return type that can be narrowed

abstract class AbstractFactory

abstract protected function build() : object;
class AppleFactory extends AbstractFactory
#[Override]
protected function build(): object
{
     return new Apple();
}

In the example above the return type of method build in AppleFactory can be narrowed down to Apple.

class AppleFactory extends AbstractFactory
#[Override]
protected function build(): Apple
{
     return new Apple();
}

One approach is to check whether the method has the override attribute to make the rule less aggressive

But alternatively the rule can check any method that has a return type object and check what is actually returned, and replace the return type.

Would you consider adding such a rule if i make a pr ? If yes, which version ? the one that checks if the method has the Override attribute ? or the more generic

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions