PHP:调用变量函数名称;如何告诉IDE我的函数被调用?

时间:2022-03-28 05:37:14

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

我目前正在尝试删除我的项目中的所有错误和警告,我的PHPStorm中的检查工具给了我。

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:

我遇到一个片段PHPStorm说“未使用的私有方法_xxx”,虽然它实际上使用,但以动态的方式。这是一个简化的代码段:

<?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?

在这个片段中,PHPStorm将告诉我“未使用的私有方法_iAmUsed”,而事实上,它被使用...我怎么能通过添加PHPDocs或其他东西,让我的IDE理解我的方法实际上是使用的?

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!

非常感谢!

2 个解决方案

#1


30  

mark used methods in phpdoc as @used example

将phpdoc中使用的方法标记为@used示例

/**
* @uses  _iAmUsed()
* @param string $whoAreYou
*/ 
public function run($whoAreYou)
{
    $methodName = '_iAm' . $whoAreYou;
    if (method_exists($this, $methodName)) {
        $this->$methodName();
    }
}

#2


7  

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

或者在运行代码分析后,您可以右键单击结果窗口中的任何检查,然后选择Suppress for statement以使PHPStorm自己添加正确的注释。有关更多信息,请访问http://www.jetbrains.com/phpstorm/webhelp/suppressing-inspections.html

#1


30  

mark used methods in phpdoc as @used example

将phpdoc中使用的方法标记为@used示例

/**
* @uses  _iAmUsed()
* @param string $whoAreYou
*/ 
public function run($whoAreYou)
{
    $methodName = '_iAm' . $whoAreYou;
    if (method_exists($this, $methodName)) {
        $this->$methodName();
    }
}

#2


7  

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

或者在运行代码分析后,您可以右键单击结果窗口中的任何检查,然后选择Suppress for statement以使PHPStorm自己添加正确的注释。有关更多信息,请访问http://www.jetbrains.com/phpstorm/webhelp/suppressing-inspections.html