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

那年仲夏 提交于 2019-11-30 14:37:18

问题


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!


回答1:


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();
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/25813465/php-variable-function-name-function-pointer-called-how-to-tell-ide-my-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!