作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
是否有用于 PHP 的 lint/static 分析器在未记录或捕获异常时发出警告?考虑这个例子:
// ERROR: InvalidArgumentException must be documented or caught inside method.
function divide($a, $b)
{
if (0 == $b) {
throw new InvalidArgumentException();
}
return $a / $b;
}
修复:
/**
* @throws InvalidArgumentException if $b is zero.
*/
function divide($a, $b)
因为必须要文档化,类似于Java在方法原型(prototype)上显式的throws
。那么这应该是可能的:
// ERROR: InvalidArgumentException must be documented or caught inside method.
function calc()
{
print divide(6, 2);
}
PHP 有一些明显的注意事项,但在大多数情况下,应该及早检测到这些缺陷。是否有任何 linter 可以执行此操作?
最佳答案
您可以使用 PHPCS
您需要为 PHPDOC 添加自己的规则,here's the Sniff
我猜你将它添加到你的规则中:
<?xml version="1.0"?>
<ruleset name="My rules">
<rule ref="Squiz.Commenting.FunctionCommentThrowTag" />
</ruleset>
但我还没有测试过。 确认有效...现在我有 phpdoc 要添加。 :/
我的 phpcs.xml:
<?xml version="1.0"?>
<ruleset name="PSR1/2">
<description>Example</description>
<file>./api</file>
<exclude-pattern>*/Database/Proxies/*</exclude-pattern>
<rule ref="PSR1" />
<rule ref="PSR2" />
<rule ref="Squiz.Commenting.FunctionCommentThrowTag" />
</ruleset>
$ bin/phpcsFILE: ...ttpdocs/api/Api/Version1/Software/AbstractSoftwareController.php----------------------------------------------------------------------FOUND 1 ERROR AFFECTING 1 LINE---------------------------------------------------------------------- 60 | ERROR | Missing @throws tag for "\DomainException" exception----------------------------------------------------------------------Time: 5.55 secs; Memory: 19.5Mb
关于php - 是否有 PHP 的 linter 可以像 Java 一样显式显示所有异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29596848/
我是一名优秀的程序员,十分优秀!