2

PHP的静态代码分析工具似乎有很多,请您推荐一种,可以检测PHP代码中抛出的异常,但永远不会被捕获?(理论上可以停止PHP脚本的执行)。

我很高兴只看到像throw new SomeException()where SomeExceptionextends之类的东西Exception

我不是在寻找太复杂的东西——只是为了警告我,如果我从(你明白了)运行someFunctionThatCanThrow(因为里面有throw声明index.php),我会遇到麻烦。即使在运行时也不会发生。

谢谢。

4

2 回答 2

3

PHPLint似乎是答案。例如,它解析

<?php

function some()
{
    if (time() == 123) {
        throw new Exception("I can't happen");
    }
}

some();

,它永远不会抛出异常(除非你过去),进入:

BEGIN parsing of test-cSdHoW
1:      <?php
2:      
3:      function some()
4:      {
5:       if (time() == 123) {
6:        throw new Exception("I can't happen");

          throw new Exception("I can't happen");
                                                \_ HERE
==== 6: notice: here generating exception(s) Exception

          throw new Exception("I can't happen");
                                                \_ HERE
==== 6: ERROR: exception(s) must be caught or declared to be thrown: Exception
7:       }
8:      }
9:      
10:     some();
==== 3: notice: guessed signature of the function `some()' as void()

        some();
             \_ HERE
==== 10: notice: here generating exception(s) Exception

        some();
             \_ HERE
==== 10: Warning: uncaught exception(s): Exception
END parsing of test-cSdHoW
==== ?: notice: unused package `dummy.php'
==== ?: notice: required module `standard'
Overall test results: 1 errors, 1 warnings.

所以这正是我所要求的:) 添加一个 docblock 并捕获异常不会导致 PHPLint 出现更多错误或警告。

于 2011-11-25T12:04:29.087 回答
2

至于 2015 年,PhpStorm 存在一个 SCA 工具,可作为插件Php Inspections (EA Extended)使用——它进行这种分析,包括嵌套调用。此外,它还考虑了上下文,例如在 __toString 导致致命的非处理异常中,插件会报告这一点。

于 2015-06-13T17:02:44.367 回答