文档错误标记


Documentation errors tag

我在一个类中有一个方法,我在其中触发了一个错误。

/**
 * Get info
 * @return string|FALSE Info
 */
public function getInfo()
{
    if ($this->info) {
        return $this->info;
    }
    trigger_error('Missing info', E_USER_WARNING);
    return FALSE;
}

我不想在这里抛出异常,因为我真的希望/需要这些代码继续运行。在其他地方,我记录了这个错误,并且记录错误超出了这个类的范围。

但我该如何记录?作为一个例外,我会使用:

/**
 * @throws Exception
 */

有类似的错误吗?我真的希望其他开发人员能够轻松地知道我的代码中发生了什么。

没有针对错误的phpdoc标记。

trigger_error()返回bool,因此您的方法不会返回或抛出任何内容。除非错误处理程序阻止执行,否则执行将继续,因此使用@return或@throws会滥用它们,并且可能会让阅读代码的人感到困惑。


我会使用不同的方法。

我会这样做:

/**
 * Has info
 *
 * @return bool Whether info is available
 */
public function hasInfo()
{
    return (bool) $this->info; // or use isset() or whatever you need
}
/**
 * Get info
 *
 * @throws Exception
 * @return string The info string
 */
public function getInfo()
{
    if (! $this->hasInfo()) {
        throw new Exception('Missing info');
    }
    return $this->info;
}

然后从你的其他代码,你可以做:

if ($object->hasInfo()) {
    $info = $object->getInfo();
} else {
    // no info!
}

我还会在我的代码库的根目录中发现异常:

try {
    MyApp::run();
}
catch(Exception $e) {
    // handle error, eg. display fatal error message
}

我同意其他人的看法,我会在这里改变我的编码方法,但为了解决您的直接问题——我可能会使用@internal标记来解释您希望开发人员注意的事情。当然,当您针对此代码运行phpDocumentor时,@internal标记不会出现在您生成的文档中,除非您使用--parse private运行时选项。。。这是因为internal-info-for-devs被认为是消费者/API感兴趣的读者的禁区,就像"@access private"物品一样。