danielson317
danielson317

Reputation: 3288

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.

Upvotes: 3

Views: 317

Answers (2)

mickmackusa
mickmackusa

Reputation: 47894

I have a CodeIgniter 3.1.11 project that I wanted to implement an exitPoint on my SendJsonResponseAndDie() method which is in my myproject/ci/application/helpers/format_helper.php file.

For me, the solution was as simple as creating a meta directory in myproject, then creating a .phpstorm.meta.php file with the following contents:

myproject/meta/.phpstorm.meta.php:

<?php

namespace PHPSTORM_META {

    function exitPoint($functionReference) {
        return "exitPoint " . $functionReference;
    }

    exitPoint(\SendJsonResponseAndDie());
}

Hey presto, the "unreachable statement" text appears whenever I mouseover code that occurs after the function call anywhere in the project.

I wish, though, that the lines of code that cannot be reached would be dimmed so that I am visually drawn to the problem instead of requiring my hover event.

Some additional reading not mentioned in @LazyOne's answer: https://github.com/JetBrains/phpstorm-stubs/blob/master/meta/.phpstorm.meta.php

Upvotes: 0

LazyOne
LazyOne

Reputation: 165158

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).

Upvotes: 5

Related Questions