3

The Setup

I use a lot of function pointers in my PHP code and some functions are called only by function pointer. A simple and common example that may relate to others:

// Stub of my custom assert failure function.
function assertFailure($file, $line, $code, $message = '')
{
  http_response_code(500);
  echo '<h1>ASSERT FAILURE: </h1>';
  // ...
  die();
}

assert_options(ASSERT_CALLBACK, 'assertFailure');

The problem

PhpStorm is flagging these functions as "not used" as they are used only in function pointers. I like the inspection as it has helped me clean up a lot of old code. So I don't want to turn it off completely.

The question(s)

  1. How do I get PhpStorm to recognize the function is used or at least inline disable the inspection for a specific function? I occasionally use lines that allow me to disable an inspection for specific use case such as a missing break in a switch: /** @noinspection PhpMissingBreakStatementInspection */. I can not find an equivalent for this.

  2. An even better solution would be for PhpStorm to recognize the string as a function pointer so I could jump to definition, check parameter lists, and it would know I was using it. Is there a setting or a comment declaration I can use similar to identifying variable types /** @var DateTime $now */.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
danielson317
  • 3,121
  • 3
  • 28
  • 43
  • Some inspections can be suppressed selectively, but it doesn't look like this is one ofthem. – Barmar Aug 06 '19 at 16:40
  • 2
    Maybe this is helpful: https://stackoverflow.com/questions/25813465/php-variable-function-name-called-how-to-tell-ide-my-function-is-called – Barmar Aug 06 '19 at 16:46
  • @Barmar excellent find. This may be a duplicate now. I was searching for "function pointer" so that solution did not appear for me. – danielson317 Aug 06 '19 at 16:57
  • 2
    `/** @uses assertFailure() */` on top of `assert_options()` call seems to work fine. – Álvaro González Aug 06 '19 at 16:58
  • It's not a general solution, since function references can be used from places you don't have control over, like `array_map('funcName', $some_array);` – Barmar Aug 06 '19 at 18:00
  • @Barmar true but usually functions that are pointed to are designed as function pointers to be called from 1 or 2 global locations. So having a long running list of all the functions callable from a single location is feasible, despite being a bit of a headache. – danielson317 Aug 06 '19 at 18:05
  • @Barmar It's certainly not as nice as IDE magic but it does work, doesn't it? You just need to put `/** @uses funcName() */` right before `array_map('funcName', $some_array);` (or, probably, anywhere in your codebase.) – Álvaro González Aug 07 '19 at 08:34

0 Answers0