Search code examples
phpstormphpdoc

How do I declare that a function dies so it is detected by PhpStorm?


Background

I use several helper functions to stop the program flow and return data. For example, while most pages are HTML I occasionally return JSON and call

/**
 * @param array|bool $response
 *
 * @die
 */
function jsonResponseDie($response)
{
  header('Content-Type: application/json');
  echo json_encode($response);
  die();
}

The Problem

However, the calling function does not detect that there is a die statement and allows code to be present after it without warning.

function recievePush()
{
  // Process a push request and respond with a json array.

  jsonResponseDie(array('status' => TRUE));
  echo 'This will never execute but PhpStorm doesn\'t know that';
}

The Question

How do I get PhpStorm to detect this function will die?

I did try a few items "@return die" or "@die" but these do not appear to be recognized. I also reviewed some documentation here but found nothing useful.


Solution

  • There is no special tags for such stuff in PHPDoc. PhpStorm also does not have any solution to that yet.

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


    UPDATE 2020-10-20: The aforementioned ticket has been implemented and such functionality is now available since PhpStorm 2020.1.x version.

    It is implemented using Advanced Metadata functionality (by creating separate PHP-like file for IDE eyes only): https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#define-exit-points

    <?php
    
    namespace PHPSTORM_META {
        exitPoint(\App\PageFlow::exitApplication());
    }
    

    Another example: as a result, the terminate() call with the bar argument provided will act as an exit point (another value will not trigger this). The last line of the run method will therefore be treated as unreachable:

    exitPoint(Application::terminate('bar'));
    

    enter image description here


    P.S. From PhpStorm 2020.3 (to be released at some point in December or so this year) and using PHP Language Level = 8.0 you will be able to use PHP 8-style attribute #[NoReturn] right in your code instead of separate Advanced Metadata file (see WI-55531 (actual/original implementation) and WI-56103 (new attribute name) tickets).