Skip to content

fix invalid union return phpdocs with void. #8349

@staabm

Description

@staabm

Feature Request

from time to time I stumble over phpdoc return types like @return HelloWorld|void which are invalid, because void cannot be combined with another type.

since a recent PHPStan release this is now getting reported - but only in final classes:
https://phpstan.org/r/93a4597d-313d-4d93-b8dd-2b1fdbe5dd16

in native PHP types combining a type with |void already results in a runtime error:
https://3v4l.org/vNCne

Diff

it would be great if rector could replace |void in phpdocs

<?php declare(strict_types = 1);

final class HelloWorld
{
    /**
-     * @return HelloWorld|void
+     * @return HelloWorld|null
     */
    public function add()
    {
		if ('POST' !== $_SERVER['REQUEST_METHOD']) {
            return;
        }
		
		return new HelloWorld();
	}
}

after rector turned |void into |null new PHPStan errors might show up in case the method in question did contain a return; without a explicit return value.

Method HelloWorld::add() should return HelloWorld|null but empty return statement found.

at best rector could then even add explicit return null; at the paths which previous just used return;.

<?php declare(strict_types = 1);

final class HelloWorld
{
    /**
     * @return HelloWorld|null
     */
    public function add()
    {
		if ('POST' !== $_SERVER['REQUEST_METHOD']) {
-            return;
+            return null;
        }
		
		return new HelloWorld();
	}
}

in the end it might even be usefull if this would be 2 separate rules, but I am not sure about that yet.

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