Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
janbarasek opened this issue Jul 2, 2021 · 3 comments
Closed

Comments

@janbarasek
Copy link
Contributor

janbarasek commented Jul 2, 2021

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
Copy link
Contributor

The official type name is just never.
php/php-src#6761

@dg
Copy link
Member

dg commented Jul 2, 2021

@return never seems good, can you send PR?

@janbarasek
Copy link
Contributor Author

Solved by #291

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants