gpt4 book ai didi

php - 是否有 PHP 的 linter 可以像 Java 一样显式显示所有异常?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:27 27 4
gpt4 key购买 nike

是否有用于 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/

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