6

Is there a way to document that a particular function calls exit()?

The use case is that we have a function that is responsible for handling redirects (checks for already sent headers, set the response code, etc...) and then crucially calls exit(). Unfortunately, PHPStorm has no idea that this particular function terminates execution and thus suggests further warnings as if this function has returned when in practice, it never would.

2
  • I wrote an answer to another (different) question where I explain why I think you should exceptions rather than header/die: stackoverflow.com/a/23855247/951387 Commented Jul 28, 2015 at 9:57
  • @jornane The idea that an exception is raised in anything other than an exceptional circumstance is pretty much an anti-pattern to me. Your argument about it allowing for clean up code to be run is just as possible with a redirect method. Commented Jul 28, 2015 at 14:16

2 Answers 2

4

At the moment it's not possible.

https://youtrack.jetbrains.com/issue/WI-10673 -- watch this ticket (star/vote/comment) to get notified on progress.


ATM I may only suggest placing explicit die() or exit() calls after such function calls.

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I'm after. I like the idea of @noreturn.
3

I know it's too old but now you can describe a function or method that calls die() or exit() by using @return never in PHPDoc:

/**
 * @return never
 */
function dead_inside(): void
{
    die();
}

UPD: Looks like they changed something or that's my fault. @return never doesn't work now as described here, so you should use either @return never-return or @return never-returns (thanks to Lucian P.). Do not forget to declare void as return type just to keep code clean.

3 Comments

Unfortunately this is never passed up the call stack so you have to add @return never and void return definition to every function that call a function that calls a function ... that calls a function that calls die() or exit()
Note: you MUST define a void return type AND @return never PHPDoc or PHPStorm will show "Unreachable statement" warning below your dead_inside() function call
PHPStorm interprets @return never-returns and @return never-return as requested, while @return never is ignored.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.