gpt4 book ai didi

php - 为什么干净的代码禁止 else 表达式

转载 作者:IT王子 更新时间:2023-10-29 00:15:55 26 4
gpt4 key购买 nike

我在函数中有这段代码:

if ($route !== null) { // a route was found
$route->dispatch();
} else {
// show 404 page
$this->showErrorPage(404);
}

现在 PHPmd 报错:

The method run uses an else expression. Else is never necessary and you can simplify the code to work without else.

现在我想知道是否真的有更好的代码来避免 else 并只向 if 部分添加 return 语句?

最佳答案

PHPMD 希望您使用早期的 return 语句来避免 else block 。类似于以下内容。

function foo($access) 
{
if ($access) {
return true;
}

return false;
}

您可以通过将以下内容添加到您的类文档 block 来抑制此警告。

/**
* @SuppressWarnings(PHPMD.ElseExpression)
*/

关于php - 为什么干净的代码禁止 else 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32677046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com