Skip to content

Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector wrong change: conflict with interface #2693

@Aerendir

Description

@Aerendir
Subject Details
Rector version v0.6.10
PHP version PHP 7.2

Rector: Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector

Current Behaviour

      *
      * @throws \RuntimeException
      */
-    public function toString(array $options = []): string
+    public function toString(array $options = []): void
     {
         // @todo Add formatters for the address. @see https://github.com/commerceguys/addressing
         throw new \RuntimeException('Method not implemented. See the @todo in the code.');

I don't know if this can be considered a bug or not as it is really a particular case.

Anyway, as you can see I have a toString() method that should return a string.

The interface, in fact, is like this:

...
public function toString(array $options = []): string;
...

But the concrete method is not yet implemented, so I throw an exception:

    public function toString(array $options = []): string
    {
        // @todo Add formatters for the address. @see https://github.com/commerceguys/addressing
        throw new \RuntimeException('Method not implemented. See the @todo in the code.');
    }

Now, Rector changes my return type hint from string to void as, technically, the method is not returning a string but actually returns void.

But this conflicts with the interface and so the code breaks.

Obviously, not handling this case, for me means that I have to completely disable the rector, so losing a very useful functionality of Rector.

DYT it is possible to handle a case like this?

I'm going to create a PR with a test case anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions