Skip to content

[TypeDeclaration] ReturnTypeDeclarationRector explodes with anonymous class instances being returned #1708

@ravanscafi

Description

@ravanscafi

Current Behaviour

Rector: Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector

I'm not sure if the error is on rector itself or on phpstan, but when running the code below, I'm getting the following error:

[ERROR] Could not process "file.php" file, due to: "Internal error.".

The callstack is something like the following:

PHPStan\ShouldNotHappenException: Internal error.

/rector/vendor/phpstan/phpstan/src/Broker/Broker.php:294
/rector/vendor/phpstan/phpstan/src/Analyser/Scope.php:1093
/rector/vendor/phpstan/phpstan/src/Analyser/Scope.php:364
/rector/packages/NodeTypeResolver/src/NodeTypeResolver.php:300
/rector/packages/NodeTypeResolver/src/NodeTypeResolver.php:258
/rector/packages/NodeTypeResolver/src/NodeTypeResolver.php:308
/rector/src/PhpParser/Node/Manipulator/FunctionLikeManipulator.php:65
/rector/packages/TypeDeclaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php:134
/rector/packages/TypeDeclaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php:85

I'm not quite sure how to fix it, I've tried to take a look at phpstan but it seems too complex for me. So I'm asking for directions here fist.

Minimal PHP Code Causing Issue

public function getObject()
{
    return new class() {};
}

Expected Behaviour

-public function getObject()
+public function getObject(): object
 {
     return new class() {};
 }

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