20

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

我遇到一个片段 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');
?>

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

请注意,我给我的“运行”调用一个静态字符串,但我们也可以想象:

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

非常感谢!

4

2 回答 2

44

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

/**
* @uses  _iAmUsed()
* @param string $whoAreYou
*/ 
public function run($whoAreYou)
{
    $methodName = '_iAm' . $whoAreYou;
    if (method_exists($this, $methodName)) {
        $this->$methodName();
    }
}
于 2015-11-23T11:09:08.237 回答
6

在方法上方添加noinspection注解:

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

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

于 2014-09-13T02:43:54.860 回答