【发布时间】:2016-02-25 01:55:34
【问题描述】:
我正在处理一个开源项目,并认为使用 phpmd 实现自动代码修订是个好主意。
它向我展示了许多我已经修复的编码错误。但是其中一个让我很好奇。
考虑以下方法:
/**
*
* @param string $pluginName
*/
public static function loadPlugin($pluginName){
$path = self::getPath()."plugins/$pluginName/";
$bootPath = $path.'boot.php';
if(\is_dir($path)){
//Autoload classes
self::$classloader->add("", $path);
//If theres a "boot.php", run it
if(is_file($bootPath)){
require $bootPath;
}
}else{
throw new \Exception("Plugin not found: $pluginName");
}
}
这里,phpmd 说Else is never necessary
...永远不需要带有 else 分支的 if 表达式。你可以 以不需要 else 的方式重写条件,并且 代码变得更易于阅读。 ...
is_dir 将在给定路径是文件或根本不存在时返回 false,因此,在我看来,此测试根本无效。
有没有办法解决它,或者干脆忽略这种情况?
【问题讨论】:
-
只是小费。 CodeSniffer 中有 Sniff 替代方案:github.com/object-calisthenics/…
标签: php coding-style conventions phpmd