We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
这不是基本测试,而是需要对令牌流进行重要的分析。
您可以使用
phpmd
phpcs
或者 - 如果这还不够 - 使用
bytekit-cli
您必须为此编写自己的嗅探。
更多工具和资源:
This is not basic testing, but requires non-trivial analysis of the token stream.
You can either use
phpmd
phpcs
or - if that doesn't suffice - look at the bytecode level with
bytekit-cli
You will have to write you own sniffs for that.
Further tools and resources:
一个好的 IDE 应该指出开发过程中的基本错误。
例如,我使用 Netbeans,它突出显示了常见的代码错误,例如定义但未使用的变量,或者在 if() 条件中误用赋值运算符,其中等式运算符更为正常(即编写
if ($x = $y)
而不是if($x == $y)
)。类似的基本内容会显示在 Netbeans 中,并在行号旁边显示一个黄色警告三角形。其他 IDE 也会有类似的功能。
我不认为它会识别出您所描述的特定错误条件,但它肯定会识别出相当数量的错误,即使对于您谈到的错误,这也是我期望标记此类事情的地方,而不是在单独的工具中。
A good IDE should point out basic errors as part of the development process.
I use Netbeans, for example, and it highlights common code mistakes such as variables which are defined but not used, or mis-use of assignment operators in an if() condition where an equality operator is more normal (ie writing
if($x = $y)
instead ofif($x == $y)
).Basic stuff like that shows up in Netbeans with a yellow warning triangle by the line number. Other IDEs will have similar features.
I don't think it picks up the specific error conditions you described, but it certainly picks up a fair number of errors, and even for the errors you talked about, this is where I would expect those kind of things to be flagged up, rather than in a separate tool.
您想要的传统上称为静态分析工具。这些工具经常做什么,
确定代码中的每个点,它了解变量的哪些事实
(在 X= NULL 之后,工具知道 X 为 NULL),然后沿着各种控制流路径传播它所知道的信息,以查看变量的状态是否与操作不一致(例如,在发现 X 为空之后,查找代码必须执行尝试以数组形式访问 X 的操作)。
为了做好这一点,您需要一个完整的 PHP 解析器,生成 AST、至少告诉您 PHP 变量范围的符号表、确定控制和数据流的某种方法,以及检测各种类型的信息集合的一堆模式的编码错误。
PHP 的此类工具之一是 PHPSat。它似乎做了其中的一些工作,您可能可以下载并运行它(我对此没有具体的经验)。它所基于的技术 Stratego 至少适合这项任务; Stratego 产生 AST,并且可以从其中的各个地方收集事实,尽管我不认为它在控制和数据流方面非常擅长。这与仅可以访问 PHP 令牌(例如另一个答案中提到的 PHPCS)的工具形成对比;仅通过令牌来计算控制和数据流是一场噩梦,实际上根本无法完成。
正确的机制似乎隐藏在 Paul Biggar 的论文中。不过,我似乎找不到任何迹象表明有人选择了它并将其用作静态分析器的基础。
What you want is traditionally called a static analysis tool. What such tools often do,
is determine for each point in the code, what kind of facts it knows about the variables
(after X= NULL, the tool knows X is NULL), and then propagates what it knows along various control flow paths to see if that state of the variables are inconsistent with an operation (e.g., after find that X is null, finding code that must be executed which attempts to access X as an array).
To do this well, you need a complete PHP parser, producing ASTs, symbol tables telling you at least the scopes of PHP variables, some way to determine control and data flow, and bunch of patterns over this collective set of information that detects various kinds of coding errors.
One such tool for PHP is PHPSat. It appears to do some of this and you can likely download an run it (I have no specific experience with it). The technology on which it is built, Stratego, is at least appropriate for the task; Stratego produces ASTs and can collect facts from various places in it, although I don't think it is very good at control and data flow. This is in contrast to a tool that simply has access to PHP tokens such as PHPCS mentioned in another answer; computing control and data flow from just the tokens is such a nightmare that in practice it won't get done at all.
The right machinery seems to be hiding in Paul Biggar's thesis. I can't seem to find any hints that anybody picked this up and used it as the basis for a static analyzer, though.