21

I'm currently trying to remove all errors and warnings I have in my project the Inspection tool from my PHPStorm give to me.

I encounter a snippet PHPStorm says "Unused private method _xxx" while it's actually used, but in a dynamical way. Here is a simplifyed snippet:

<?php
class A
{
    private function _iAmUsed()
    {
        //Do Stuff...
    }

    public function run($whoAreYou)
    {
        $methodName = '_iAm' . $whoAreYou;
        if (method_exists($this, $methodName)) {
            $this->$methodName();
        }
    }
}

$a = new A();
$a->run('Used');
?>

In this snippet, PHPStorm will tell me "Unused private method _iAmUsed" while, in fact, it is used... How can I, by adding PHPDocs or something, whatever, make my IDE understand my method is actually used?

Note that I give to my "run" call, a static string, but we can imagine also this:

<?php
$a->run($_POST['whoYouAre']); //$_POST['whoYouAre'] == 'Used'
?>

Thanks a lot!

danielson317
  • 3,121
  • 3
  • 28
  • 43
niconoe
  • 1,191
  • 1
  • 11
  • 25
  • Just as an FYI, IDE warnings are there just to let you know something could be wrong. Removing all of them (where they're arbitrary anyways) isn't really a feasible goal. – Machavity Sep 12 '14 at 17:19
  • Yes, you shouldn't cater too much to the surface analysis of IDEs (in PHPStorm it's really neither static code checking, nor accommodates much for dynamic features of PHP). Just apply "Ignore this instance" in the reports if you already verified it's working as intended. – mario Sep 12 '14 at 17:37
  • 1
    One of the "stupid" ways of removing such warning (due to your dynamic usage) is to mark that method as `protected` instead of `private`. PhpStorm (as well as any other PHP IDE around here) cannot detect such highly dynamic usage using only static (and near static) code checking (as already mentioned above) – LazyOne Sep 12 '14 at 18:06

1 Answers1

47

mark used methods in phpdoc as @used example

/**
* @uses  _iAmUsed()
* @param string $whoAreYou
*/ 
public function run($whoAreYou)
{
    $methodName = '_iAm' . $whoAreYou;
    if (method_exists($this, $methodName)) {
        $this->$methodName();
    }
}
yawa yawa
  • 1,484
  • 13
  • 7
  • This is well better than the "noinspection" tag. Thank you very well. – niconoe Nov 23 '15 at 15:17
  • 3
    To give more information about this, there's also the tag "used-by" that can helps to refer the usage. Using both "uses" and "used-by" tags allows a quick navigation and reference in both ways. – niconoe Dec 29 '15 at 10:08
  • @niconoe I can't seem to be able to find any reference of this "used-by" annotation (at least for PHPStorm). Can you provide more info? – Aykın Jul 31 '18 at 10:38
  • Looks like `@used-by` is not really a tag to be manually added. It is an auto generated tag mentioned [here](https://docs.phpdoc.org/references/phpdoc/tags/uses.html), with the purpose to provide two-way referencing when the documentation is generated. In fact once one method points to another, we can also tell which other methods are pointing at that one. See e.g. 'Find usages' command works fine. If you'd still like to add a way to navigate the documentation easily in PHPStorm in the other direction, try the `@see` tag to refer back to the "user" from the "used". – Bence Szalai Jul 21 '19 at 20:17