PHP: Variable function name (function pointer) called ; How to tell IDE my function is called?
Asked Answered
A

2

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!

Appendicitis answered 12/9, 2014 at 17:13 Comment(3)
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.Almondeyed
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.Garrotte
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)Hiddenite
H
50

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();
    }
}
Hardshell answered 23/11, 2015 at 11:9 Comment(4)
This is well better than the "noinspection" tag. Thank you very well.Appendicitis
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.Appendicitis
@Appendicitis 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?Molal
Looks like @used-by is not really a tag to be manually added. It is an auto generated tag mentioned here, 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".Doerrer
H
6

Add a noinspection annotation above the method:

/** @noinspection PhpUnusedPrivateMethodInspection */
private function _iAmUsed()
{
    //Do Stuff...
}

Or after running code analysis you can right-click any inspection in the results window and choose Suppress for statement to have PHPStorm add the proper annotation itself. For more information see http://www.jetbrains.com/phpstorm/webhelp/suppressing-inspections.html

Hydrocephalus answered 13/9, 2014 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.