nette / application

🏆 A full-stack component-based MVC kernel for PHP that helps you write powerful and modern web applications. Write less, have cleaner code and your work will bring you joy.
https://doc.nette.org/application
Other
412 stars 106 forks source link

Methods like sendResponse() should contain never-return typehint #290

Closed janbarasek closed 3 years ago

janbarasek commented 3 years ago

Hello,

I think methods like sendJson() should contain PHPStan annotation @phpstan-return never-return like this:

/**
 * Sends JSON data to the output.
 * @throws Nette\Application\AbortException
 * @phpstan-return never-return
 */
public function sendJson(mixed $data): void
{
    $this->sendResponse(new Responses\JsonResponse($data));
}

Currently, this can generate hundreds of errors when PhpStan reports possible undefined variables. The annotation marks the code after this call as dead.

For example:

public function actionDefault(): void
{
    if ($this->variable) {
        $a = 1;
    } else {
        $this->sendJson('error');
    }

    echo $a; // generate possible undefined variable, but it is not true
}

Yes, there is an official extension for PhpStan and Nette framework that does this solution, but it would be good to have native support as well.

Thanks!

JanTvrdik commented 3 years ago

The official type name is just never. https://github.com/php/php-src/pull/6761

dg commented 3 years ago

@return never seems good, can you send PR?

janbarasek commented 3 years ago

Solved by #291