Skip to content

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

Description

@janbarasek

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions